实验7
任务6
View Code
View Code
View Code
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include<stdlib.h> 5 6 #include<time.h> 7 #define N 100 8 int main() { 9 char s[N][N]; 10 FILE *fin; 11 FILE* fp; 12 char name[80]; 13 gets(name); 14 int i = 0, n; 15 fp = fopen("C:\\Users\\空白\\Desktop\\C语言\\实验7数据文件及部分代码_gbk\\list.txt", "r"); 16 fin = fopen(name, "w"); 17 if (!fp) { 18 perror("文件打开失败"); 19 return 3; 20 } 21 if (!fin) { 22 perror("文件打开失败"); 23 return 2; 24 } 25 26 while (fgets(s[i], N, fp) != NULL) 27 28 i++; 29 n = i; 30 31 int ab[N] = { 0 }; 32 int m = 0; 33 srand(time(NULL)); 34 35 for (i = 0; i <5; i++) { 36 m = rand() % n; 37 38 if (ab[m] == 0) { 39 printf("%s", s[m]); 40 ab[m] = 1; 41 fprintf(fin, "%s", s[m]); 42 } 43 else 44 i--; 45 } 46 fclose(fp); 47 fclose(fin); 48 return 0; 49 }


任务5
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include<stdlib.h> 5 6 #define N 10 7 8 typedef struct { 9 long id; 10 char name[20]; 11 float objective; 12 float subjective; 13 float sum; 14 char result[10]; 15 } STU; 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 void output(STU st[], int n) { 44 int i; 45 46 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 47 for (i = 0; i < n; i++) 48 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); 49 } 50 void read(STU st[], int n) { 51 int i; 52 FILE* fin; 53 54 fin = fopen("C:\\Users\\空白\\Desktop\\C语言\\实验7数据文件及部分代码_gbk\\examinee.txt", "r"); 55 if (!fin) { 56 printf("fail to open file\n"); 57 return; 58 } 59 60 for (i = 0; i < n; i++) 61 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 62 63 fclose(fin); 64 } 65 66 67 void write(STU s[], int n) { 68 FILE* fp; 69 fp = fopen("ning.txt", "w"); 70 if (!fp) { 71 printf("fail to open file\n"); 72 return; 73 } 74 fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 75 for (int i = 0; i < n; i++) { 76 if (strcmp(s[i].result, "通过") == 0) 77 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); 78 } 79 fclose(fp); 80 } 81 82 int process(STU st[], int n, STU st_pass[]) { 83 int k = 0; 84 for (int i = 0; i < n; i++) { 85 st[i].sum = st[i].objective + st[i].subjective; 86 if (st[i].sum >= 60) { 87 strcpy(st[i].result, "通过"); 88 st_pass[k++] = st[i]; 89 } 90 else 91 strcpy(st[i].result, "不通过"); 92 } 93 return k; 94 }


通过率漏接了;
实验4
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 #include<stdlib.h> 5 6 int main() { 7 FILE* fp; 8 9 int i = 0; 10 fp = fopen("C:\\Users\\空白\\Desktop\\C语言\\实验7数据文件及部分代码_gbk\\data4.txt", "r"); 11 12 if (!fp) { 13 perror("文件打开失败"); 14 return 3; 15 16 } 17 int k = 0; 18 19 char ch; 20 while ((ch = fgetc(fp)) != EOF) { 21 if (ch == '\n') 22 i++; 23 if(ch!=' '&&ch!='\n'&&ch!='\t') 24 k++; 25 } 26 if (k > 0 && ch != '\n') 27 i++; 28 printf("data4.txt统计结果\n"); 29 printf("行数:%d\n", i); 30 printf("字符数%d\n", k); 31 fclose(fp); 32 return 0; 33 } 34

浙公网安备 33010602011771号