实验7
1 #include<stdio.h> 2 int main(){ 3 FILE *data4; 4 int ch,lines=0,chars=0; 5 data4=fopen("data4.txt","r"); 6 if(!data4) 7 { 8 printf("Open file failed(data4.txt)"); 9 return 1; 10 } 11 while((ch=fgetc(data4))!=EOF){ 12 switch(ch){ 13 case' ': 14 case'\r': 15 case'\t': 16 break; 17 case'\n': 18 lines++; 19 break; 20 default: 21 chars++; 22 break; 23 } 24 } 25 lines++; 26 fclose(data4); 27 printf("data4.txt统计结果:\n行数:%d\n字符数(不计空白符):%d",lines,chars); 28 return 0; 29 }

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.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 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].result); 52 } 53 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 55 void read(STU st[], int n) { 56 int i; 57 FILE *fin; 58 59 fin = fopen("examinee.txt", "r"); 60 if (!fin) { 61 printf("fail to open file\n"); 62 return; 63 } 64 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 68 fclose(fin); 69 } 70 71 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 72 int process(STU st[], int n, STU st_pass[]) { 73 int i,count=0; 74 for(i=0;i<n;i++){ 75 st[i].sum=st[i].objective+st[i].subjective; 76 if(st[i].sum>=60){ 77 strcpy(st[i].result,"通过"); 78 st_pass[count++]=st[i];} 79 else 80 strcpy(st[i].result,"未通过"); 81 } 82 return count; 83 } 84 85 // 把通过考试的考生完整信息写入文件list_pass.txt 86 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 87 void write(STU st[], int n) { 88 int i; 89 FILE *fin; 90 fin = fopen("list_pass.txt", "w"); 91 if (!fin) { 92 printf("fail to open file\n"); 93 return; 94 } 95 fprintf(fin, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 96 for (i = 0; i < n; i++) 97 fprintf(fin, "%ld %s %.2f %.2f %.2f %s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective,st[i].sum,st[i].result); 98 99 fclose(fin);

1 #include <stdio.h>
2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 7 typedef struct { 8 char id[20]; 9 char name[20]; 10 char class_name[50]; 11 } Student; 12 13 int main() { 14 Student students[80]; 15 int winners[5]; 16 int i, j; 17 FILE *fp_in, *fp_out; 18 char out_filename[50]; 19 int is_duplicate; 20 21 22 fp_in = fopen("list.txt", "r"); 23 if (fp_in == NULL) { 24 printf("错误:无法打开 list.txt,请确保文件在当前目录下。\n"); 25 return 1; 26 } 27 28 29 for (i = 0; i < 80; i++) { 30 fscanf(fp_in, "%s %s %s", students[i].id, students[i].name, students[i].class_name); 31 } 32 fclose(fp_in); 33 34 35 srand((unsigned)time(NULL)); 36 37 38 for (i = 0; i < 5; i++) { 39 while (1) { 40 41 int index = rand() % 80; 42 43 44 is_duplicate = 0; 45 for (j = 0; j < i; j++) { 46 if (winners[j] == index) { 47 is_duplicate = 1; 48 break; 49 } 50 } 51 52 53 if (!is_duplicate) { 54 winners[i] = index; 55 break; 56 } 57 } 58 } 59 60 // 61 printf("请输入要保存中奖名单的文件名 (例如 result.txt): "); 62 scanf("%s", out_filename); 63 64 65 fp_out = fopen(out_filename, "w"); 66 if (fp_out == NULL) { 67 printf("错误:无法创建输出文件。\n"); 68 return 1; 69 } 70 71 72 printf("\n-------- 中奖名单 --------\n"); 73 fprintf(fp_out, "-------- 中奖名单 --------\n"); 74 75 for (i = 0; i < 5; i++) { 76 int idx = winners[i]; 77 78 79 printf("%-15s %-10s %s\n", students[idx].id, students[idx].name, students[idx].class_name); 80 81 82 fprintf(fp_out, "%-15s %-10s %s\n", students[idx].id, students[idx].name, students[idx].class_name); 83 } 84 85 printf("--------------------------\n"); 86 printf("名单已保存到 %s\n", out_filename); 87 88 fclose(fp_out); 89 90 return 0; 91 }


浙公网安备 33010602011771号