(完整版)C语言随机点名
- 格式:doc
- 大小:16.51 KB
- 文档页数:9
C#随机点名系统设计报告班级: 软件1202*名:**学号: *********2013 年12 月一、设计描述:此系统为课堂随机点名系统:采用了windows窗体界面设计创建了此系统。
系统功能包括:1、从已设置的学号文本信息文件中随机抽取一个学号。
2、记录某一个学号被点名信息。
3、显示当前被点学号的被点信息,以及所有同学被点信息。
4、每一个同学最多被点三次,当被点次数超过3时,显示警告窗口,提示用户重新点名。
二、系统详细设计——控件选取:1、listBox1:用来存放及显示所有同学被点信息。
2、Button1:就是开始点名按钮,用来控制随机抽数,当点下开始按钮,其会变成停止按钮,按下停止按钮,显示最终被点学号。
3、Button2:就是用来显示当前被点同学的被点情况。
4、Button3:就是重置按钮,用来清除listBox1中的数据信息。
5、Button4:就是显示listBox1中记录的所有同学被点情况。
6、timer1:用来控制抽取过程中数据在空白处闪动。
7、label1:用来显示被点学号。
三、系统详细设计——代码实现:using System;using System.IO;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Collections;using System.Timers;namespace点名系统{public partial class Form1 : Form{bool first_click = true; //初始化int x;int[] c = new int[31];String num;Random r = new Random();ArrayList content = new ArrayList();ArrayList result = new ArrayList();public Form1(){InitializeComponent();timer1.Interval = 10;timer1.Stop();} //初始化 endprivate void textBox1_TextChanged(object sender, EventArgs e){}private void pictureBox1_Click(object sender, EventArgs e){}private void timer1_Tick(object sender, EventArgs e) //timer1:控制随机点名,在已创建的文件中,随机读取相关学号信息{FileStream fs1 = new FileStream(@"D:/2.txt", FileMode.Open);StreamReader sr = new StreamReader(fs1);string str1 = sr.ReadToEnd();sr.Close(); fs1.Close();string[] strQ = str1.Split('\n');Random rdn = new Random();int index = rdn.Next(0, strQ.Length);x = index;num = strQ[index];label1.Text = strQ[index];} //timer endprivate void button1_Click(object sender, EventArgs e) //button1:开始点名按钮,用来控制timer1的运行,以及提示警告{first_click = !first_click;if (!first_click){timer1.Start();button1.Text = "停止";}else{timer1.Stop();button1.Text = "开始";c[x]++;if (c[x] > 3)MessageBox.Show("该同学已被点过 3 次,请重新点名"); //警告提示。
随机点名案例随机点名是一种常见的教学方法,用于随机选择学生回答问题或参与学习活动。
下面是一些关于随机点名的案例:1. 教师在课堂上使用随机点名的方式来鼓励学生积极参与讨论。
每个学生都有机会被点名回答问题或发表意见,这样可以增加学生的参与度和学习兴趣。
2. 随机点名可以帮助教师了解学生的学习情况和掌握程度。
通过随机点名,教师可以知道每个学生的学习进度,及时发现问题并给予帮助。
3. 在小组活动中,可以使用随机点名的方式来确定小组成员。
每个学生都有机会与不同的同学合作,培养合作意识和团队精神。
4. 随机点名可以帮助学生建立自信心。
每个学生都有机会被点名回答问题,即使答错了也不要紧,可以通过讨论和纠正来学习和成长。
5. 随机点名可以激发学生的学习动力。
学生们都希望被点名回答问题,因此会更加认真听讲和准备课前作业,以免被点名时无法回答问题。
6. 随机点名可以促进课堂秩序和纪律。
学生们知道教师会随机点名,因此会保持警觉和专注,不敢懈怠或做其他与学习无关的事情。
7. 随机点名可以增加课堂的互动和活跃度。
学生们可以互相提问或回答问题,促进彼此之间的交流和合作。
8. 随机点名可以帮助教师更好地掌握学生的学习情况和进度。
通过随机点名,教师可以了解每个学生的学习兴趣、学习困难以及需要改进的地方,有针对性地进行教学。
9. 随机点名可以培养学生的自律性和责任感。
每个学生都要随时准备回答问题,这要求他们自觉地学习和预习,提高学习效果。
10. 随机点名可以让学生更好地记忆和掌握知识。
被点名时需要立即回答问题,这要求学生对知识点进行快速回忆和思考,巩固学习成果。
随机点名是一种有效的教学方法,可以提高学生的学习积极性和参与度,帮助教师了解学生的学习情况和进度,促进课堂的互动和活跃。
同时,学生们也可以通过随机点名来培养自律性、责任感和团队合作精神。
C#课堂随机点名系统题目:课堂随机点名系统班级:姓名:学号:指导教师 :朱新峰成绩:2021年12月一、任务描述本次任务要求完成一个课堂随机点名系统。
具体内容为:教师在每次上课时,有时需要叫学生起来答复下列问题,为了公平起见,使每个学生被叫起来答复下列问题的概率相同,故采用 c#来编写一个随机点名系统。
具体要求如下:〔一〕完成一个点名系统界面的设计,具体设计如下:1.安置一个“开始〞按钮,当按下“开始〞按钮时,按钮上面的字显示为“停止〞,并且开始在界面上随机抽取数字。
2.安置一个“被点名单〞按钮,可以查看已经被点过的学生的学号以及被点次数。
3.添加两个标签,分别在上面显示“欢送进入点名系统〞和抽取的随机数字。
4. 添加一个listbox用来存放已经被点过的学生的学号以及被点次数。
5.安置一个“去除〞按钮,用来去除 listbox 中的数据。
6.安置一个“保存被点名单〞按钮,保存本次点名的数据。
〔二〕该点名系统能够实现随机点名,并且当一个学生被点次数超过三次时,会提醒老师重新点名。
〔三〕能将本节课点名的情况记录到文件中,有利于老师下课后查看分析学生上课答复下列问题的情况并且有利于老师下节课点其他未被点过的学生。
二、系统概要设计〔一〕模块整体设计图课堂随机点名系统界点保面存设被计名点名单记被抽录点随被三机点次取名后学单提号醒〔二〕模块功能介绍1.窗体类〔1〕窗体事件 Form1()功能:对各变量进行初始化〔2〕开始按钮事件 button1_Click(object sender, EventArgs e)功能:进行随机点名,将点到的学号和被点次数写入 listbox ,并在某学号点到三次后提醒老师〔3〕定时器事件 timer1_Tick(object sender, EventArgs e)功能:产生随机学号并将数据在界面上显示出来〔4〕被点名单按钮事件button2_Click(object sender, EventArgs e)功能:显示和隐藏listbox〔5〕去除按钮事件 button3_Click(object sender, EventArgs e)功能:去除 listbox 中的内容〔6〕保存被点名单事件button4_Click(object sender, EventArgs e)功能:将被点名单写入文件并记录点名时间2.写文件类〔1〕写文件函数 WriteData(string filestr,string st)功能:将点到的学号和被点次数写入文件三、系统详细设计〔一〕数据结构设计1.本系统采用文件保存点到的学号及其被点次数2.定义全局变量 n , num以及名为 count 的数组分别用来存放班级人数,产生的随机学号以及每个学号被点次数〔二〕模块接口设计:1.各类的原型为:public partial class Form1 : Form/*窗体类*/public class FileWrite/* 写文件类 */2. 各事件原型为:public Form1()/* 窗体事件 */private void button1_Click(object sender, EventArgs e)/*“开始〞按钮事件*/ timer1_Tick(object sender, EventArgs e)/* 定时器事件 */private void button2_Click(object sender, EventArgs e)/*“被点名单〞按钮事件*/ private void button3_Click(object sender, EventArgs e)/* “保存被点名单〞按钮事件*/ public static void WriteData(string filestr,string st)/*写文件函数 */四、系统实现1.界面设计2.点名界面3.开始点名4.显示被点名单5.文档数据6.提醒界面五、系统调试在做这个系统的时候,由于对 C#的语法不太了解,所以即使是这样一个简单的系统,还是花了我一个星期的时间,并且还有许多不完善的地方。
设计介绍:本系统能从数据库中调取学生信息数据库,对采取两种方式进行点名。
1、能根据输入的随机数产生一定数量的随机学生,然后进行一一点名。
2、能根据输入的扫描频率进行动态的扫描,但按下暂停按钮时会停在某个学生的信息上,此时对该学生进行点名。
当点名后,没有学生答应时,可按加入缺课记录按钮。
当系统提示添加成功,则添加成功。
代码解释:using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace dianming{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){this.dataGridView1.DataSource = getDataTable("dbo.stuinfo");changewith();}public void changewith()//当选择不同学生时,左侧的信息栏相应的变化。
{this.txtnum.Text = this.dataGridView1[0,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.textname.Text = this.dataGridView1[1,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtage.Text = this.dataGridView1[2,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtgrade.Text = this.dataGridView1[3,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtsex.Text = this.dataGridView1[4,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtmajor.Text = this.dataGridView1[5,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtcount.Text = this.dataGridView1[6,dataGridView1.CurrentCell.RowIndex].Value.ToString();this.txtphone.Text = this.dataGridView1[7,dataGridView1.CurrentCell.RowIndex].Value.ToString();}public static string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=点名数据库;Integrated Security=True";//数据库连接字符串public static bool ExecuteSQL(string sql)//用于执行传入的sql语句返回true则表示成功{SqlConnection con = new SqlConnection(connectionString);SqlCommand cmd = new SqlCommand(sql, con);try{con.Open();cmd.ExecuteNonQuery();return true;}catch{return false;}finally{con.Close();con.Dispose();cmd.Dispose();}}public static DataSet GetDataSet(string sql)//获取数据集{DataSet ds = new DataSet();SqlConnection con = new SqlConnection(connectionString);SqlDataAdapter da = new SqlDataAdapter(sql, con);try{con.Open();da.Fill(ds);}catch (Exception ex){throw new Exception(ex.ToString());}finally{con.Close();con.Dispose();da.Dispose();}return ds;}public static DataTable getDataTable(string str)//返回table类型的对象{string sql = "select * from " + str;DataSet ds = new DataSet();SqlConnection con = new SqlConnection(connectionString);SqlDataAdapter da = new SqlDataAdapter(sql, con);try{con.Open();da.Fill(ds);}catch (Exception ex){throw new Exception(ex.ToString());}finally{con.Close();con.Dispose();da.Dispose();}return ds.Tables[0];}private void button3_Click(object sender, EventArgs e)//开始按钮触发的事件{if(this.timer .Text!=null&&this.timer .Text !="")this.timer1.Interval =Convert .ToInt32(this.timer.Text);this.timer1.Enabled = true;}private void button4_Click(object sender, EventArgs e)//暂停按钮触发的事件{this.timer1.Enabled = false;}static public int[] GetNoRepeatRandNumber(int len)//获取len个随机数,以数组的形式//返回{int[] no = new int[len];for (int i = 0; i < len; i++){no[i] = i;}Random rd = new Random();for (int i = len - 1; i >= 0; i--){int temp = rd.Next(i + 1);int t = no[temp];no[temp] = no[i];no[i] = t;}return no;}private void btnOK_Click(object sender, EventArgs e)//确定按钮触发的事件{Random rand = new Random();int[] a = GetNoRepeatRandNumber(50);string[] num=new string[Convert.ToInt32(ranknum.Text.Trim ())];for (int i = 0; i < Convert.ToInt32(ranknum.Text.Trim()); i++){if (a[i] / 10 != 0)num[i] = string.Format("0890740{0}", a[i]);else num[i] = string.Format("08907400{0}", a[i]);}string numrank = "";foreach (string s in num){numrank += s+",";}numrank += "089074000";string sql = @"select * from dbo.stuinfo where 学号in("+numrank+")";DataSet ds = GetDataSet(sql);this.dataGridView1.DataSource = ds.Tables[0];}private void timer1_Tick(object sender, EventArgs e)//timer控件触发的循环事件{int a = dataGridView1.RowCount-1;if (dataGridView1.CurrentCell.RowIndex < 0)dataGridView1.CurrentCell = dataGridView1[0, 0];int f = dataGridView1.CurrentCell.RowIndex;dataGridView1.CurrentCell = dataGridView1[0, (f)%a+1];changewith();}private void btnadd_Click(object sender, EventArgs e)//添加缺勤记录按钮触发的事件{string name = this.textname.Text.Trim();string num = this.txtnum.Text.Trim();string count = this.txtcount.Text.Trim();int newcount = Convert.ToInt32(count) + 1;string sql = @"insert into dbo.queqinxinxivalues('"+num+"', '"+name +"', "+"getdate())";if (ExecuteSQL(sql)){MessageBox.Show("添加缺勤记录成功!", "提示");}else{MessageBox.Show("添加缺勤记录失败!", "提示",MessageBoxButtons .OK,MessageBoxIcon.Error);}}private search Search;//缺勤窗体,此处不列出其代码了。
c语言随机函数的调用和使用方法C语言随机函数的调用和使用方法随机函数在编程中被广泛使用,特别是在游戏开发、模拟实验和密码学等领域。
C语言提供了一些内置的随机函数,可以生成伪随机数。
在本文中,我将一步一步地介绍如何调用和使用C语言中的随机函数。
第一步:包含头文件在C语言中,我们需要包含头文件<cstdlib>来使用随机函数。
这个头文件中包含了生成伪随机数的函数原型和相关定义。
我们可以使用下面的代码来包含这个头文件:c#include <cstdlib>第二步:生成随机种子在使用随机函数之前,我们需要生成一个随机种子。
种子是一个起始点,用于生成随机数列。
如果我们不提供随机种子,随机函数将以相同的顺序生成相同的随机数序列。
我们可以使用`time`函数以当前时间作为种子来生成随机种子,如下所示:csrand(time(0));上述代码中,`srand`函数用于设置一个新的随机种子,而`time(0)`函数获取当前的系统时间。
通过使用当前时间作为种子,我们可以确保每次程序运行时都能生成不同的随机数序列。
第三步:调用随机函数接下来,我们可以调用`rand`函数来生成一个伪随机数。
`rand`函数返回一个介于0和`RAND_MAX`之间的整数值。
`RAND_MAX`是一个常量,表示生成的随机数的最大值。
我们可以使用下面的代码来调用`rand`函数,并将生成的随机数存储在一个变量中:cint randomNumber = rand();以上代码将生成一个随机数并将其存储在名为`randomNumber`的整型变量中。
第四步:控制生成的随机数范围有时候我们希望生成的随机数在一个特定的范围内。
为了做到这一点,我们需要使用一些算术操作来控制生成的随机数的范围。
一个常见的方法是使用取余操作符`%`,通过对生成的随机数取余来限制其范围。
例如,如果我们想要生成1到10之间的随机数,我们可以使用下面的代码:cint randomNumber = rand() % 10 + 1;上述代码中,`rand() % 10`将生成一个介于0和9之间的随机数,然后我们通过加1操作将其范围移动到1到10之间。
c随机点名系统课程设计一、课程目标知识目标:1. 让学生理解随机点名系统的基本原理和编程思路;2. 使学生掌握运用编程语言(如Python)实现随机点名功能;3. 帮助学生了解计算机在实际应用中的优势,如提高工作效率、减少人工干预。
技能目标:1. 培养学生运用编程语言解决实际问题的能力;2. 提高学生在团队协作中沟通、分工与协作的能力;3. 培养学生运用信息技术工具进行数据处理和问题求解的能力。
情感态度价值观目标:1. 激发学生对计算机编程的兴趣和热情,提高其主动学习的积极性;2. 培养学生面对问题时勇于尝试、不断探索的良好品质;3. 增强学生的团队意识,使其认识到团队协作的重要性。
课程性质:本课程为信息技术学科,以实践操作为主,结合理论讲解,旨在培养学生的编程兴趣和实际操作能力。
学生特点:六年级学生,具有一定的信息技术基础,对新鲜事物充满好奇心,具备一定的逻辑思维能力。
教学要求:结合学生特点,注重实践操作,以任务驱动法引导学生主动探究,强调团队协作,提高学生的编程能力和信息素养。
在教学过程中,将课程目标分解为具体的学习成果,以便于后续的教学设计和评估。
二、教学内容1. 理论知识:- 计算机编程基本概念;- Python编程语言简介;- 随机数及其在编程中的应用。
2. 实践操作:- Python编程环境的搭建与使用;- 编写随机点名系统的代码实现;- 调试与优化随机点名系统。
3. 教学大纲:- 第一课时:计算机编程基本概念,Python编程语言简介;- 第二课时:随机数原理及在编程中的应用;- 第三课时:搭建Python编程环境,编写随机点名系统代码;- 第四课时:调试与优化随机点名系统,总结与拓展。
4. 教材关联:- 《信息技术》六年级下册,第三章“编程入门”;- 教材中涉及Python编程基础、条件语句、循环语句等内容,为随机点名系统的实现提供支持。
5. 教学内容安排与进度:- 每课时45分钟,共计4课时;- 前两课时注重理论知识讲解,后两课时侧重实践操作;- 每课时结束后安排课后作业,巩固所学内容。
1引言 ................................................................................................................ 错误!未定义书签。
1.1编写目的.............................................................................................. 错误!未定义书签。
1.2背景...................................................................................................... 错误!未定义书签。
1.3定义 (3)1.4参考资料 (3)2总体设计 (3)2.1需求规定 (3)2.2运行环境 (4)2.3基本设计概念和处理流程 (4)2.4结构 (5)2.5功能需求与程序的关系 (6)2.6人工处理过程 (6)2.7尚未问决的问题 (7)3接口设计 (7)3.1用户接口 (7)3.2外部接口 (7)3.3内部接口 (7)4运行设计 (7)4.1运行模块组合 (7)4.2运行控制 (8)4.3运行时间 (8)5系统数据结构设计 (8)5.1逻辑结构设计要点 (8)5.2物理结构设计要点 (9)5.3数据结构与程序的关系 (9)6系统出错处理设计 (9)6.1出错信息 (9)6.2补救措施 (10)6.3系统维护设计 (10)概要设计说明书1引言1.1编写目的在高等院校、职中专、大专学校里,学生考勤是一项日常必要性的工作,学校的各院系需要了解和掌握各班级的学生出勤情况,以加强对学生的教育和管理。
因而学生考勤是学校一项非常重要的工作。
传统的学生考勤以及平时在课堂上的表现,主要是依靠老师在课堂上点名或学生会抽查点名制度,还有上课提问等。
(完整版)C语言随机点名#include#include#include#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< p="">}bool IsExist(int id){for(int i=0;i<stu_num_max;i++)< p="">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++){< p="">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++)< p="">{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#include#include#include#includestruct 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++)< p="">{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#include#include#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++)< p="">{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++)< p="">{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){intstuID[4]={2013011001,2013011002,2013011003,2013011004};//WriteData();srand((unsigned)time(NULL));//随机种子TeacherDM(stuID[rand()%(3-0+1)+0]);}#include #include #include #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++)< p="">沈阳航院设计用纸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++)< p="">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<="" p="" printf("\t\t%s\t\t%s\t\t被点名%d 次\n",std[i].num,std[i].name,std[i].time);="">case 's': save();printf("****文件保存成功!退出请输入q****\n");break;case 'b': for(i=0;i<size;i++)< p="">沈阳航院设计用纸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); }</size;i++)<></size;i++)<></size;i++)<></stu_num;j++)<></stu_num;i++)<></i;k++)<></stu_num;j++)<></stu_num;i++){<></stu_num_max;i++)<> <>。
c语⾔⼤作业⽼师点名,C语⾔随机点名(最新整理)《C语⾔随机点名(最新整理)》由会员分享,可在线阅读,更多相关《C语⾔随机点名(最新整理)(10页珍藏版)》请在⼈⼈⽂库⽹上搜索。
1、include #include #include #define STU_NUM_MAX 64 / 假设最多有 64 个学⽣struct Studentchar name10; intstuID;stuSTU_NUM_MAX;int existSTU_NUM_MAX; / ⽤以保存被点过名static int index=0; / 记住点名的次数void Iitialize()for(inti=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。
#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); }。