实验七
实验任务4
源代码
1 #include <stdio.h> 2 #include <ctype.h> 3 #include <stdlib.h> 4 5 int main(){ 6 FILE *fp; 7 char ch; 8 int line_count=0; 9 int char_count=0; 10 int is_new_line=1; 11 12 fp=fopen("data4.txt","r"); 13 if(fp==NULL){ 14 printf("无法打开文件data.txt\n"); 15 return 1; 16 } 17 18 while((ch=fgetc(fp))!=EOF){ 19 20 if(ch=='\n'){ 21 line_count++; 22 is_new_line=1; 23 } 24 25 if(!isspace(ch)){ 26 char_count++; 27 } 28 } 29 30 if(!is_new_line){ 31 line_count++; 32 } 33 34 fclose(fp); 35 36 printf("data4.txt 统计结果:\n"); 37 printf("行数:\t\t\t%d\n字符数(不计空白符):\t%d\n",line_count,char_count); 38 39 system("pause"); 40 41 return 0; 42 }
运行结果截图

实验任务5
源代码
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 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 void read(STU st[], int n); 16 void write(STU st[], int n); 17 void output(STU st[], int n); 18 int process(STU st[], int n, STU st_pass[]); 19 20 int main() 21 { 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 35 printf("\n通过考试的名单写入文件:已完成!\n"); 36 write(stu_pass, cnt); 37 38 pass_rate = 1.0 * cnt / N; 39 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 40 system("pause"); 41 42 return 0; 43 } 44 45 46 void output(STU st[], int n) 47 { 48 int i; 49 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 50 for (i = 0; i < n; i++) 51 { 52 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, 53 st[i].objective, st[i].subjective, st[i].sum, st[i].result); 54 } 55 } 56 57 58 void read(STU st[], int n) 59 { 60 int i; 61 FILE *fin; 62 fin = fopen("examinee.txt", "r"); 63 if (!fin) 64 { 65 printf("fail to open file\n"); 66 return; 67 } 68 for (i = 0; i < n; i++) 69 { 70 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 71 } 72 fclose(fin); 73 } 74 75 76 int process(STU st[], int n, STU st_pass[]) 77 { 78 int i, cnt = 0; 79 for(i = 0; i < n; i++) 80 { 81 st[i].sum = st[i].objective + st[i].subjective; 82 if(st[i].sum >= 60) 83 { 84 strcpy(st[i].result, "通过"); 85 st_pass[cnt] = st[i]; 86 cnt++; 87 } 88 else 89 { 90 strcpy(st[i].result, "不通过"); 91 } 92 } 93 return cnt; 94 } 95 96 97 void write(STU st[], int n) 98 { 99 int i; 100 FILE *fp; 101 fp = fopen("list_pass.txt","w"); 102 for(i = 0; i < n; i++) 103 { 104 fprintf(fp,"%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", 105 st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result); 106 } 107 fclose(fp); 108 }
运行结果截图


实验任务6
源代码
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 6 #define MAX_STU 80 7 typedef struct { 8 char id[12]; 9 char name[20]; 10 char cls[30]; 11 } Student; 12 13 int cmp(const void *a, const void *b) { 14 Student *sa = (Student*)a; 15 Student *sb = (Student*)b; 16 return atoll(sa->id) - atoll(sb->id); 17 } 18 19 int main() { 20 Student all_stu[MAX_STU]; 21 int flag[MAX_STU] = {0}; 22 23 Student lucky[5]; 24 int total_cnt = 0; 25 srand((unsigned int)time(NULL)); 26 27 FILE *fin = fopen("list.txt", "r"); 28 if (!fin) { 29 printf("无法打开学生名单文件list.txt\n"); 30 return 1; 31 } 32 while (fscanf(fin, "%s %s %s", 33 all_stu[total_cnt].id, 34 all_stu[total_cnt].name, 35 all_stu[total_cnt].cls) != EOF) { 36 total_cnt++; 37 if (total_cnt >= MAX_STU) break; 38 } 39 fclose(fin); 40 41 int cnt = 0; 42 while (cnt < 5) { 43 int idx = rand() % total_cnt; 44 if (flag[idx] == 0) { 45 flag[idx] = 1; 46 lucky[cnt++] = all_stu[idx]; 47 } 48 }
运行结果截图

浙公网安备 33010602011771号