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;
}
上面的代码,我留下几个细节问题留给你自己学着解决,都是很简单的:
上面的代码,我没有对重复的学号作判断。
上面的代码,我没有把点名存放到另一个文件,而是用数组替代(可以实现的也很简单)。我怕写得代码太多,百度限制提交。
上面的代码,是测试数据,stu.dat目标文件并没有64个学生,我只写入了12条数据。
上面的代码,我没有对数据数量(最多64条)作判断。
-
佚名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编写的计算机语言,叫做机器语言,编写特别繁琐,后来有了汇编语言,采用一些助记符表示机器语言中的机器指令,也是比较...
风水
起名
网名
- 1 带凉生的网名
- 2 简单幸福情侣网名
- 3 乔丹qq网名
- 4 可爱带点拽的网名
- 5 用宇霖起个网名怎么起
- 6 代表坏的两个字的网名
- 7 关于青字六字的网名
- 8 藏语藏语网名
- 9 智慧团建如何修改昵称
- 10 一男生对女生说了一个昵称
说说
- 1 个性签名女q
- 2 短信后面带个性签名
- 3 被闺蜜出卖的个性签名
- 4 qq个性签名 流星
- 5 找个qq签名生活
- 6 qq签名江辰
- 7 闺蜜太自私心寒的说说
- 8 睡得不想起床说说
- 9 去量尺的说说
- 10 一个人没有朋友的说说