C语言随机点名

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

#include

#define STU_NUM_MAX64//假设最多有64个学生struct Student

{

char name[10];

int stuID;

}stu[STU_NUM_MAX];

int exist[STU_NUM_MAX];//用以保存被点过名

static int index=0;//记住点名的次数

void Iitialize(){

for(int i=0;i

}

bool IsExist(int id){

for(int i=0;i

if(exist[i]==id)return true;//已存在

return false;//不存在

}

void Add()//添加数据

{

FILE*fp;

int stu_num;

printf("\t\t You want to input the number of student?:");

scanf("%d",&stu_num);

for(int i=0;i

printf("\n");

printf("\t\tPlease input student ID:");

scanf("%d",&stu[i].stuID);

printf("\t\tPlease input student name:");

scanf("%s",stu[i].name);

fflush(stdin);

}

if((fp=fopen("stu.dat","ab"))==NULL){

printf("Can't open file\n");

exit(1);

}

for(int j=0;j

{

if(fwrite(&stu[j],sizeof(struct Student),1,fp)!=1)

printf("Error writing file.\n");

}

fclose(fp);

}

void rollcall()//随机点名

{

FILE*fp;

if((fp=fopen("stu.dat","rb"))==NULL)

{

printf("Can't open file.\n");

exit(1);

}

srand((unsigned)time(NULL));

int i=0;

int randID=rand()%(64-1+1)+1;//1~64

printf("\t\t随机点到的学号为:%d\n\t\t%s\t%s\n",randID,"StuID","StuName");

do

{

fseek(fp,i*sizeof(struct Student),SEEK_SET);

if(fread(&stu[i],sizeof(struct Student),1,fp))

{

if(stu[i].stuID==randID&&!IsExist(randID)){

printf("\t\t%4d\t%5s\n",stu[i].stuID,stu[i].name);

exist[index++]=randID;

break;}

}

i++;

}while(!feof(fp));

fclose(fp);

}

int main()

{

int select=0;

char answer='y';

Iitialize();

do

{

printf("1.添加数据2.随机点名3.退出\n请选择:");

fflush(stdin);

scanf("%d",&select);

switch(select)

{

case1:

Add();

break;

case2:

rollcall();

break;

case3:

return0;

}

fflush(stdin);

printf("You want to continue?:");

scanf("%c",&answer);

}while(answer=='y'||answer=='Y');

return0;

}

#include

#include

#include

#include

#include

struct studentinfo

{

char sNo[5];

char sxueNo[14];

char sname[20];

}st[100];

int main()

{

int i=0,j=0,k,flag,RN,*a;

FILE *fp;

char ch,filename[20]={0},line[100]={0};

printf("Please input file name:");

fflush(stdin);

gets(filename);

fp=fopen(filename,"r");

printf("名单如下:\n");

while (fgets(line,sizeof(line)-1,fp))

{

if (line[0]!='\n' && line[0]!=' ')

{

sscanf(line,"%s %s %s\n",st[i].sNo,st[i].sxueNo,st [i].sname);

printf("%s\n%s\n%s\n",st[i].sNo,st[i].sxueNo,st[i].sna me);

i++;

}

}

srand((unsigned)time(NULL));

a=(int*)malloc(sizeof(int)*i);

memset(a,-1,sizeof(a));

printf("按任意键点名:");

fflush(stdin);

while (!(ch=getch())==NULL)

相关文档
最新文档