实验7
任务4源码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <string.h> 4 int main() 5 { 6 FILE* fp; 7 fp = fopen("e:\\data4.txt", "r"); 8 if (fp == NULL) 9 { 10 printf("无法打开文件\n"); 11 return 1; 12 } 13 int count = 0; 14 char c; 15 while ((c=fgetc(fp))!=EOF) 16 { 17 if (c != ' ' && c!='\n') 18 { 19 count++; 20 } 21 } 22 printf("%d", count); 23 fclose(fp); 24 return 0; 25 }
任务4结果
任务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 int id; 10 char name[20]; 11 float objective; // 客观题得分 12 float subjective; // 操作题得分 13 float sum; // 总分 14 char ans[10]; // 考试结果 15 } STU; 16 17 // 函数声明 18 void finput(STU st[], int n); 19 void foutput(STU st[], int n); 20 void output(STU st[], int n); 21 int process(STU st[], int n, STU st_pass[]); 22 23 int main() { 24 STU stu[N], stu_pass[N]; 25 int cnt; 26 double pass_rate; 27 28 printf("从文件读入%d个考生信息...\n", N); 29 finput(stu, N); 30 31 printf("\n对考生成绩进行统计...\n"); 32 cnt = process(stu, N, stu_pass); 33 34 printf("\n通过考试的名单:\n"); 35 output(stu, N); // 输出到屏幕 36 foutput(stu, N); // 输出到文件 37 38 pass_rate = 1.0 * cnt / N; 39 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 40 41 return 0; 42 } 43 44 // 把通过考试的考生完整信息输出到屏幕上 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 46 void output(STU st[], int n) { 47 int i; 48 49 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 50 for (i = 0; i < n; i++) 51 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].ans); 52 } 53 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 55 void finput(STU st[], int n) { 56 int i; 57 FILE* fin; 58 59 fin = fopen("examinee.txt", "r"); 60 if (fin == NULL) { 61 printf("fail to open file\n"); 62 exit(0); 63 } 64 65 while (!feof(fin)) { 66 for (i = 0; i < n; i++) 67 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 68 } 69 70 fclose(fin); 71 } 72 73 // 把通过考试的考生完整信息写入文件list_pass.txt 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 75 void foutput(STU s[], int n) { 76 FILE* fout; 77 int i; 78 79 // 保存到文件 80 fout = fopen("list_pass.txt", "w"); 81 if (!fout) { 82 printf("fail to open or create list_pass.txt\n"); 83 exit(0); 84 } 85 86 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 87 88 for (i = 0; i < n; i++) 89 fprintf(fout, "%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].ans); 90 91 fclose(fout); 92 } 93 94 95 96 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 97 int process(STU st[], int n, STU st_pass[]) { 98 int i; 99 int k = 0; 100 for (i = 0; i < n; i++) 101 { 102 st[i].sum = st[i].objective + st[i].subjective; 103 if (st[i].sum >= 60) 104 { 105 strcpy(st[i].ans,"通过"); 106 st_pass[k++] = st[i]; 107 } 108 else 109 { 110 strcpy(st[i].ans, "未通过"); 111 } 112 } 113 return k; 114 }
任务5结果
任务6源码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<time.h> 5 int main() 6 { 7 FILE* fp; 8 fp = fopen("list.txt","r"); 9 if (fp == NULL) 10 { 11 printf("failed"); 12 exit(0); 13 } 14 int i; 15 char a[80][100]; 16 for (i = 0; i < 80; i++) 17 { 18 fgets(a[i], 100, fp); 19 } 20 FILE* fp2; 21 fp2 = fopen("lucky.txt", "w"); 22 if (fp2 == NULL) 23 { 24 printf("failed"); 25 exit(0); 26 } 27 srand(time(NULL)); 28 for (i = 0; i < 5; i++) 29 { 30 int t = 0; 31 t = rand() % 81; 32 fprintf(fp2, "%s", a[t]); 33 printf("%s", a[t]); 34 } 35 return 0; 36 }
1 #define _CRT_SECURE_NO_WARNINGS 2 #define N 5 3 #include<stdio.h> 4 #include<stdlib.h> 5 #include<time.h> 6 #include<string.h> 7 int main() 8 { 9 FILE* fp; 10 fp = fopen("list.txt","r"); 11 if (fp == NULL) 12 { 13 printf("failed"); 14 exit(0); 15 } 16 int i; 17 char a[80][100]; 18 for (i = 0; i < 80; i++) 19 { 20 fgets(a[i], 100, fp); 21 } 22 FILE* fp2; 23 char Time[20]; 24 time_t timer; 25 timer = time(NULL); 26 strftime(Time, 20, "%Y.%m.%d", localtime(&timer)); 27 fp2 = fopen(Time, "w"); 28 if (fp2 == NULL) 29 { 30 printf("failed"); 31 exit(0); 32 } 33 char b[5][100]; 34 srand((double)time(NULL)); 35 for (i = 0; i < N;) 36 { 37 int t = 0; 38 t = rand() % 81; 39 if (strcmp(a[t], " ")) 40 { 41 strcpy(b[i], a[t]); 42 strcpy(a[t], " "); 43 i++; 44 } 45 } 46 int j; 47 for (i = 0; i < N - 1; i++) 48 { 49 for (j = 0; j < N - i - 1; j++) 50 { 51 if (strcmp(b[j], b[j + 1]) > 0) 52 { 53 char c[100]; 54 strcpy(c, b[j]); 55 strcpy(b[j], b[j + 1]); 56 strcpy(b[j + 1], c); 57 } 58 } 59 } 60 for (i = 0; i < N; i++) 61 { 62 fprintf(fp2, "%s" , b[i]); 63 puts(b[i]); 64 } 65 return 0; 66 }
任务6结果