实验七
实验7
任务4
代码
1 #include <stdio.h> 2 #include <string.h> 3 #define N 100 4 5 int main (){ 6 FILE *file = fopen("data4.txt", "r"); 7 if (file == NULL) { 8 printf("无法打开文件 data4.txt\n"); 9 return 1; 10 } 11 int line = 0; 12 int sum = 0; 13 int i = 0; 14 15 char ch[N]; 16 while (fgets(ch,N,file) != NULL){ 17 line ++; 18 for(i = 0;ch[i] != '\0';i++){ 19 if(ch[i] != ' '&& ch[i] != '\n') 20 sum ++; 21 } 22 } 23 24 fclose (file); 25 printf("data4.txt统计结果:\n"); 26 printf("行数:\t\t%d\n", line); 27 printf("字符数(不计空白符):\t%d\n", sum); 28 return 0; 29 }
截图

任务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 // 待补足 75 // xxx 76 FILE *fp ; 77 fp = fopen("list_pass.txt", "w"); 78 if (!fp) { 79 printf("无法打开输出文件\n"); 80 return; 81 } 82 int i = 0; 83 fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 84 for (int i = 0; i < n; i++) { 85 fprintf(fp, "%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); 86 } 87 fclose(fp); 88 } 89 90 91 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 92 int process(STU st[], int n, STU st_pass[]) { 93 // 待补足 94 // xxx 95 int i = 0; 96 char a[] = "通过"; 97 char b[] = "未通过"; 98 int guo = 0; 99 for (i = 0;i<n;i++){ 100 st[i].sum = st[i].objective+st[i].subjective; 101 102 if(st[i].sum >= 60){ 103 strcpy(st[i].result,a); 104 guo++; 105 } 106 else strcpy(st[i].result,b); 107 } 108 109 return guo; 110 }
截图

任务6
代码
#include <stdio.h> #include <string.h> #include <time.h> #include <stdlib.h> #define N 80 #define M 100 void write(); void read(); void fsd(); int main (){ printf("---------------中奖名单----------------\n"); char luck[5][M]; FILE *fp; fp = fopen("list.txt","rb"); if(!fp){ printf("无法打开输出文件\n"); return 0;} int i = 0,j = 0; int number; char t[N][M]; for (i = 0;i<N;i++){ fgets(t[i],M,fp); } fclose(fp); srand(time(NULL)); for (i = 0;i < 5;i++ ){ strcpy(luck[i] , t[rand() % N]); for (j = 0;j<i;j++){ if (strcmp(luck[j], t[rand() % N] ) == -1 ){ i--; break; } } } char temp[M]; for (i = 0;i<4;i++){ for (j = 0;j<4;j++){ if(strcmp(luck[j],luck[j+1]) > 0){ strcpy(temp , luck[j]); strcpy(luck[j] , luck[j+1]); strcpy(luck[j+1] , temp); } } } for (i = 0;i<5;i++){ printf("%s",luck[i]); } char filename[N]; printf("\n---------------保存到文件-------------\n"); printf("输入文件名:"); scanf("%s",filename); fp = fopen(filename, "w"); for (i = 0; i < 5; i++) { fprintf(fp, "%s\n", luck[i]); } printf("中奖名单已成功写入文件 \n"); }
截图


实验总结:通过这七次实验,我感受到了c语言的魅力,也特别感谢老师在编程方面给予我的指导,扩宽了我的编程思路,希望以后还能有机会更加深入学习其他编程语言。
                    
                
                
            
        
浙公网安备 33010602011771号