实验7
任务4
源代码
1 #include <stdio.h> 2 3 void func1(); 4 void func2(); 5 6 int main() 7 { 8 printf("data4.txt统计结果\n"); 9 func1(); 10 func2(); 11 return 0; 12 } 13 //统计行数 14 void func1() 15 { 16 int cnt = 1; 17 char ch; 18 FILE* fp; 19 fp = fopen("data4.txt", "r"); 20 if (!fp) 21 { 22 printf("fail to open file to read"); 23 return; 24 } 25 while (!feof(fp)) 26 { 27 ch = fgetc(fp); 28 //if (ch == EOF) 29 // break; 30 if (ch == '\n') 31 cnt++; 32 } 33 printf("%-20s%d\n", "行数:", cnt); 34 } 35 //统计字数 36 void func2() 37 { 38 int cnt = 0; 39 char ch; 40 FILE* fp; 41 fp = fopen("data4.txt", "r"); 42 if (!fp) 43 { 44 printf("fail to open file to read"); 45 return; 46 } 47 while (!feof(fp)) 48 { 49 ch = fgetc(fp); 50 if (ch == EOF) 51 break; 52 if (ch!=' '&&ch!='\n'&&ch!='\t') 53 cnt += 1; 54 55 56 } 57 printf("%-20s%d\n", "字符数(不计空白符)", cnt); 58 }
运行结果
任务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 int i; 75 FILE* fp; 76 fp = fopen("list_pass.txt", "w"); 77 if (!fp) 78 printf("fail to open file to write"); 79 for (i = 0; i < n; i++) 80 fprintf(fp, "%-10d%-10s\t%-10.0f%-10.0f%-10.0f%-15s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 81 fclose(fp); 82 } 83 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 84 int process(STU st[], int n, STU st_pass[]) { 85 int i; 86 int cnt = 0; 87 for (i = 0; i < n; i++) 88 { 89 st[i].sum = st[i].objective + st[i].subjective; 90 if (st[i].sum >= 60) 91 { 92 strcpy(st[i].result, "通过"); 93 cnt++; 94 } 95 else 96 strcpy(st[i].result, "不通过"); 97 } 98 return cnt; 99 }
运行图片
任务6
源代码
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <time.h> 6 #define N 50 7 #define M 100 8 #define NUM 20 9 typedef struct stu 10 { 11 int id; 12 char name[N]; 13 char class[N]; 14 }STU; 15 void read(STU st[]); 16 void pick(int x[]); 17 void output(int x[], STU st[]); 18 void save(int x[], STU st[], char target[]); 19 20 21 int main() 22 { 23 int i = 0; 24 int x[NUM];//保存抽到的序号 25 char target_file[50]; 26 char end[] = ".txt"; 27 STU members[M]; 28 read(members); 29 pick(x); 30 printf("----------随机抽点名单----------\n"); 31 output(x,members); 32 printf("----------保存到文件----------\n"); 33 //获取当前日期 34 time_t current_time; 35 struct tm* local_time; 36 current_time = time(NULL); 37 local_time = localtime(¤t_time); 38 strftime(target_file, sizeof(target_file), "%Y %m %d %A %H %M %S.txt", local_time); 39 save(x, members, target_file); 40 printf("已成功保存至%s",target_file); 41 return 0; 42 } 43 44 void read(STU st[]) 45 { 46 int i = 0; 47 int num; 48 FILE *fp; 49 fp = fopen("list.txt", "r"); 50 if (!fp) 51 printf("fail to open file to read"); 52 while (!feof(fp)) 53 { 54 num = fscanf(fp, "%d%s%s", &st[i].id,st[i].name,st[i].class); 55 if (num != 3) 56 break; 57 i++; 58 } 59 fclose(fp); 60 } 61 62 void pick(int x[]) 63 { 64 int i = 0; 65 int j; 66 int jug; 67 int random_num; 68 do 69 { 70 jug = 1; 71 srand((unsigned int)time(NULL)); 72 random_num = rand()%80; 73 for (j = 0; j < i; j++) 74 { 75 if (x[j] == random_num) 76 jug = 0; 77 } 78 if (jug == 0) 79 continue; 80 x[i] = random_num; 81 i++; 82 } while (i != NUM); 83 } 84 void output(int x[],STU st[]) 85 { 86 int i; 87 for (i = 0; i < NUM; i++) 88 { 89 printf("%-20d%-20s%-40s\n", st[x[i]].id, st[x[i]].name, st[x[i]].class); 90 } 91 } 92 void save(int x[], STU st[], char target[]) 93 { 94 int i; 95 FILE *fp; 96 fp = fopen(target, "w"); 97 for (i = 0; i < NUM; i++) 98 { 99 fprintf(fp, "%-20d%-20s%-40s\n", st[x[i]].id, st[x[i]].name, st[x[i]].class); 100 } 101 fclose(fp); 102 }
运行结果