实验7
4.
#include <stdio.h> #include <stdlib.h> int main() { int line=0,len=0; char ch; FILE *fp; fp=fopen("D:\\data4.txt","r"); if(!fp){ printf("fail to open file to read\n"); return 0; } while((ch=fgetc(fp))!=EOF){ if(ch=='\n') line++; if(ch!=' '&&ch!='\t'&&ch!='\n') len+=1; } fclose(fp); line++; printf("data4.txt统计结果:\n"); printf("行数: %d\n", line); printf("字符数(不计空白符): %d\n",len); system("pause"); return 0; }

5.
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.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 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息: 已完成\n", N); 28 read(stu, N); 29 30 printf("\n对考生成绩进行统计: 已完成\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n所有考生完整信息:\n"); 34 output(stu, N); 35 36 printf("\n通过考试的名单写入文件: 已完成!\n"); 37 write(stu_pass, cnt); 38 39 pass_rate = 1.0 * cnt / N; 40 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 41 42 system("pause"); 43 44 return 0; 45 } 46 47 // 把所有考生完整信息输出到屏幕上 48 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 49 void output(STU st[], int n) { 50 int i; 51 52 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 53 for (i = 0; i < n; i++) 54 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); 55 } 56 57 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 58 void read(STU st[], int n) { 59 int i; 60 FILE *fin; 61 62 fin = fopen("D:\\examinee.txt", "r"); 63 if (!fin) { 64 printf("fail to open file\n"); 65 return; 66 } 67 68 for (i = 0; i < n; i++) 69 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 70 71 fclose(fin); 72 } 73 74 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 75 int process(STU st[], int n, STU st_pass[]) { 76 int i,k=0; 77 for(i=0;i<n;i++) 78 { 79 st[i].sum=st[i].objective+st[i].subjective; 80 if(st[i].sum>=60) 81 { 82 strcpy(st[i].result,"通过"); 83 st_pass[k++]=st[i]; 84 } 85 else 86 strcpy(st[i].result,"未通过"); 87 } 88 89 return k; 90 91 } 92 93 // 把通过考试的考生完整信息写入文件list_pass.txt 94 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 95 void write(STU st[], int n) { 96 int i; 97 FILE *fw; 98 99 fw=fopen("D:\\list_pass1.txt","w"); 100 if(!fw){ 101 printf("书写失败"); 102 return ; 103 } 104 fprintf(fw,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 105 for (i = 0; i < n; i++){ 106 fprintf(fw,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 107 st[i].id, 108 st[i].name, 109 st[i].objective, 110 st[i].subjective, 111 st[i].sum, 112 st[i].result); 113 } 114 115 fclose(fw); 116 }

6.
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string> #define MAX 100 typedef struct { char id[16]; char name[16]; char cls[32]; }STU; int read(STU st[], int n); void draw_lottery(STU st[], int total, int count); void save_to_file(STU winners[], int count); int main(){ STU students[MAX]; int total; total=read(students, MAX); printf("从文件读入%d个考生信息: 已完成\n",total); draw_lottery(students, total, 5); system("pause"); return 0; } int read(STU st[], int n) { int i=0; FILE *fin; fin = fopen("D:\\list.txt", "r"); if (!fin) { printf("fail to open file\n"); return 0; } while(i<n&&fscanf(fin, "%s %s %[^\t\n]", &st[i].id, st[i].name, &st[i].cls)==3){ i++; } fclose(fin); return i; } void draw_lottery(STU st[], int total, int count) { int i, index; int *selected; STU winners[10]; if (count > total) { printf("抽取人数超过总人数!\n"); return; } selected = (int *)calloc(total, sizeof(int)); if (!selected) { printf("内存分配失败!\n"); return; } srand((unsigned)time(NULL)); printf("\n========== 中奖名单 ==========\n"); printf("学号\t\t姓名\t班级\n"); printf("--------------------------------\n"); for (i = 0; i < count; i++) { do { index = rand() % total; } while (selected[index] == 1); selected[index] = 1; winners[i] = st[index]; printf("%s\t%s\t%s\n", winners[i].id, winners[i].name, winners[i].cls); } printf("================================\n"); free(selected); save_to_file(winners, count); } void save_to_file(STU winners[], int count) { int i; char filename[100]; FILE *fw; printf("\n请输入要保存的文件名(如:winners.txt):"); scanf("%s", filename); fw = fopen(filename, "w"); if (!fw) { printf("文件创建失败!\n"); return; } fprintf(fw, "========== 中奖名单 ==========\n"); fprintf(fw, "学号\t\t姓名\t班级\n"); fprintf(fw, "--------------------------------\n"); for (i = 0; i < count; i++) { fprintf(fw, "%s\t%s\t%s\n", winners[i].id, winners[i].name, winners[i].cls); } fprintf(fw, "================================\n"); fclose(fw); printf("中奖名单已保存到文件:%s\n", filename); }

浙公网安备 33010602011771号