实验7
任务4
代码
1 #include<stdio.h> 2 3 4 int main(){ 5 int hang=1,zi=0; 6 char ch; 7 FILE *fp; 8 fp=fopen("data4.txt","r"); 9 if(fp==NULL){ 10 printf("fail to open file to read\n"); 11 return 0; 12 } 13 while((ch=fgetc(fp))!=EOF){ 14 if(ch=='\n') 15 hang++; 16 if((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!='\r')) 17 zi++; 18 } 19 20 fclose(fp); 21 printf("data4.txt统计结果:\n行数:%d\n字符数(不计空白符):%d",hang,zi); 22 return 0; 23 }
结果

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

任务6
代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 80 5 #define n 5 6 typedef struct{ 7 long id;//学号 8 char name[20];//姓名 9 char clas[20];//班级 10 }STU; 11 12 void read(STU stu[]); 13 void write(STU stu[]); 14 void output(STU stu[]); 15 void random(STU stu[],STU stu1[]); 16 17 int main(){ 18 STU stu[N],stu1[N]; 19 read(stu); 20 random(stu,stu1); 21 printf("------------随机抽点名单----------\n"); 22 output(stu1); 23 printf("\n"); 24 printf("------------保存到文件---------\n"); 25 write(stu1); 26 printf("输入文件名:202483290470.txt\n") ; 27 printf("保存成功!"); 28 return 0; 29 } 30 31 void read(STU stu[]){ 32 int i; 33 FILE *fin; 34 35 fin = fopen("list.txt", "r"); 36 if (!fin) { 37 printf("fail to open file\n"); 38 return; 39 } 40 41 while (!feof(fin)) { 42 for (i = 0; i < N; i++) 43 fscanf(fin, "%ld %s %s", &stu[i].id,&stu[i].name,&stu[i].clas); 44 } 45 46 fclose(fin); 47 } 48 49 void random(STU stu[],STU stu1[]){ 50 srand(time(NULL)); 51 for(int i=0;i<n;i++){ 52 int index=rand()%N; 53 stu1[i]=stu[index]; 54 } 55 } 56 57 void output(STU stu[]){ 58 int i; 59 for(i=0;i<n;i++){ 60 printf("%ld %s %s\n", stu[i].id,stu[i].name,stu[i].clas); 61 } 62 } 63 64 void write(STU stu[]){ 65 FILE *fp; 66 fp=fopen("202483290470.txt","w"); 67 if(!fp){ 68 printf("fail to open file\n"); 69 return ; 70 } 71 fwrite(stu,sizeof(STU),n,fp); 72 fclose(fp); 73 }
结果

浙公网安备 33010602011771号