实验7
实验任务4
#include <stdio.h> #include <string.h> #define N 100 int main(){ int i,j,line,n; char t; char s[N][N]; FILE *fp; fp=fopen("data4.txt","r"); if(!fp){ perror("data4.txt"); return 1; } i=0; while(fgets(s[i],N,fp)!=NULL) ++i; line=i; fclose(fp); n=0; for(i=0;i<line;++i){ for(j=0;s[i][j]!='\0';++j){ if(s[i][j]!=' '&&s[i][j]!='\n'&&s[i][j]!='\t'){ ++n; } } } printf("date4.txt统计结果:\n"); printf("行数:%d\n",line); printf("字符数(不计空白符):%d",n); return 0; }

实验任务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 for (i = 0; i < n; i++){ 64 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 65 st[i].sum= st[i].objective+st[i].subjective; 66 if(st[i].sum>=60) 67 strcpy(st[i].result,"通过"); 68 else 69 strcpy(st[i].result,"未通过"); 70 } 71 fclose(fin); 72 } 73 74 // 把通过考试的考生完整信息写入文件list_pass.txt 75 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 76 void write(STU s[], int n) { 77 int i,j; 78 79 STU pass[N]; 80 FILE *fp; 81 82 j=0; 83 84 fp=fopen("list_pass.txt","w"); 85 for(i=0;i<N;++i){ 86 if(s[i].sum>=60){ 87 pass[j]=s[i]; 88 ++j; 89 } 90 } 91 for(i=0;i<j;++i){ 92 fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 93 pass[i].id,pass[i].name,pass[i].objective, pass[i].subjective,pass[i].sum,pass[i].result); 94 } 95 96 97 fclose(fp); 98 } 99 100 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 101 int process(STU st[], int n, STU st_pass[]) { 102 int k,i; 103 k=0; 104 105 for(i=0;i<N;++i){ 106 if(st[i].sum>=60){ 107 ++k; 108 } 109 } 110 111 return k; 112 }

实验任务6
1 #include <stdio.h> 2 #include <math.h> 3 #include <stdlib.h> 4 #include <time.h> 5 #include <string.h> 6 7 #define N 100 8 #define M 5 9 int main(){ 10 int i,n,t; 11 int gone[N]={0}; 12 char s[N][N],fname[80],hit[N][N]; 13 FILE *fp,*fq; 14 15 fp=fopen("list.txt","r"); 16 if(!fp){ 17 perror("list.txt"); 18 return 1; 19 } 20 21 i=0; 22 while(fgets(s[i],N,fp)!=NULL) 23 ++i; 24 n=i; 25 26 srand(time(0)); 27 for(i=0;i<M;){ 28 t=rand()%n; 29 if(gone[t]) continue; 30 31 gone[t]=1; 32 strcpy(hit[i],s[t]); 33 ++i; 34 } 35 for(i=0;i<M;++i){ 36 printf(hit[i]); 37 } 38 39 40 printf("Enter Filename\n"); 41 scanf("%s",fname); 42 fq=fopen(fname,"w"); 43 if(!fq){ 44 perror(fname); 45 return 1; 46 } 47 for(i=0;i<M;++i){ 48 fprintf(fq,fname,hit[i]); 49 } 50 51 fclose(fq); 52 fclose(fp); 53 return 0; 54 }


浙公网安备 33010602011771号