QQ网名大全

c语言 随机点名

#include<stdio.h>
#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;
}

上面的代码,我留下几个细节问题留给你自己学着解决,都是很简单的:

  1. 上面的代码,我没有对重复的学号作判断。

  2. 上面的代码,我没有把点名存放到另一个文件,而是用数组替代(可以实现的也很简单)。我怕写得代码太多,百度限制提交。

  3. 上面的代码,是测试数据,stu.dat目标文件并没有64个学生,我只写入了12条数据。

  4. 上面的代码,我没有对数据数量(最多64条)作判断。

佚名
2024-05-23 23:05:04
最佳回答
类似问题(10)
  • 佚名
    2024-05-23 17:55:46

    你好,怎样用c语言输出一个1到100的随机数

    源程序如下:#include "pch.h"#include <iostream>#include <time.h>using namespace ...

  • 佚名
    2024-05-23 19:18:06

    c语言函数

    #include<stdio.h>#define N 10void showarr(int *a,int n) { int i; for ( i=0;i&...

  • 佚名
    2024-05-23 09:45:10

    c语言函数

    5次fun(9)=9-fun(7);fun(7)=7-fun(5);fun(5)=5-fun(3);fun(3)=3-fun(1);fun(1)=3;结果为7吧...

  • 佚名
    2024-05-23 22:25:38

    C语言函数

    if (score[i][j]>=0 && score[i][j]<60) t[0]++你的 运行 就判断到这 拿她 后面的 也是大于60...

  • 佚名
    2024-05-23 15:47:39

    C语言函数

    B B A D B B A D A A

  • 佚名
    2024-05-23 08:30:10

    C语言 基本

    C语言的基本词法由三部分组成:符号集、关键字、保留字。 符号集就是一门语言中允许出现的字符的集合,C语言的符号集就是ASCⅡ码表中的一些字符,在键盘上不能直接...

  • 佚名
    2024-05-23 04:17:07

    易语言写随机重命名怎么写?

    置随机数种子()计次循环(取数组成员数(文件路径数组),n)新名字=取数据摘要(到字节集(到文本( 取随机数(1,1000))+到文本(n))))文件更名(文件...

  • 佚名
    2024-05-23 08:00:00

    C#编写随机点名系统

    使用random类随机数字,在使用if根据随机的数字显示名字。例如:有三个人(随机到1就显示第一个人,而就显示第二个人...)(初学者,个人想法)

  • 佚名
    2024-05-23 08:00:00

    C语言如何srand和rand函数产生10个1-100内的随机数

    需要准备的材料分别有:电脑、C语言编译器。1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。2、在test.cpp文件中,输入C语言...

  • 佚名
    2024-05-23 08:00:00

    计算机的c语言是什么意思??

    C语言就是计算机识别的语言。最原始的就是用0和1编写的计算机语言,叫做机器语言,编写特别繁琐,后来有了汇编语言,采用一些助记符表示机器语言中的机器指令,也是比较...