c語言點名程序
1. c語言 隨機點名
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#defineSTU_NUM_MAX64//假設最多有64個學生
structStudent
{
charname[10];
intstuID;
}stu[STU_NUM_MAX];
intexist[STU_NUM_MAX];//用以保存被點過名
staticintindex=0;//記住點名的次數
voidIitialize(){
for(inti=0;i<STU_NUM_MAX;i++)exist[i]=0;
}
boolIsExist(intid){
for(inti=0;i<STU_NUM_MAX;i++)
if(exist[i]==id)returntrue;//已存在
returnfalse;//不存在
}
voidAdd()//添加數據
{
FILE*fp;
intstu_num;
printf(" ?:");
scanf("%d",&stu_num);
for(inti=0;i<stu_num;i++){
printf(" ");
printf(" PleaseinputstudentID:");
scanf("%d",&stu[i].stuID);
printf(" Pleaseinputstudentname:");
scanf("%s",stu[i].name);
fflush(stdin);
}
if((fp=fopen("stu.dat","ab"))==NULL) {
printf("Can'topenfile ");
exit(1);
}
for(intj=0;j<stu_num;j++)
{
if(fwrite(&stu[j],sizeof(structStudent),1,fp)!=1)
printf("Errorwritingfile. ");
}
fclose(fp);
}
voidrollcall()//隨機點名
{
FILE*fp;
if((fp=fopen("stu.dat","rb"))==NULL)
{
printf("Can'topenfile. ");
exit(1);
}
srand((unsigned)time(NULL));
inti=0;
intrandID=rand()%(64-1+1)+1;//1~64
printf(" 隨機點到的學號為:%d %s %s ",randID,"StuID","StuName");
do
{
fseek(fp,i*sizeof(structStudent),SEEK_SET);
if(fread(&stu[i],sizeof(structStudent),1,fp))
{
if(stu[i].stuID==randID&&!IsExist(randID)){
printf(" %4d %5s ",stu[i].stuID,stu[i].name);
exist[index++]=randID;
break;}
}
i++;
}while(!feof(fp));
fclose(fp);
}
intmain()
{
intselect=0;
charanswer='y';
Iitialize();
do
{
printf("1.添加數據2.隨機點名3.退出 請選擇:");
fflush(stdin);
scanf("%d",&select);
switch(select)
{
case1:
Add();
break;
case2:
rollcall();
break;
case3:
return0;
}
fflush(stdin);
printf("Youwanttocontinue?:");
scanf("%c",&answer);
}while(answer=='y'||answer=='Y');
return0;
}
上面的代碼,我留下幾個細節問題留給你自己學著解決,都是很簡單的:
上面的代碼,我沒有對重復的學號作判斷。
上面的代碼,我沒有把點名存放到另一個文件,而是用數組替代(可以實現的也很簡單)。我怕寫得代碼太多,網路限制提交。
上面的代碼,是測試數據,stu.dat目標文件並沒有64個學生,我只寫入了12條數據。
上面的代碼,我沒有對數據數量(最多64條)作判斷。
2. 怎麼用C語言編寫隨機點名
使用系統函數 int rand(void)產生偽隨機數,先用void srand(unsigned int seed)函數設置隨機種子,這樣就會產生真正的隨機函數
3. c語言 課堂隨機點名程序
10分就想要這個程序呀!
這個不好做,你先等等吧
4. c語言 課堂隨機點名程序 跪求大神幫我修改正確!
#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=0;i<chou;i++)
{
flag=0;
tmp=rand()%stucnt+1;
for (j=0;j<i;j++)
{
if (sel[j]==tmp)
{
flag=1;
break;
}
}
if (flag==1)
{
i--;
}else
{
sel[i]=tmp;
}
}
printf("點名情況如下:\n");
printf("學號\t\t姓名\t性別\n");
for (i=0;i<chou;i++)
{
fflush(stdin);
getchar();
printf("%s",stubuff[sel[i]-1]);
}
}
func2(char stubuff[100][100],int stucnt)
{
int i;
for (i=0;i<stucnt;i++)
{
fflush(stdin);
getchar();
printf("%s",stubuff[i]);
}
}
5. 求c語言班級點名程序,高手們來幫幫忙啊
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Student
{
char name[20];
bool IsRead;
}Student;
int main()
{
int i;
int select;
int mark=1;
//下面中你還可以加入一些學生,記得初始化的IsRead都是false
Student student[]=
{
{"張三",false},
{"李四",false},
{"王五",false},
{"劉六",false},
{"趙七",false}
};
printf("歡迎進入點門系統,以下是全班的花名冊:\n");
for (i=0;i<sizeof(student)/sizeof(Student);i++)
printf("%s ",student[i].name);
printf("\n");
do
{
printf("請選擇:\n1.點名\n2.顯示沒有點到的學生名字\n3.退出點名系統\n");
scanf("%d",&select);
system("cls");//如果是Linux系統,把cls改成clear。如果是其他系統請把這行注釋掉,清頻
if(1==select)
{
mark = 1;
while (1)
{
for(i=0;i<sizeof(student)/sizeof(Student);i++)
mark=mark*(student[i].IsRead? 1:0);
if(mark)
{
printf("你已經把全班點了個遍\n");
break;
}
srand(time(NULL));
i = rand()%(sizeof(student)/sizeof(Student));
if(!student[i].IsRead)
{
printf("點名:%s\n",student[i].name);
student[i].IsRead = true;
break;
}
}
}
else if (2==select)
{
for(i=0;i<sizeof(student)/sizeof(Student);i++)
{
if(!student[i].IsRead)
printf("%s ",student[i].name);
}
printf("\n");
}
else if(3==select)
{
printf("謝謝使用\n");
return 0;
}
}while(true);
return 0;
}
這可是我一個字一個字的打出來的,專門為你寫的!~
6. 用C語言編寫一個隨機點名程序
例:
#include<stdio.h>/*standardinput&output*/
#include<stdlib.h>/*standardlibary*/
#include<string.h>/*string*/
#include<conio.h>/*ConsoleInput/Output*/
#include<time.h>
structstudentinfo/*學生信息的結構體*/
{
charsNo[5];/*學生編號*/
charsxueNo[14];/*學號*/
charsname[20];/*學生的姓名*/
}st[100];
charhash[100]={0};/*鏈表的數組*/
intmain()
{
inti=0,j=0,flag=0,RN,*a;
FILE*fp;
charch,filename[20]={0},line[100]={0};
printf("Pleaseinputfilename:");
//fflush(stdin);/*用來清空輸入緩存,以便不影響後面輸入的東西*/
gets(filename);/*鍵盤輸入文件名*/
fp=fopen(filename,"r");/*openreadonly*/
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].sname);/*列印出來*/
i++;/*統計人數*/
}
}
/*設置隨機數種子*/
srand((unsigned)time(NULL));
/*sizeof(類型符)是計算類型所佔位元組數,sizeof(int)是int所佔位元組數,再乘以i,得到i個int型數據的總位元組數。malloc函數用於動態開辟一塊內存空間,參數為開辟的內存空間位元組數,返回開辟的內存空間的首地址指針。*/
a=(int*)malloc(sizeof(int)*i);
memset(a,-1,sizeof(a));/*將已開辟內存空間a的第4個位元組設置為-1*/
printf("按空格鍵點名,其他鍵退出:");
fflush(stdin);
while((ch=getch())=='')
/*while(!(ch=getch())==NULL)*/
{
if(flag==i)/*如果flag等於總人數*/
{
printf("%s\n","點名結束");
break;
}
RN=rand()%i;/*產生一個隨機數*/
while(hash[RN]==1)/*判斷有沒有完成某個一個學生點名*/
RN=rand()%i;/*產生隨機數*/
flag++;/*計數*/
printf("\n~~~~~\n%s\n%s\n%s\n------------\n",st[RN].sNo,st[RN].sxueNo,st[RN].sname);/*輸出學生的信息*/
hash[RN]=1;
}
}
(6)c語言點名程序擴展閱讀:
printf函數使用注意事項
1、域寬
%d:按整型數據的實際長度輸出。
如果想輸出指定寬度可以指定域寬,%md--&gt;m域寬,列印出來以後,在控制台上,顯示m位;
如果我們要列印的數的位數如果超過我們設定m則原樣輸出;
如果我們要列印的數的位數如果小於我們設定的位數,則補空白,具體如下:
如果m為正數,則左對齊(左側補空白);
如果m為負數,則右對齊(右側補空白)。
2、轉義字元
如果想輸出字元"%",則應該在「格式控制」字元串中用連續兩個%表示。
如:printf("%f%%",1.0/3);輸出結果:0.333333%。
7. 隨機點名程序設計 C語言編程
設置一個足夠大的隨機池,給每一個學生分配相同的空間,然後利用隨機數來選取被點名的學生,同時對該學生所分配的空間和其他學生的空間進行縮減或增加。然後執行下一輪。
大致思路就是這樣,希望能夠幫到你哦~
8. 跪求c語言上課隨機點名程序設計
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#defineSTU_NUM_MAX4
structStudentInfo//學生信息結構
{
charname[15];
intstu_id;
}stu[STU_NUM_MAX];
voidWriteData()//寫入學生信息
{
FILE*fp;
intstu_num=4;
for(inti=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'topenfile ");
exit(1);
}
for(intj=0;j<stu_num;j++)
{
if(fwrite(&stu[j],sizeof(structStudentInfo),1,fp)!=1)
printf("Errorwritingfile. ");
}
fclose(fp);
}
voidTeacherDM(intstuID)//教師點名
{
FILE*fp;
boolfind_mark=false;
printf(" %s %s ","學號","姓名");
if((fp=fopen("myfile.dat","rb"))==NULL)
{
printf("Can'topenfile ");
exit(1);
}
inti=0;
do
{
fseek(fp,i*sizeof(structStudentInfo),SEEK_SET);
fread(&stu[i],sizeof(structStudentInfo),1,fp);
if(stu[i].stu_id==stuID)
{
printf(" %4d %s ",stu[i].stu_id,stu[i].name);
printf(" 請【%s】同學回答某某問題. ",stu[i].name);
find_mark=true;
break;
}
i++;
}while(!feof(fp));
if(!find_mark)printf(" 未能找到學生號為:%d的記錄! ",stuID);
fclose(fp);
}
voidmain(void)
{
intstuID[4]={2013011001,2013011002,2013011003,2013011004};
//WriteData();
srand((unsigned)time(NULL));//隨機種子
TeacherDM(stuID[rand()%(3-0+1)+0]);
}
運行效果截圖:
另外多說一句,你所說的公正性,是不是指被點名過的同學不會再次被隨機點名到。如果是這個意思,那麼你可以通過數組來設置它,即把點名過的同學的學號或姓名保存到一維數組里,隨機判斷時只需循環檢查下該同學是否已被點名過。這里就留給你做了。
9. 用c語言編寫一個自動點名系統,具體要求如下一、具體題目: 設計一個供教師上課使用的
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
//---------------data structure----------------
struct student{
char id[30];
char name[30];
int called_times;
};
struct node{
struct node *next;
struct student student;
};
//------------end of data structure----------------
//------------global data-----------------------
static struct node *list_entry;
static int num_called, num_uncalled;
//------------end of global data-------------------
//------------list functions----------------------
static struct node *newnode(struct student *student){
struct node *node;
if((node = (struct node *)malloc(sizeof(struct node))) == NULL)
exit(-1);
memcpy(&node->student, student, sizeof(struct student));
return node;
}
static void append(struct node *node){
node->next = list_entry;
list_entry = node;
}
//------------end of list functions--------------
//------------file operations------------------
static void read_data(FILE *file){
rewind(file);
while(1){
struct student buf;
struct node *node;
int res = fread(&buf, sizeof(struct student), 1, file);
if(res != 1){
break;
}else{
if((node = newnode(&buf)) == NULL)
exit(-1);
append(node);
if(buf.called_times == 1)
++num_called;
else
++num_uncalled;
}
}
}
static void save_data(FILE *file){
rewind(file);
struct node *node;
int cnt = 0;
for(node = list_entry; node;)
if(fwrite(&node->student, sizeof(struct student), 1, file) != 1)
break;
else{
list_entry = node;
node = node->next;
free(list_entry);
++cnt;
}
printf("%d students saved!\n", cnt);
}
//------------end of file operations------------
//------------cmd functions---------------------
static void show_menu(void){
printf("\n\t-------welcome to rolling system---------\
\n\t you can press the CAPITAL charactor or item number to operate.\
\n\t other charactor are not recognized.\
\n\t1.show_(A)ll\
\n\t2.show_(C)alled\
\n\t3.show_(U)ncaled\
\n\t4.add_(I)tem\
\n\t5.(D)el_item\
\n\t6.call_a_(N)ame\
\n\t7.(Q)uit\
\n\t--------------------------------------\
\n\tenter your choice:");
}
static void show_title(void){
printf("%-30s%-30s\n", "ID", "NAME");
}
static void show_item(struct student *student){
printf("%-30s%-30s\n", \
student->id, \
student->name \
);
}
static void show_all(void){
struct node *node;
show_title();
for(node = list_entry; node; node = node->next)
show_item(&node->student);
}
static void show_called(void){
struct node *node;
show_title();
for(node = list_entry; node; node = node->next)
if(node->student.called_times == 1)
show_item(&node->student);
}
static void show_uncalled(void){
struct node *node;
show_title();
for(node = list_entry; node; node = node->next)
if(node->student.called_times == 0)
show_item(&node->student);
}
static void refresh(void){
struct node *node;
num_uncalled += num_called;
num_called = 0;
for(node = list_entry; node; node = node->next)
node->student.called_times = 0;
}
static void call_a_name(void){
struct node *node;
int index;
srand(time(0));
if(num_uncalled == 0)
refresh(); // all called_times reset to 0
show_title();
index = rand() % num_uncalled;
for(node = list_entry; node; node = node->next)
if(node->student.called_times == 0){
if(index-- == 0)
break;
}
show_item(&node->student);
node->student.called_times = 1;
++num_called;
--num_uncalled;
}
void add_item(void){
struct node *node;
struct student student;
student.called_times = 0;
printf("pls enter new item as follows\n");
show_title();
scanf("%s %s",\
student.id,
student.name
);
node = newnode(&student);
append(node);
++num_uncalled;
}
void del_item(void){
char id[30];
struct node *node, *ahead;
printf("Pls enter the ID you want to delete:");
scanf("%s", id);
for(ahead = node = list_entry; node; ahead = node, node = node->next){
if(strcmp(id, node->student.id)) //id unmatched
continue;
else{ //matched
ahead->next = node->next; //del node;
node->student.called_times == 0? --num_uncalled : --num_called;
free(node);
node = ahead->next;
}
}
}
//------------end of cmd functions--------------
int main(void){
FILE *file;
char *filename = "student.txt";
if((file = fopen(filename, "r")) == NULL)
printf("cannot open %s\n", filename), exit(-2);
read_data(file);
fclose(file);
while(1){
char cmd;
int select_quit_sign = 0;
printf("total %d students, called %d, uncalled %d.\n", num_called + num_uncalled, num_called, num_uncalled);
show_menu();
scanf("%c", &cmd);
printf("you cmd is:%c\n", cmd);
switch(cmd){
case '1':case 'a':case 'A':
show_all();
break;
case '2':case 'c':case 'C':
show_called();
break;
case '3':case 'u':case 'U':
show_uncalled();
break;
case '4':case 'i':case 'I':
add_item();
break;
case '5':case 'd':case 'D':
del_item();
break;
case '6':case 'n':case 'N':
call_a_name();
break;
case '7':case 'q':case 'Q':
select_quit_sign = 1;
break;
default:
printf("unknown command!\n");
break;
}
printf("--------"); //bottom of output
if(select_quit_sign == 1)
break;
scanf("%*c"); //clear input buffer
}
if((file = fopen(filename, "w")) == NULL)
printf("cannot open %s\n", filename), exit(-2);
save_data(file);
fclose(file);
return 0;
}
10. 求C語言做的學生點名程序(急......~!!!!!)
#include<stdio.h>
#include<string.h>
main()
{
int a=0,b=0,c=0,d=0;
char chr='O';
char t[]="noEnd";
//char *m[]={"20013011001杜永寧","20013011002王傳華","20013011003殷泳","20013011004楊柳青"};
printf("........... A 開始點名.........end 結束點名...........\n");
printf("........... B 顯示顯示所有同學點名情況 ...........\n");
printf("........... C 保存點名 ...........\n");
printf("........... D 退出點名系統 ...........\n");
FILE *ft;
//fp=fopen("myfile.dat","r");
ft=fopen("baochun.txt","w");
// fscanf(fp,"%s,%s,%s,%s",m[0],m[1],m[2],m[3]);
while(chr!='x')
{
scanf("%c",&chr);
switch(chr)
{
case 'A':
while(strcmp(t,"over")!=0)
{
if(a+b+c+d==12) {a=b=c=d=0; printf ("清零!\n");}
scanf("%s",&t);
if(strcmp(t,"20013011001杜永寧")==0)
{
a++;
if(a<=3) printf("杜永寧已被點%d次\n",a);
if(a>3)
{ a=3;
printf("杜永寧已被點完三次\n");
}
}
else if(strcmp(t,"20013011002王傳華")==0) {
b++;
if(b<=3) printf("王傳華已被點%d次\n",b);
if(b>3)
{b=3;
printf("王傳華已被點完三次\n");}
}
else if(strcmp(t,"20013011003殷泳")==0){
c++;
if(c<=3) printf("殷泳已被點%d次\n",c);
if(c>3)
{c=3;
printf("殷泳已被點完三次\n");
}
}
else if(strcmp(t,"20013011004楊柳青")==0) {
d++;
if(d<=3) printf("楊柳青已被點%d次\n",d);
if(d>3)
{d=3;
printf("楊柳青已被點完三次\n");
}
}
}break;
case 'B': {
printf("20013011001杜永寧已被點過名的次數為%d\n20013011002王傳華已被點過名的次數為%d\ n20013011003殷泳已被點過名的次數為%d\n20013011004楊柳青已被點過名的次數為%d\n",a,b,c,d);}break;
case 'C':{ printf("保存成功\n");
fprintf(ft,"20013011001 杜永寧 已被點過名的次數為%d\n,20013011002 王傳華已被點過名的次數為%d\n,20013011003 殷泳 已被點過名的次數為%d\n,20013011004 楊柳青已被點過名的次數為%d\n",a,b,c,d);}break;
default:break;
}
}//while(chr!='x');
}