实验七
任务四
1源代码及运行截图
1 #include <stdio.h> 2 #include<stdlib.h> 3 int main() 4 { 5 FILE *fp; 6 char ch; 7 int line = 1, charNum = 0; 8 9 fp = fopen("C:\\Users\\LENOVO\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\data4.txt", "r"); 10 if (fp == NULL) 11 { 12 printf("文件打开失败!\n"); 13 return 1; 14 } 15 16 while ((ch = fgetc(fp)) != EOF) 17 { 18 if (ch == '\n') 19 { 20 line++; 21 } 22 // 排除空格、回车、Tab 23 if (ch != ' ' && ch != '\n' && ch != '\t') 24 { 25 charNum++; 26 } 27 } 28 fclose(fp); 29 30 printf("data4.txt统计结果:\n"); 31 printf("行数:\t\t\t%d\n", line); 32 printf("字符数(不计空白符):\t%d\n", charNum); 33 system("pause"); 34 return 0; 35 }

任务五
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 // 函数声明 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 system("pause"); 41 return 0; 42 } 43 44 void output(STU st[], int n) { 45 int i; 46 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", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 50 } 51 void read(STU st[], int n) { 52 int i; 53 FILE *fin; 54 55 fin = fopen("C:\\Users\\Administrator\\Desktop\\C实验7\\实验7数据文件及部分代码_gbk\\examinee.txt", "r"); 56 if (!fin) { 57 printf("fail to open file\n"); 58 return; 59 } 60 61 for (i = 0; i < n; i++) 62 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 63 64 fclose(fin); 65 } 66 67 int process(STU st[], int n, STU st_pass[]) { 68 int i; 69 int cntpass=0; 70 for(i=0;i<n;i++) 71 { 72 st[i].sum=st[i].objective+st[i].subjective; 73 if(st[i].sum>=60) 74 { 75 strcpy(st[i].result,"通过"); 76 st_pass[cntpass++]=st[i]; 77 } 78 else 79 { 80 strcpy(st[i].result,"不通过"); 81 } 82 } 83 return cntpass; 84 } 85 86 87 void write(STU st[], int n) { 88 int i; 89 FILE *fout=fopen("C:\\Users\\Administrator\\Desktop\\C实验7\\实验7数据文件及部分代码_gbk\\list_pass.txt","w"); 90 if(fout==NULL) 91 { 92 printf("fail to open output file\n"); 93 return; 94 } 95 for (i = 0; i < n; i++) 96 { 97 fprintf(fout,"%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); 98 } 99 fclose(fout); 100 }


任务六
1.源代码及运行截图
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <string.h> 6 #include<stdlib.h> 7 #define MAX_STU 80 8 #define LEN_LINE 100 9 10 int main() 11 { 12 13 char student[MAX_STU][LEN_LINE]; 14 int flag[MAX_STU] = {0}; 15 FILE *fp; 16 char saveFile[50]; 17 int count = 0; 18 int i, j, randNum; 19 char winner[5][LEN_LINE]; 20 int winCnt = 0; 21 22 23 fp = fopen("C:\\Users\\LENOVO\\Downloads\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\list.txt", "r"); 24 if (fp == NULL) 25 { 26 printf("无法打开list.txt文件!\n"); 27 return 1; 28 } 29 while (fgets(student[count], LEN_LINE, fp) != NULL && count < MAX_STU) 30 { 31 student[count][strcspn(student[count], "\n")] = '\0'; 32 count++; 33 } 34 fclose(fp); 35 36 srand((unsigned int)time(NULL)); 37 while (winCnt < 5) 38 { 39 randNum = rand() % count; 40 if (flag[randNum] == 0) 41 { 42 flag[randNum] = 1; 43 strcpy(winner[winCnt], student[randNum]); 44 winCnt++; 45 } 46 } 47 48 printf("--------中奖名单--------\n"); 49 for (i = 0; i < 5; i++) 50 { 51 printf("%s\n", winner[i]); 52 } 53 54 55 printf("输入文件名:"); 56 scanf("%s", saveFile); 57 58 fp = fopen(saveFile, "w"); 59 if (fp == NULL) 60 { 61 printf("文件创建失败!\n"); 62 return 1; 63 } 64 for (i = 0; i < 5; i++) 65 { 66 fprintf(fp, "%s\n", winner[i]); 67 } 68 fclose(fp); 69 70 printf("--------保存到文件--------\n"); 71 printf("保存成功!\n"); 72 system("pause"); 73 return 0; 74 }



浙公网安备 33010602011771号