实验七
实验四
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 int line = 0; 6 int chars = 0; 7 char ch; 8 char x; 9 int y; 10 11 FILE *fp; 12 13 fp= fopen("data4.txt", "r"); 14 15 if (fp == NULL) 16 { 17 printf("fail to open file to read\n"); 18 system("pause"); 19 return 1; 20 } 21 22 while ((ch = fgetc(fp)) != EOF) { 23 y = 1; 24 x = ch; 25 26 if (ch == '\n') 27 { 28 line++; 29 } 30 else if (ch != ' ' && ch != '\t') 31 { 32 chars++; 33 } 34 } 35 36 if (y && x != '\n') 37 { 38 line++; 39 } 40 41 printf("data4.txt统计结果:\n"); 42 printf("行数:%d\n", line); 43 printf("字符数(不计算空白符):%d\n", chars); 44 45 fclose(fp); 46 47 system("pause"); 48 return 0; 49 }
运行结果截图

实验五
代码
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 42 system("pause"); 43 return 0; 44 } 45 46 // 把所有考生完整信息输出到屏幕上 47 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 48 void output(STU st[], int n) { 49 int i; 50 51 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 52 for (i = 0; i < n; i++) 53 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); 54 } 55 56 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 57 void read(STU st[], int n) { 58 int i; 59 FILE *fin; 60 61 fin = fopen("examinee.txt", "r"); 62 if (!fin) { 63 printf("fail to open file\n"); 64 return; 65 } 66 67 for (i = 0; i < n; i++) 68 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 69 70 fclose(fin); 71 } 72 73 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 74 int process(STU st[], int n, STU st_pass[]) 75 { 76 int i, cnt = 0; 77 78 for (i = 0; i < n; i++) 79 { 80 st[i].sum = st[i].objective + st[i].subjective; 81 82 if (st[i].sum >= 60) 83 { 84 strcpy(st[i].result, "通过"); 85 st_pass[cnt++] = st[i]; 86 } 87 else 88 { 89 strcpy(st[i].result, "不通过"); 90 } 91 } 92 return cnt; 93 } 94 95 96 // 把通过考试的考生完整信息写入文件list_pass.txt 97 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 98 void write(STU st[], int n) 99 { 100 int i; 101 102 FILE *fout = fopen("list_pass.txt", "w"); 103 104 if (!fout) 105 { 106 printf("无法创建文件 list_pass.txt\n"); 107 return; 108 } 109 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); 110 111 for (i = 0; i < n; i++) 112 { 113 fprintf(fout, "%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); 114 } 115 fclose(fout); 116 }
运行结果截图


实验六(必做)
代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 #define m 80 7 #define w 5 8 9 typedef struct { 10 char id[20]; 11 char name[20]; 12 char class_name[50]; 13 } Student; 14 15 int main() { 16 Student students[m]; 17 18 int count = 0; 19 20 FILE *fp; 21 22 int winner_indices[w]; 23 24 char filename[100]; 25 26 FILE *out; 27 28 int i, j, x, y; 29 30 size_t len; 31 32 fp = fopen("list.txt", "r"); 33 34 if (fp == NULL) 35 { 36 printf("fail to open file\n"); 37 return 1; 38 } 39 40 while (count < m &&fscanf(fp, "%s %s %[^\n]",students[count].id,students[count].name,students[count].class_name) == 3) 41 { 42 count++; 43 } 44 fclose(fp); 45 46 srand((unsigned int)time(NULL)); 47 48 for (i = 0; i < w; i++) 49 { 50 do 51 { 52 x = rand() % count; 53 y = 0; 54 for (j = 0; j < i; j++) 55 { 56 if (winner_indices[j] == x) 57 { 58 y = 1; 59 break; 60 } 61 } 62 } 63 while (y); 64 65 winner_indices[i] = x; 66 } 67 printf("---中奖名单---\n"); 68 69 for (i = 0; i < w; i++) 70 { 71 x = winner_indices[i]; 72 73 printf("%s %s %s\n",students[x].id,students[x].name,students[x].class_name); 74 } 75 printf("\n---保存到文件---\n"); 76 77 printf("输入文件名:"); 78 79 fflush(stdout); 80 81 if (fgets(filename, sizeof(filename), stdin) != NULL) 82 { 83 len = strlen(filename); 84 85 if (len > 0 && filename[len - 1] == '\n') 86 { 87 filename[len - 1] = '\0'; 88 } 89 } 90 91 out = fopen(filename, "w"); 92 93 if (out == NULL) 94 { 95 printf("error %s\n", filename); 96 return 1; 97 } 98 99 for (i = 0; i < w; i++) 100 { 101 x = winner_indices[i]; 102 103 fprintf(out, "%s %s %s\n",students[x].id,students[x].name,students[x].class_name); 104 } 105 fclose(out); 106 107 printf("保存成功!\n"); 108 109 system("pause"); 110 return 0; 111 }
运行结果截图



浙公网安备 33010602011771号