c语言大作业老师点名,C语言随机点名(最新整理)

《C语言随机点名(最新整理)》由会员分享,可在线阅读,更多相关《C语言随机点名(最新整理)(10页珍藏版)》请在人人文库网上搜索。

1、include #include #include #define STU_NUM_MAX 64 / 假设最多有 64 个学生struct Studentchar name10; int stuID;stuSTU_NUM_MAX;int existSTU_NUM_MAX; / 用以保存被点过名static int index=0; / 记住点名的次数void Iitialize()for(int i=0;i #include #include #include #include struct studentinfost100;char sNo5; char sxueNo14; char sna。

2、me20;int main()int i=0,j=0,k,flag,RN,*a; FILE *fp;char ch,filename20=0,line100=0; printf(Please input file name:); fflush(stdin);gets(filename); fp=fopen(filename,r); printf(名单如下:n);while (fgets(line,sizeof(line)-1,fp)if (line0!=n & line0!= )name);me);sscanf(line,%s %s %sn,sti.sNo,sti.sxueNo,sti.spr。

3、intf(%sn%sn%sn,sti.sNo,sti.sxueNo,sti.sna 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 #include #include #define STU_NUM_MAX 4struct StudentInfo / 学生信息结构char name15; int stu_id。

4、;stuSTU_NUM_MAX;void WriteData() /写入学生信息FILE *fp;int stu_num=4;for (int i=0;i #include #include #define SIZE 4 struct student char num20;char name10;int time; stdSIZE; void save() int i; FILE *fp;if(fp=fopen(myfile.dat,rb+)=NULL)printf(文件打开失败n);exit(0); for(i=0;iSIZE;i+)沈 阳 航 院 设 计 用 纸9if(fwrite(&st。

5、di,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;iSIZE;i+)fread(&stdi,sizeof(struct student),1,fp);/*将 fp 所指向的文件中的数据存储到数组 std 中*/printf(*欢迎使用上课随机点名程序*n); printf(*输入a:显示所有同学名情。

6、况*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;iSIZE;i+)printf(tt%stt%stt 被点名%d 次n,stdi.num,stdi. name,stdi.time);break;case s: save();printf(*文件保存成功!退出请输入 q*n); break;case b: for(i=0;iSIZE;i+)沈 阳 航。

7、 院 设 计 用 纸10stdi.time=0;printf(点名次数归%dn,stdi.time);break;case q:exit(0);case d: loop: j=rand()/1000;/*调用 rand 的函数进行点名*/if(j=0)std0.time+; printf(tt%stt%sn,stdj.num,stdj.name); else if(j=1)std1.time+; printf(tt%stt%sn,stdj.num,stdj.name); else if(j=2)std2.time+; printf(tt%stt%sn,stdj.num,stdj.name); 。

8、else if(j=3)std3.time+; printf(tt%stt%sn,stdj.num,stdj.name);else goto loop;break; fclose(fp); “”“”At the end, Xiao Bian gives you a passage. Minand once said, people who learn to learn are very happy people. In every wonderful life, learning is an eternal theme. As a professional clerical and teach。

9、ing position, I understand the importance of continuous learning, life is diligent, nothing can be gained, only continuous learning can achieve better self. Only by constantly learning and mastering the latest relevant knowledge, can employees from all walks of life keep up with the pace of enterprise development and innovate to meet the needs of the market. This document is also edited by my studio professionals, there may be errors in the document, if there are errors, please correct, thank you。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我可以为您提供一些C语言代码作为参考。请注意,这只是一个示例,您可以根据自己的需求进行更改。 ``` #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int i, n, randNum; char names[10][100] = {"张三", "李四", "王五", "赵六", "孙七", "周八", "吴九", "郑十", "冯十一", "陈十二"}; int len = sizeof(names) / sizeof(names[0]); srand(time(NULL)); for (i = 0; i < len; i++) { randNum = rand() % len; printf("%d: %s\n", i + 1, names[randNum]); } return 0; } ``` 这段代码实现了从数组`names`中随机选取名字的功能。首先,它定义了一个字符数组,其中存储了10个名字。然后,使用`srand()`和`rand()`函数生成随机数。最后,通过循环从`names`数组中随机选择名字并打印出来。 ### 回答2: 你可以使用C语言编写一个随机点名的小程序,实现随机选择学生的功能。首先,你需要定义一个学生数组来存储所有的学生名字。然后,通过使用rand()函数来生成一个随机数,将其与学生数组的索引对应起来,这样就可以随机选择一个学生了。 以下是一个简单的示例代码,供你参考: ```c #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { char* students[] = {"小明", "小红", "小刚", "小华", "小李"}; // 学生名字数组 int numStudents = sizeof(students) / sizeof(students[0]); // 学生数量 int randomNumber; srand(time(0)); // 初始化随机数种子 // 生成一个随机数,范围为0到numStudents-1之间 randomNumber = rand() % numStudents; printf("随机点名的学生是:%s\n", students[randomNumber]); return 0; } ``` 这个程序首先定义了一个存储学生名字的数组,然后计算出学生数量。接下来,通过使用`rand()`函数生成一个0到numStudents-1之间的随机数。最后,将随机数作为索引,从学生数组中选择一个学生名字,并打印输出。 请注意,为了保证每次运行得到不同的随机数,我们在程序开始时使用`srand(time(0));`来初始化随机数的种子。这样,每次运行程序时都会得到不同的随机数。 希望以上的解答能满足你的需求,如果有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值