实验七
task4.c
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main() { 5 FILE *fp = fopen("C:\\Users\\李超然\\Documents\\Visual Studio 2010\\Projects\\实验4\\实验4\\Debug\\data4.txt", "r"); 6 if (!fp) { 7 printf("打开文件失败\n"); 8 system("pause"); 9 return 1; 10 } 11 int ch; 12 int line = 0; 13 int charCnt = 0; 14 int isNewLine = 1; 15 16 while ((ch = fgetc(fp)) != EOF) { 17 if (ch == ' ' || ch == '\t' || ch == '\n') { 18 if (ch == '\n') { 19 line++; 20 isNewLine = 1; 21 } 22 continue; 23 } 24 charCnt++; 25 isNewLine = 0; 26 } 27 if (!isNewLine) line++; 28 29 fclose(fp); 30 printf("data4.txt统计结果:\n"); 31 printf("行数: %d\n", line); 32 printf("字符数(不计空白符): %d\n", charCnt); 33 system("pause"); 34 return 0; 35 }
运行结果截图:

task5.c
源代码:
1 #define _CRT_SECURE_NO_WARNINGS 1 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 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 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 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 41 system("pause"); 42 return 0; 43 } 44 45 void output(STU st[], int n) { 46 int i; 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 50 st[i].id, st[i].name, st[i].objective, 51 st[i].subjective, st[i].sum, st[i].result); 52 } 53 54 void read(STU st[], int n) { 55 int i; 56 FILE *fin; 57 // 绝对路径,精准锁定当前文件夹里的文件 58 fin = fopen("C:\\Users\\李超然\\Documents\\Visual Studio 2010\\Projects\\实验7\\实验7\\Debug\\examinee.txt", "r"); 59 if (!fin) { 60 printf("fail to open examinee.txt文件\n"); 61 system("pause"); 62 exit(1); 63 } 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, 66 &st[i].objective, &st[i].subjective); 67 fclose(fin); 68 } 69 70 int process(STU st[], int n, STU st_pass[]) { 71 int passNum = 0; 72 for (int i = 0; i < n; i++) { 73 st[i].sum = st[i].objective + st[i].subjective; 74 if (st[i].sum >= 60) { 75 strcpy(st[i].result, "通过"); 76 st_pass[passNum++] = st[i]; 77 } else { 78 strcpy(st[i].result, "未通过"); 79 } 80 } 81 return passNum; 82 } 83 84 void write(STU st[], int n) { 85 FILE *fp = fopen("C:\\Users\\李超然\\Documents\\Visual Studio 2010\\Projects\\实验7\\实验7\\Debug\\list_pass.txt", "w"); 86 if (!fp) { 87 printf("写入文件失败!\n"); 88 return; 89 } 90 fprintf(fp, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); 91 for (int i = 0; i < n; i++) { 92 fprintf(fp, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", 93 st[i].id, st[i].name, st[i].objective, 94 st[i].subjective, st[i].sum, st[i].result); 95 } 96 fclose(fp); 97 }
运行结果截图:


task6.c
源代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #include <string.h> 5 #define STU_MAX 80 6 7 typedef struct { 8 char id[20]; 9 char name[20]; 10 char cls[30]; 11 } Student; 12 13 void sortStu(Student arr[], int n) { 14 for (int i = 0; i < n - 1; i++) { 15 for (int j = 0; j < n - i - 1; j++) { 16 if (strcmp(arr[j].id, arr[j+1].id) > 0) { 17 Student t = arr[j]; 18 arr[j] = arr[j+1]; 19 arr[j+1] = t; 20 } 21 } 22 } 23 } 24 25 int main() { 26 FILE *fp = fopen("C:\\Users\\李超然\\Documents\\Visual Studio 2010\\Projects\\实验7\\实验7\\Debug\\list.txt", "r"); 27 if (!fp) { 28 printf("无法打开list.txt\n"); 29 system("pause"); 30 return 1; 31 } 32 Student allStu[STU_MAX]; 33 int total = 0; 34 while (fscanf(fp, "%s %s %s", allStu[total].id, allStu[total].name, allStu[total].cls) == 3) { 35 total++; 36 } 37 fclose(fp); 38 39 int flag[STU_MAX] = {0}; 40 Student win[5]; 41 int winCnt = 0; 42 srand((unsigned)time(NULL)); 43 44 while (winCnt < 5) { 45 int idx = rand() % total; 46 if (flag[idx] == 0) { 47 flag[idx] = 1; 48 win[winCnt++] = allStu[idx]; 49 } 50 } 51 52 sortStu(win, 5); 53 54 printf("=====中奖名单=====\n"); 55 for (int i = 0; i < 5; i++) { 56 printf("%s %s %s\n", win[i].id, win[i].name, win[i].cls); 57 } 58 59 time_t now = time(NULL); 60 struct tm *t = localtime(&now); 61 char fileName[50]; 62 strftime(fileName, sizeof(fileName), "%Y%m%d.txt", t); 63 64 FILE *fw = fopen(fileName, "w"); 65 for (int i = 0; i < 5; i++) { 66 fprintf(fw, "%s %s %s\n", win[i].id, win[i].name, win[i].cls); 67 } 68 fclose(fw); 69 printf("文件%s保存成功!\n", fileName); 70 71 system("pause"); 72 return 0; 73 }
运行测试截图:

实验总结:
1.在实际操作中理清了 VS2010 下相对路径与绝对路径的使用差异:程序运行时默认在项目的Debug文件夹下查找文本文件,之前多次出现 “无法打开 xxx.txt” 报错,都是文件存放位置错误导致,学会了通过绝对路径锁定文件位置来规避路径查找问题。
2.理解fopen、fscanf、fprintf、fclose等文件操作函数的使用规则,明确文件打开模式r只读、w覆盖写入的区别。
浙公网安备 33010602011771号