📄 randrecord.c
字号:
#include "mydb.h"
char *major[]={"电子","土木","能源","电信","建规","机械","计算机系","软件","经管","数学系","物理系","化学系","道桥","中文系"};
char *subname[]={"马哲","电路","数电","模电","固电","量子","复变","物理","微积分","线代",\
"体育","英语","语文","C语言","近代史","信号","思修","概率论","单片机","数据库",\
"工程制图","电工实习","军事理论","毛概"};
//以上字符串数组都是为了随机产生数据库用到
//本函数将用于产生长为wordsize,类型为type的字符串
//并将其放于buf中
bool makeword(char*buf,int wordsize,int type)
{
int i=0;
if(!buf)return FALSE;
memset(buf,0,BUFSIZE);
for(;i<wordsize&&i<20;i++)
if(type==ALPHAWORD)buf[i]=97+myrand(0,25);
else if(type==NUMBERWORD)buf[i]=48+myrand(0,9);
else return FALSE;
return TRUE;
}
//本函数用于随机将专业与年号班级相搭配
//组成类似"电子0603"的字符串放于buf中
bool makemajor(char *buf)
{
int majorcount=sizeof(major)/sizeof(char*);
int randmajor=myrand(1,10000)%majorcount,randyear=1978+myrand(1,10000)%31;
randyear%=100;
int randclass=myrand(1,10000)%20+1;
memset(buf,0,BUFSIZE);
char temp[10]={0};
itoa(randyear,temp,10);
if(randyear<10)
{
temp[1]=*temp;
*temp=0x30;
}
sprintf(buf,"%s%s",major[randmajor],temp);
memset(temp,0,10);
itoa(randclass,temp,10);
if(randclass<10)
{
temp[1]=*temp;
*temp=0x30;
}
sprintf(buf,"%s%s",buf,temp);
return TRUE;
}
//随机产生学生信息的总控制函数
bool makestu(long stucount)
{
long i=0;
srand(time(0));char buf[20];
pmystu newstu=NEW(mystu);
printf("\n系统正在产生学生信息......");
for(;i<stucount;i++)
{
memset(newstu,0,MYSTUSIZE);
newstu->stu_sex=myrand(1,10000)%2;
if(!makeword(buf,myrand(3,10),ALPHAWORD))return FALSE;
memcpy(newstu->stu_name,buf,BUFSIZE);
printf("%s",buf);
head(BUFSIZE-strlen(buf)+8);
memset(buf,0,BUFSIZE);
itoa((i+1)*100/stucount,buf,10);
strcat(buf,"%");
printf("%s",buf);
back(BUFSIZE+strlen(buf)+8);
if(!makeword(buf,12,NUMBERWORD))return FALSE;
if(search(buf,SEARCH_SCHID)){i--;continue;}
memcpy(newstu->stu_schid,buf,BUFSIZE);
makemajor(buf);
memcpy(newstu->stu_major,buf,BUFSIZE);
addstu(newstu);
}
head(50);back(50);
printf("学生信息产生成功!\n");
return TRUE;
}
//随机产生学科信息的函数
//学分和开课学期具有随机性
bool makesub()
{
printf("\n系统正在产生学科信息......");
psubinfo newsub=NEW(subinfo);
int i;
int randsubcount=sizeof(subname)/sizeof(char*);
for(i=0;i<randsubcount;i++)
{
memset(newsub,0,SUBINFOSIZE);
newsub->sub_term=myrand(1,8);
newsub->sub_rate=myrand(2,12)*0.5;
strcpy(newsub->sub_name,subname[i]);
if(search(newsub->sub_name,SEARCH_SUB))
{i--;continue;}
addsub(newsub);
}
printf("学科信息产生成功!\n");
return TRUE;
}
//每个学生和每门学科的分数都将随机产生并写入数据库
bool makemark()
{
if(!myfileinfo)
{
myfileinfo=NEW(fileinfo);
fcntl(0,READ,myfileinfo,FILEINFOSIZE);
}
printf("\n系统正在产生分数信息......");
psubmark newmark=NEW(submark);
long seek=DATASEEK;
newmark->flag=1;int i,j;
for(i=0;i<myfileinfo->stucount;i++,seek+=DATATRANS)
for(j=0;j<myfileinfo->subcount;j++)
{
newmark->sub_id=j+1;
newmark->sub_mark=50+myrand(0,100)*0.5;
editmark(newmark,seek);
}
printf("学生分数信息产生成功!\n");
return TRUE;
}
//本函数随机产生范围x~y的随机数
//包括超大数
long myrand(long x,long y)
{
int a=rand();
int b=rand();
int c=rand();
long g=a*b+c;
g=x+g%(y-x+1);
return g;
}
//随机建立数据库的总控制函数
bool MakeDataBase()
{
long stucount=0;
printf("\n\n请输入想要产生的学生记录数:");
scanf("%ld",&stucount);
printf("\n系统将为您产生%ld个学生的相关记录.....\n",stucount);
printf("\n系统正在自动为您建立数据库,请稍后......\n");
if(!makestu(stucount))SETERR("\n\n数据库建立失败!\n\n");
if(!makesub())SETERR("\n\n数据库建立失败!\n\n");
if(!makemark())SETERR("\n\n数据库建立失败!\n\n");
printf("\n数据库建立成功!系统为您建立了");
printf("%ld 个学生和%d门科目及所有成绩!\n\n",stucount,RANDSUBCOUNT);
printf("\n\n请按任意键继续.....\n\n");
getch();
return TRUE;
}
void back(int i)
{
int j=0;
for(;j<i;j++)
printf("\b");
}
void head(int i)
{
int j=0;
for(;j<i;j++)
printf(" ");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -