实验7
任务4
代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 4 5 #define N 3 6 7 void write(); 8 void count(); 9 10 int main() { 11 printf("测试1: 把数据写入文本文件\n"); 12 write(); 13 14 printf("\n测试2: 统计文件的行数和非空白字符数\n"); 15 count(); 16 17 return 0; 18 } 19 20 void write() { 21 const char* lines[N] = { 22 "0123456789-0123456789", 23 "nuist2025", 24 "cosmos galaxy" 25 }; 26 FILE* fp = fopen("data4.txt", "w"); 27 if (!fp) { 28 printf("fail to open file to write\n"); 29 return; 30 } 31 for (int i = 0; i < N; ++i) { 32 fputs(lines[i], fp); 33 fputs("\n", fp); 34 } 35 fclose(fp); 36 } 37 38 int is_whitespace(int ch) { 39 return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'; 40 } 41 42 void count() { 43 FILE* fp = fopen("data4.txt", "r"); 44 if (!fp) { 45 printf("fail to open file to read\n"); 46 return; 47 } 48 int ch, lines = 0, chars = 0; 49 while ((ch = fgetc(fp)) != EOF) { 50 if (ch == '\n') 51 lines++; 52 if (!is_whitespace(ch)) 53 chars++; 54 } 55 fseek(fp, -1, SEEK_END); 56 if (ftell(fp) >= 0) { 57 ch = fgetc(fp); 58 if (ch != '\n') 59 lines++; 60 } 61 fclose(fp); 62 printf("行数: %d\n", lines); 63 printf("非空白字符数: %d\n", chars); 64 }
运行结果:

任务5
代码:
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.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 FILE* fp = fopen("examinee.txt", "w"); 24 if (!fp) { 25 printf("fail to open file\n"); 26 return 1; 27 } 28 fprintf(fp, "1001\t桃乐丝\t36\t55\n"); 29 fprintf(fp, "1002\t稻草人\t28\t40\n"); 30 fprintf(fp, "1003\t千寻\t39\t55\n"); 31 fprintf(fp, "1004\t白龙\t35\t60\n"); 32 fprintf(fp, "1005\t汤婆婆\t20\t35\n"); 33 fprintf(fp, "1006\t无脸男\t33\t50\n"); 34 fprintf(fp, "1007\t希达\t40\t32\n"); 35 fprintf(fp, "1008\t巴鲁\t28\t30\n"); 36 fprintf(fp, "1009\t苏菲\t37\t60\n"); 37 fprintf(fp, "1010\t哈尔\t35\t57\n"); 38 fclose(fp); 39 printf("examinee.txt 文件已生成!\n"); 40 41 STU stu[N], stu_pass[N]; 42 int cnt; 43 double pass_rate; 44 45 printf("从文件读入%d个考生信息...\n", N); 46 read(stu, N); 47 48 printf("\n对考生成绩进行统计...\n"); 49 cnt = process(stu, N, stu_pass); 50 51 printf("\n通过考试的名单:\n"); 52 output(stu, N); // 输出所有考生完整信息到屏幕 53 write(stu, N); // 输出考试通过的考生信息到文件 54 55 pass_rate = 1.0 * cnt / N; 56 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 57 58 return 0; 59 } 60 61 // 把所有考生完整信息输出到屏幕上 62 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 63 void output(STU st[], int n) { 64 int i; 65 66 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 67 for (i = 0; i < n; i++) 68 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); 69 } 70 71 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 72 void read(STU st[], int n) { 73 int i; 74 FILE* fin; 75 76 fin = fopen("examinee.txt", "r"); 77 if (!fin) { 78 printf("fail to open file\n"); 79 return; 80 } 81 82 while (!feof(fin)) { 83 for (i = 0; i < n; i++) 84 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 85 } 86 87 fclose(fin); 88 } 89 90 // 把通过考试的考生完整信息写入文件list_pass.txt 91 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 92 void write(STU s[], int n) { 93 int i; 94 FILE* fp; 95 fp = fopen("list_pass.txt", "w"); 96 fprintf(fp, "%-10s %-8s %-10s %-10s %-10s %-8s\n", "准考证号", "姓名", "客观题得分", "操作题得分", "总分", "结果"); 97 for (i = 0; i < n; i++) { 98 if (s[i].sum >= 60) 99 fprintf(fp, "%-10ld %-8s %-10.2f %-10.2f %-10.2f %-8s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 100 } 101 fclose(fp); 102 } 103 104 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 105 int process(STU st[], int n, STU st_pass[]) { 106 int i, j; 107 for (i = 0; i < n; ++i) { 108 st[i].sum = st[i].objective + st[i].subjective; 109 } 110 for (i = 0, j = 0; i < n; ++i) { 111 if (st[i].sum >= 60) { 112 strcpy(st[i].result, "通过"); 113 st_pass[j] = st[i]; 114 j++; 115 } 116 else { 117 strcpy(st[i].result, "不通过"); 118 } 119 } 120 return j; 121 }
运行结果:


任务6
代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<time.h> 4 #include<stdlib.h> 5 6 #define N 5 7 #define M 80 8 9 struct student 10 { 11 char id[10]; 12 char name[10]; 13 char num_class[30]; 14 }STU; 15 16 int main() 17 { 18 struct student x[M], y[N]; 19 char name_file[50]; 20 char filePath[200]; 21 int i=0,j=0, k,number; 22 FILE *fp; 23 srand(time(NULL)); 24 25 printf("------------------20250525中奖名单-------------------\n"); 26 27 fp = fopen("E:\\学科资料(整理)\\大一下\\C语言实验\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\list.txt","r"); 28 29 if(!fp) 30 { 31 printf("fail to open file to read\n"); 32 return 0; 33 } 34 35 while(!feof(fp)) 36 { 37 number = fscanf(fp, "%s%s%s", x[i].id, x[i].name,x[i].num_class); 38 if(number != 3) 39 break; 40 i++; 41 } 42 fclose(fp); 43 44 45 while(j<5) 46 { 47 k=rand()%79; 48 y[j++]=x[k]; 49 printf("%-10s%-10s%-30s\n", x[k].id, x[k].name, x[k].num_class); 50 } 51 52 printf("输入保存文件名:"); 53 scanf("%s",name_file); 54 sprintf(filePath, "E:\\学科资料(整理)\\大一下\\C语言实验\\实验7数据文件及部分代码\\实验7数据文件及部分代码\\%s.txt", name_file); 55 56 fp=fopen(filePath,"w"); 57 58 if(fp==NULL) 59 { 60 printf("fail to open file to write\n"); 61 return 0; 62 } 63 64 for(j=0;j<N;++j) 65 { 66 fprintf(fp,"%-10s%-10s%-30s\n",y[j].id, y[j].name,y[j].num_class); 67 } 68 fclose(fp); 69 70 system("pausse"); 71 return 0; 72 }
运行结果:

浙公网安备 33010602011771号