首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

随机生成若干个电话号码解决方法

2013-01-28 
随机生成若干个电话号码#includestdio.h#includestdlib.h#includestring.h#includetime.h#include

随机生成若干个电话号码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>

struct Stu{
    unsigned long ID;
    char name[7];
    char num[13];
};

void setId(char *tel_obj) //n的值暂未使用
{
    int i; //用来给电话号码依次赋值

    char tel_num[10]={'0','1','2','3','4','5','6','7','8','9'}; 
    char tel_link[10];
    strcpy(tel_obj,"13");
    srand((unsigned)time(NULL));
   
        for(i=0;i<9;i++)
    {
            tel_link[i]=tel_num[rand()%10];
    }
        tel_link[9]='\0';
        strcat(tel_obj,tel_link);
        puts(tel_obj);    
}

int main()
{
    FILE *fp;
    int i;
    char tel_obj[12];
    char* p=&tel_obj[12];
    char *tel;
    fp=fopen("sun.txt","w");
//  for(i=0;i<10;i++)  为什么不能循环呢?
    setId(p);
    fclose(fp);
    return 0;
}
[解决办法]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<math.h>

struct Stu{
unsigned long ID;
char name[7];
char num[13];
};

void setId(char *tel_obj) //n的值暂未使用
{
int i; //用来给电话号码依次赋值

char tel_num[10]={'0','1','2','3','4','5','6','7','8','9'}; 
char tel_link[10];
strcpy(tel_obj,"13");
srand((unsigned)time(NULL));

for(i=0;i<9;i++)
{
tel_link[i]=tel_num[rand()%10];
}
tel_link[9]='\0';
strcat(tel_obj,tel_link);
puts(tel_obj);    
}

int main()
{
FILE *fp;
int i;
char tel_obj[12];
char* p=tel_obj;//改这行取数组的开头地址。
char *tel;
fp=fopen("sun.txt","w");
for(i=0;i<10;i++) 
{
setId(p);
fprintf(fp, "%s\n", p);//写入文件 
}
fclose(fp);
return 0;

[解决办法]
srand((unsigned)time(NULL)); 这句最好放在主函数里面,否则随机的数据几乎是一样的。
[解决办法]
参考C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
*       Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>


#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
*       Seeds the random number generator with the int given.  Adapted from the
*       BASIC random number generator.
*
*Entry:
*       unsigned seed - seed to seed rand # generator with
*
*Exit:
*       None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
        unsigned int seed
        )
{
        _getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
*       returns a pseudo-random number 0 through 32767.
*
*Entry:
*       None.
*
*Exit:
*       Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
        void
        )
{
        _ptiddata ptd = _getptd();

        return( ((ptd->_holdrand = ptd->_holdrand * 214013L
            + 2531011L) >> 16) & 0x7fff );
}

热点排行