#include<stdio.h>#include<stdlib.h>#include<time.h>#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<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~64printf("\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<stdio.h>#include <stdlib.h>#include<string.h>#include <conio.h>#include <time.h>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){RN=rand()%i;flag=1;for (k=0;k<i;k++){if (RN==a[k]){flag=0;break;}}if (flag){a[j]=RN;j++;printf("\n~~~~~\n%s\n%s\n%s\n------------\n",st[RN]. sNo,st[RN].sxueNo,st[RN].sname);}}}#include<stdio.h>#include<stdlib.h>#include<time.h>#define STU_NUM_MAX4struct StudentInfo//学生信息结构{char name[15];int stu_id;}stu[STU_NUM_MAX];void WriteData()//写入学生信息{FILE*fp;int stu_num=4;for(int i=0;i<stu_num;i++){printf("请输入第%d个学生的姓名:",i+1);scanf("%s",stu[i].name);printf("请输入第%d个学生的学号:",i+1);scanf("%d",&stu[i].stu_id);}if((fp=fopen("myfile.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 StudentInfo),1,fp)!=1)printf("Error writing file.\n");}fclose(fp);}void TeacherDM(int stuID)//教师点名{FILE*fp;bool find_mark=false;printf("\n\t%s\t\t%s\n","学号","姓名");if((fp=fopen("myfile.dat","rb"))==NULL){printf("Can't open file\n");exit(1);}int i=0;do{fseek(fp,i*sizeof(struct StudentInfo),SEEK_SET);fread(&stu[i],sizeof(struct StudentInfo),1,fp);if(stu[i].stu_id==stuID){printf("\t%4d\t%s\n",stu[i].stu_id,stu[i].name);printf("\n\n\t请【%s】同学回答某某问题.\n",stu[i].name);find_mark=true;break;}i++;}while(!feof(fp));if(!find_mark)printf("\n\t\t未能找到学生号为:%d的记录!\n",stuID);fclose(fp);}void main(void){int stuID[4]={2013011001,2013011002,2013011003,2013011004};//WriteData();srand((unsigned)time(NULL));//随机种子TeacherDM(stuID[rand()%(3-0+1)+0]);}#include <stdlib.h> #include <stdio.h> #include <math.h> #define SIZE 4 struct student {char num[20]; char name[10]; int time; }std[SIZE]; void save() {int i; FILE *fp;if((fp=fopen("myfile.dat","rb+"))==NULL) {printf("文件打开失败\n"); exit(0);}for(i=0;i<SIZE;i++)沈阳航院设计用纸9if(fwrite(&std[i],sizeof(struct student),1,fp)!=1) /*将内存中的数据输出到磁盘文件中*/ printf("文件保存失败\n"); }void main() {int j,i; char c; FILE *fp;if((fp=fopen("myfile.dat","ab+"))==NULL) {printf("文件打开失败\n"); exit(0); }for(i=0;i<SIZE;i++)fread(&std[i],sizeof(struct student),1,fp); /*将fp所指向的文件中的数据存储到数组std中*/printf("*************欢迎使用上课随机点名程序**************\n"); printf("******输入a:显示所有同学名情况*********\n"); printf("******输入d: 开始点名*********\n"); printf("******输入s:保存点名*********\n"); printf("******输入b: 点名次数归零*********\n"); printf("******输入q: 退出*********\n"); printf("******************************************\n"); while(1) {c=getchar(); switch(c) {case 'a': for(i=0;i<SIZE;i++) printf("\t\t%s\t\t%s\t\t被点名%d次\n",std[i].num,std[i].name,std[i].time); break;case 's': save();printf("****文件保存成功!退出请输入q****\n");break;case 'b': for(i=0;i<SIZE;i++)沈阳航院设计用纸10std[i].time=0;printf("点名次数归%d\n",std[i].time);break; case 'q': exit(0); case 'd': loop: j=rand()/1000; /*调用rand的函数进行点名*/ if(j==0) { std[0].time++;printf("\t\t%s\t\t%s\n",std[j].num,std[j].name); }else if(j==1) {std[1].time++;printf("\t\t%s\t\t%s\n",std[j].num,std[j].name); }else if(j==2) {std[2].time++;printf("\t\t%s\t\t%s\n",std[j].num,std[j].name); }else if(j==3) {std[3].time++;printf("\t\t%s\t\t%s\n",std[j].num,std[j].name); }else goto loop; break;} }fclose(fp); }。