#include<stdlib.h>
#include?<time.h>
#define?STU_NUM_MAX?64?//?假設最多有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<STU_NUM_MAX;i++)?exist[i]=0;
}
bool?IsExist(int?id){
for(int?i=0;i<STU_NUM_MAX;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<stu_num;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<stu_num;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)
{
case?1:
Add();
break;
case?2:
rollcall();
break;
case?3:
?return?0;
}
fflush(stdin);
printf("You?want?to?continue?:");
scanf("%c",&answer);
}?while?(answer=='y'||answer=='Y');
return?0;
}
上面的代碼,我留下幾個細節問題留給妳自己學著解決,都是很簡單的:
上面的代碼,我沒有對重復的學號作判斷。
上面的代碼,我沒有把點名存放到另壹個文件,而是用數組替代(可以實現的也很簡單)。我怕寫得代碼太多,百度限制提交。
上面的代碼,是測試數據,stu.dat目標文件並沒有64個學生,我只寫入了12條數據。
上面的代碼,我沒有對數據數量(最多64條)作判斷。