#include <stdlib.h>
#include <time.h>
#define STU_NUM_MAX 4
struct StudentInfo // 学生信息结构
{
char name[15]
int stu_id
}stu[STU_NUM_MAX]
void WriteData() //写入学生信息
{
FILE *fp
int stu_num=4
for (int 孙亩卜i=0i<stu_numi++)
{
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=0j<stu_numj++)
{
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 <stdio.h>#include <stdlib.h>
#include<string.h>
#include <time.h>
func1(char stubuff[100][100],int stucnt)
func2(char stubuff[100][100],int stucnt)
struct Student
{
int stdno
char name[20]
char sex[20]
}
int main()
{
FILE *fp
int stucnt=0,x
char line[100]="\0",stubuff[100][100]
char filename[20]
struct Student *st
printf("\t\t\t欢迎使用课堂随机点名程序!\n\n")
printf("点名前请输入您要点名的班级:")
scanf("%s",filename)
fp=fopen(filename,"r")
while (fgets(line,99,fp)!=NULL)
{
strcpy(stubuff[stucnt++],line)
}
printf("该班一共有%d个学生\n",stucnt)
srand((unsigned)time(NULL))
printf("请选择:\n")
printf("1:开始点名\n2:查看全班具体情况\n3:结束程序\n")
printf("请腊棚输入您的选择:")
scanf("%d",&x)
switch(x)
{
case 1:
func1(stubuff[100][100],stucnt)break
case 2:
func2(stubuff[100][100],stucnt)break
case 3:
printf("\t\t\t谢谢使用!\n")
}
fclose(fp)
return 0
}
func1(char stubuff[100][100],int stucnt)
{
int i,j,chou,tmp,sel[100]={0},flag
do
{
printf("请输入这次要点名的人数:")
scanf("%d",&chou)
if (chou>stucnt)
{
printf("仔游这个班没有这么多学生\n")
}
} while(chou>stucnt ||chou<0)
for (i=0i<choui++)
{
flag=0
tmp=rand()%stucnt+1
for (j=0j<ij++)
{
if (sel[j]==tmp)
{
flag=1
break
}
}
if (flag==1)
{
i--
}else
{
sel[i]=tmp
}
}
printf("点名情况如下:\n")
printf("学号\t\t姓念局销名\t性别\n")
for (i=0i<choui++)
{
fflush(stdin)
getchar()
printf("%s",stubuff[sel[i]-1])
}
}
func2(char stubuff[100][100],int stucnt)
{
int i
for (i=0i<stucnti++)
{
fflush(stdin)
getchar()
printf("%s",stubuff[i])
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)