实验7
任务4
源代码:
1 #include <stdio.h> 2 3 int main() 4 { 5 int line_count=1,char_count=0; 6 7 FILE *fp=fopen("data4.txt","r"); 8 9 while(!feof(fp)) 10 { 11 char ch=fgetc(fp); 12 if(ch==EOF) 13 break; 14 15 if(ch=='\n') 16 line_count++; 17 else if(!(ch=='\t'||ch==' ')) 18 char_count++; 19 } 20 21 fclose(fp); 22 23 printf("data4.txt统计结果:\n行数:%d\n字符数(不计空白符):%d",line_count,char_count); 24 25 return 0; 26 }
运行结果:

任务5
源代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; // 准考证号 8 char name[20]; // 姓名 9 float objective; // 客观题得分 10 float subjective; // 操作题得分 11 float sum; // 总分 12 char result[10]; // 考试结果 13 } STU; 14 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU s[], int n) { 74 FILE *fout=fopen("list_pass.txt","w"); 75 76 int i; 77 for(i=0;i<n;i++) 78 fprintf(fout,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 79 80 fclose(fout); 81 } 82 83 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 84 int process(STU st[], int n, STU st_pass[]) { 85 int i,count=0; 86 for(i=0;i<n;i++) 87 { 88 st[i].sum=st[i].objective+st[i].subjective; 89 if(st[i].sum>=60) 90 { 91 strcpy(st[i].result,"通过"); 92 st_pass[count++]=st[i]; 93 } 94 else 95 { 96 strcpy(st[i].result,"未通过"); 97 } 98 } 99 return count; 100 }
运行结果:


任务6
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 int i=0; 8 char **allstu=(char**)malloc(sizeof(char*)*80); 9 for(i=0;i<80;i++) 10 allstu[i]=(char*)malloc(sizeof(char)*64);//$ 11 12 //读取80名学生信息 13 14 FILE *fp=fopen("list.txt","r"); 15 16 i=0; 17 while(!feof(fp)) 18 { 19 fgets(allstu[i],64,fp); 20 i++; 21 } 22 23 fclose(fp); 24 25 //挑选5名学生,并对他们升序排序 26 int mark[80]; 27 for(i=0;i<80;i++) 28 mark[i]=0; 29 30 int pickedn=0; 31 int picked[5];//$ 32 srand((unsigned)time(NULL)); 33 while(pickedn<5) 34 { 35 int num=rand()%80; 36 if(mark[num]==-1) 37 continue; 38 else 39 { 40 mark[num]=-1; 41 picked[pickedn++]=num; 42 } 43 } 44 45 //显示挑选的名单 46 printf("-----------随机抽点名单-----------\n"); 47 for(i=0;i<5;i++) 48 printf("%s",allstu[picked[i]]); 49 50 //保存挑选的名单 51 char fn[16]; 52 printf("\n-----------保存到文件-----------\n输入文件名:"); 53 scanf("%s",fn); 54 FILE *fout=fopen(fn,"w"); 55 for(i=0;i<5;i++) 56 fprintf(fout,"%s",allstu[picked[i]]); 57 fclose(fout); 58 printf("\n文档保存成功!"); 59 60 for(i=0;i<80;i++) 61 free(allstu[i]); 62 free(allstu); 63 }
运行结果:


任务6*
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int main() 6 { 7 int i=0; 8 char **allstu=(char**)malloc(sizeof(char*)*80); 9 for(i=0;i<80;i++) 10 allstu[i]=(char*)malloc(sizeof(char)*64);//$ 11 12 //读取80名学生信息 13 14 FILE *fp=fopen("list.txt","r"); 15 16 i=0; 17 while(!feof(fp)) 18 { 19 fgets(allstu[i],64,fp); 20 i++; 21 } 22 23 fclose(fp); 24 25 //挑选5名学生,并对他们升序排序 26 int mark[80]; 27 for(i=0;i<80;i++) 28 mark[i]=0; 29 30 int pickedn=0; 31 int picked[5];//$ 32 srand((unsigned)time(NULL)); 33 while(pickedn<5) 34 { 35 int num=rand()%80; 36 if(mark[num]==-1) 37 continue; 38 else 39 { 40 mark[num]=-1; 41 picked[pickedn++]=num; 42 } 43 } 44 45 int j,n=5; 46 for(i=1;i<=n-1;i++) 47 { 48 for(j=0;j<n-i;j++) 49 { 50 if(picked[j]>picked[j+1]) 51 { 52 int temp=picked[j]; 53 picked[j]=picked[j+1]; 54 picked[j+1]=temp; 55 } 56 } 57 } 58 59 //获取日期 60 char date[15];//$ 61 time_t t=time(NULL); 62 struct tm *lt=localtime(&t); 63 strftime(date,sizeof(date),"%Y%m%d.txt",lt); 64 65 //显示挑选的名单 66 printf("-----------%s抽点名单-----------\n",date); 67 for(i=0;i<5;i++) 68 printf("%s",allstu[picked[i]]); 69 70 //保存挑选的名单到"日期.txt" 71 FILE *fout=fopen(date,"w"); 72 for(i=0;i<5;i++) 73 fprintf(fout,"%s",allstu[picked[i]]); 74 fclose(fout); 75 printf("\n文档保存成功!"); 76 77 for(i=0;i<80;i++) 78 free(allstu[i]); 79 free(allstu); 80 }
运行结果:



浙公网安备 33010602011771号