实验
实验任务4
源代码:
1 #include<stdlib.h> 2 #include<stdio.h> 3 #define N 100 4 5 int read_str(); 6 int read_char(); 7 int main() 8 { 9 int a,b; 10 a=read_str(); 11 b=read_char(); 12 printf("data4.txt统计结果:\n%-20s%d\n%-20s%d\n","行数",a,"字符数(不计空白符):",b); 13 system("pause"); 14 return 0; 15 } 16 17 int read_str() 18 { 19 char x[N][N]; 20 int i,n; 21 FILE *fp; 22 fp=fopen("data4.txt","r"); 23 if(fp==NULL){ 24 printf("fail to open file to read\n"); 25 return 0; 26 } 27 28 i=0; 29 while(i<N&&(fgets(x[i],N,fp)!=NULL)) 30 i++; 31 fclose(fp); 32 return i; 33 } 34 35 int read_char() 36 { 37 int i,n; 38 FILE *fp; 39 fp=fopen("data4.txt","r"); 40 if(fp==NULL){ 41 printf("fail to open file to read\n"); 42 return 0;} 43 i=0; 44 while ((n = fgetc(fp)) != EOF) { 45 if (n != ' ' && n != '\n' && n != '\t' && n != '\r') { 46 i++; 47 } 48 } 49 fclose(fp); 50 return i; 51 }
实验截图:

实验任务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 system("pause"); 42 return 0; 43 } 44 45 // 把所有考生完整信息输出到屏幕上 46 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 47 void output(STU st[], int n) { 48 int i; 49 50 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 51 for (i = 0; i < n; i++) 52 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); 53 } 54 55 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 56 void read(STU st[], int n) { 57 int i; 58 FILE *fin; 59 60 fin = fopen("examinee.txt", "r"); 61 if (!fin) { 62 printf("fail to open file\n"); 63 return; 64 } 65 66 for (i = 0; i < n; i++) 67 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 68 69 fclose(fin); 70 } 71 72 // 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数 73 int process(STU st[], int n, STU st_pass[]) { 74 int i,m=0; 75 for(i=0;i<n;i++) 76 { 77 st[i].sum=st[i].objective+st[i].subjective; 78 if(st[i].sum>=60) 79 { 80 strcpy(st[i].result,"通过"); 81 st_pass[m]=st[i]; 82 m++; 83 84 } 85 else 86 strcpy(st[i].result,"不通过"); 87 88 } 89 return m; 90 91 } 92 93 // 把通过考试的考生完整信息写入文件list_pass.txt 94 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 95 void write(STU st[], int n) { 96 FILE *fp; 97 int i; 98 fp=fopen("list_pass.txt","w"); 99 if(fp==NULL) 100 { 101 printf("没打开\n"); 102 return; 103 } 104 fprintf(fp,"%-20s%-20s%-20s%-20s%-20s%-20s\n","准考证号","姓名","客观题得分","操作题得分","总分","结果"); 105 for(i=0;i<n;++i) 106 107 fprintf(fp,"%-20ld%-20s%-20.2f%-20.2f%-20.2f%-20s\n",st[i].id,st[i].name,st[i].objective,st[i].subjective,st[i].sum,st[i].result); 108 }
实验截图:


实验任务6
源代码:
1 #include <stdio.h> 2 #include <time.h> 3 #include<stdlib.h> 4 5 #define TOTAL 80 6 #define WIN 5 7 8 typedef struct { 9 long id; 10 char name[20]; 11 char className[30]; 12 } STU; 13 14 15 int main() { 16 FILE *fp; 17 STU st[80]; 18 int i,count,flag,repeat; 19 int lucky[WIN]; 20 21 22 srand((unsigned)time(NULL)); 23 fp = fopen("list.txt", "r"); 24 if (fp == NULL) { 25 printf("错误:无法打开 list.txt 文件,请确保文件在同目录下。"); 26 27 return 1; 28 } 29 30 for (i = 0; i < TOTAL; i++) { 31 if (fscanf(fp, "%ld %s %s", 32 &st[i].id, 33 st[i].name, 34 st[i].className) != 3) { 35 printf("读取数据失败,第 %d 行格式错误!\n", i + 1); 36 fclose(fp); 37 return 1; 38 } 39 } 40 fclose(fp); 41 42 count = 0; 43 while (count < WIN) { 44 flag = rand() % TOTAL; 45 repeat = 0; 46 47 48 for (i = 0; i < count; i++) { 49 if (lucky[i] == flag) { 50 repeat = 1; 51 break; 52 } 53 } 54 55 56 if (!repeat) { 57 lucky[count] = flag; 58 count++; 59 } 60 } 61 62 63 printf("========== 中奖名单 ==========\n"); 64 printf("%-14s %-10s %-20s\n", 65 "准考证号", "姓名", "班级"); 66 printf("----------------------------------------\n"); 67 68 69 char filename[100]; 70 printf("请输入要保存的文件名:"); 71 scanf("%s", filename); 72 73 74 fp = fopen(filename, "w"); 75 if (fp == NULL) { 76 printf("错误:无法创建文件 %s!\n", filename); 77 return 1; 78 } 79 80 fprintf(fp, "%-14s %-10s %-20s\n", 81 "准考证号", "姓名", "班级"); 82 83 84 for (i = 0; i < WIN; i++) { 85 int idx = lucky[i]; 86 printf("%-14ld %-10s %-20s\n", 87 st[idx].id, 88 st[idx].name, 89 st[idx].className); 90 91 fprintf(fp, "%-14ld %-10s %-20s\n", 92 st[idx].id, 93 st[idx].name, 94 st[idx].className); 95 } 96 97 printf("----------------------------------------\n"); 98 printf("中奖名单已保存到 %s\n", filename); 99 100 fclose(fp); 101 102 system("pause"); 103 return 0; 104 }
实验截图:


浙公网安备 33010602011771号