实验7
实验任务4
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 int main() 4 { 5 FILE *fp; 6 char ch; 7 int line=0,charCnt=0; 8 fp=fopen("C:\\Users\\lenovo\\Desktop\\实验7数据文件及部分代码_utf-8\\实验7数据文件及部分代码_utf-8\\tesk7\\Debug\\data4.txt","r"); 9 if(fp==NULL) 10 { 11 printf("打开文件失败\n"); 12 system("pause"); 13 return 1; 14 } 15 while((ch=fgetc(fp))!=EOF) 16 { 17 if(ch=='\n') 18 { 19 line++; 20 } 21 if(ch!=' '&&ch!='\n'&&ch!='\t') 22 { 23 charCnt++; 24 } 25 } 26 fclose(fp); 27 printf("data4.txt统计结果:\n"); 28 printf("行数:%d\n",line); 29 printf("字符数(不计空白符):%d\n",charCnt); 30 system("pause"); 31 return 0; 32 }
运行结果截图

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


实验任务6
源代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <string.h> 6 #define STU_NUM 80 7 #define WIN_NUM 5 8 typedef struct{ 9 char id[15]; 10 char name[20]; 11 char cls[30]; 12 }Student; 13 int readList(Student stu[]) 14 { 15 FILE *fp; 16 int i; 17 fp=fopen("list.txt","r"); 18 if(fp==NULL) 19 { 20 printf("无法打开list.txt\n"); 21 system("pause"); 22 exit(1); 23 } 24 i=0; 25 while(fscanf(fp,"%s %s %s",stu[i].id,stu[i].name,stu[i].cls)!=EOF&&i<STU_NUM) 26 { 27 i++; 28 } 29 fclose(fp); 30 return i; 31 } 32 void getRandomIdx(int total,int winIdx[]) 33 { 34 int flag[STU_NUM]={0}; 35 int cnt=0; 36 int r; 37 srand((unsigned)time(NULL)); 38 while(cnt<WIN_NUM) 39 { 40 r=rand()%total; 41 if(flag[r]==0) 42 { 43 flag[r]=1; 44 winIdx[cnt++]=r; 45 } 46 } 47 } 48 int main() 49 { 50 Student allStu[STU_NUM]; 51 Student winner[WIN_NUM]; 52 int winIdx[WIN_NUM]; 53 char filename[30]; 54 int total; 55 int i; 56 FILE *fw; 57 total=readList(allStu); 58 getRandomIdx(total,winIdx); 59 for(i=0;i<WIN_NUM;i++) 60 { 61 winner[i]=allStu[winIdx[i]]; 62 } 63 printf("----------------中奖名单----------------\n"); 64 for(i=0;i<WIN_NUM;i++) 65 { 66 printf("%s\t%s\t%s\n",winner[i].id,winner[i].name,winner[i].cls); 67 } 68 printf("输入文件名:"); 69 scanf("%s",filename); 70 fw=fopen(filename,"w"); 71 if(fw==NULL) 72 { 73 printf("文件创建失败!\n"); 74 system("pause"); 75 return 1; 76 } 77 for(i=0;i<WIN_NUM;i++) 78 { 79 fprintf(fw,"%s\t%s\t%s\n",winner[i].id,winner[i].name,winner[i].cls); 80 } 81 fclose(fw); 82 printf("----------------保存到文件----------------\n"); 83 printf("保存成功!\n"); 84 system("pause"); 85 return 0; 86 }
运行结果截图


浙公网安备 33010602011771号