关于第7次实验课作业
实验结论
task4.c
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #define N 10000 4 int main() { 5 char ch[N]; 6 int line = 1; 7 int n = 0; 8 int j = 0; 9 FILE* fp; 10 fp = fopen("data4.txt", "r"); 11 if (!fp) { 12 printf("fail to open the file to read\n"); 13 return 0; 14 } 15 int i = 0; 16 while (!feof(fp)) { 17 ch[i] = fgetc(fp); 18 if (ch[i] == '\n') { 19 line++; 20 } 21 if (ch[i] == EOF) { 22 break; 23 } 24 if (ch[i] == '\n' || ch[i] == ' ' || ch[i] == ' ') { 25 j++; 26 } 27 i++; 28 } 29 n = i - j; 30 fclose(fp); 31 printf("data4.txt统计结果:\n"); 32 printf("%-20s%-20d\n", "行数", line); 33 printf("%-20s%-20d", "字符数(不计空白符)", n); 34 return 0; 35 }
task5.c
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.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 write(stu, N); // 输出考试通过的考生信息到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 39 40 return 0; 41 } 42 43 // 把所有考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 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); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void read(STU st[], int n) { 55 int i; 56 FILE* fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (!fin) { 60 printf("fail to open file\n"); 61 return; 62 } 63 64 while (!feof(fin)) { 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 } 68 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void write(STU s[], int n) { 75 FILE* fout; 76 fout=fopen("list_pass.txt", "w"); 77 if (!fout) { 78 printf("fail to open the file to write\n"); 79 return; 80 } 81 fprintf(fout, "%-20s %-20s %-20s %-20s %-20s %-20s\n","准考证号","姓名","客观题得分","操作题得分","总分","结果"); 82 for (int i = 0; i < n; i++) { 83 if (s[i].sum >= 60) { 84 fprintf(fout, "%-20ld %-20s %-20.2f %-20.2f %-20.2f %-20s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 85 } 86 } 87 fclose(fout); 88 } 89 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 90 int process(STU st[], int n, STU st_pass[]) { 91 int cnt = 0; 92 for (int i = 0; i < N; i++) { 93 st[i].sum = st[i].objective + st[i].subjective; 94 if (st[i].sum >= 60) { 95 strcpy(st[i].result, "通过"); 96 st_pass[cnt] = st[i]; 97 cnt++; 98 } 99 else { 100 strcpy(st[i].result, "不通过"); 101 } 102 } 103 return cnt; 104 }
task6.c
必做
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #define N 80 6 typedef struct { 7 char num[20]; 8 char name[20]; 9 char class[40]; 10 }STU; 11 STU st[N]; 12 void read(); 13 void func(); 14 int main() 15 { 16 read(); 17 func(); 18 return 0; 19 } 20 void read() { 21 FILE* fin; 22 fin = fopen("list.txt", "r"); 23 if (!fin) { 24 printf("fail to open the file to read\n"); 25 return; 26 } 27 int number; 28 int i = 0; 29 while (!feof(fin)) { 30 number = fscanf(fin, "%s%s%s", st[i].num, st[i].name, st[i].class); 31 if (number != 3) { 32 break; 33 } 34 i++; 35 } 36 } 37 void func() { 38 srand(time(NULL)); 39 int cnt[5] = { 0 }; 40 int sign = 1; 41 for (int i = 0; i < 5; i++) { 42 int t = rand() % 80; 43 for (int j = 0; j++; j < 5) { 44 if (cnt[j] == t) { 45 sign = 0; 46 break; 47 } 48 } 49 if (sign) { 50 cnt[i] = t; 51 } 52 } 53 54 FILE* fp; 55 fp = fopen("result.txt", "w"); 56 if (!fp) { 57 printf("fail to open the file to write\n"); 58 return; 59 } 60 for (int i = 0; i < 5; i++) { 61 printf("%-20s %-20s %-40s\n", st[cnt[i]].num, st[cnt[i]].name, st[cnt[i]].class); 62 fprintf(fp, "%-20s %-20s %-40s\n", st[cnt[i]].num, st[cnt[i]].name, st[cnt[i]].class); 63 } 64 }
标签:%-,cnt,int,st,STU,实验,printf,文档 From: https://www.cnblogs.com/Samoyed0721/p/18623754