实验7
任务4
源代码
1 #include<stdio.h> 2 int read1(){ 3 int count=0; 4 5 FILE *fp; 6 fp=fopen("datal4.txt","r"); 7 8 if(fp==NULL){ 9 printf("fail to open file to read \n"); 10 return 0; 11 } 12 13 char c; 14 15 while(!feof(fp)){ 16 c=fgetc(fp); 17 if(c!=' '&&c!='\n'&&c!='\t'){ 18 count++; 19 } 20 } 21 fclose(fp); 22 return count; 23 } 24 int read2(){ 25 int i; 26 int line=0; 27 28 FILE *fp; 29 fp=fopen("datal4.txt","r"); 30 31 if(fp==NULL){ 32 printf("fail to open file to read \n"); 33 return 0; 34 } 35 36 char c; 37 38 while(!feof(fp)){ 39 c=fgetc(fp); 40 if(c=='\n'){ 41 line++; 42 } 43 } 44 if(c!='\n'&&line==0){ 45 line=1; 46 } 47 fclose(fp); 48 return line; 49 } 50 int main(){ 51 52 printf("datal4.txt统计结果:\n"); 53 54 printf("行数:%-20d\n",read2()); 55 56 printf("字符数(不计空白符):%-20d\n",read1()); 57 58 return 0; 59 }
结果

任务5
源代码
1 #include<stdio.h> 2 #include<string.h> 3 #define N 10 4 5 typedef struct{ 6 long id; 7 char name[20]; 8 float objective; 9 float subjective; 10 float sum; 11 char result[10]; 12 }STU; 13 14 void read(STU st[],int n); 15 void write(STU st[],int n); 16 void output(STU st[],int n); 17 int process(STU st[],int n,STU st_pass[]); 18 19 int main(){ 20 STU stu[N],stu_pass[N]; 21 int cnt; 22 double pass_rate; 23 24 printf("从文件读入%d个考生信息...\n",N); 25 read(stu,N); 26 27 printf("\n对考生成绩进行统计...\n"); 28 cnt=process(stu,N,stu_pass); 29 30 printf("\n通过考试的名单:\n"); 31 output(stu,N); 32 write(stu,N); 33 34 35 pass_rate=1.0*cnt/N; 36 printf("\n本次等级考试通过率:%.2f%%\n",pass_rate*100); 37 38 return 0; 39 } 40 41 void read(STU st[],int n){ 42 int i; 43 FILE *fp; 44 fp=fopen("examinee.txt","r"); 45 if(!fp){ 46 printf("fail to open file \n"); 47 return ; 48 } 49 50 while(!feof(fp)){ 51 for(i=0;i<n;i++) 52 fscanf(fp,"%ld %s %f %f",&st[i].id,st[i].name, 53 &st[i].objective,&st[i].subjective); 54 } 55 fclose(fp); 56 } 57 58 void write(STU st[],int n){ 59 int i; 60 61 FILE *fp; 62 63 fp=fopen("list_pass.txt","w"); 64 if(!fp){ 65 printf("fail to open file \n"); 66 return ; 67 } 68 69 for(i=0;i<n;i++) 70 if(st[i].objective+st[i].subjective>=60) 71 fprintf(fp,"%1d\t\t%8s\t\t%.2f\t\t%.2f\t\t%.2f\t\t%-15s \n", 72 st[i].id,st[i].name,st[i].objective,st[i].subjective, 73 st[i].sum,st[i].result); 74 75 fclose(fp); 76 } 77 void output(STU st[],int n){ 78 int i; 79 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 80 for(i=0;i<n;i++) 81 printf("%1d\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%-15s\n", 82 st[i].id,st[i].name, 83 st[i].objective,st[i].subjective, 84 st[i].sum,st[i].result); 85 86 } 87 int process(STU st[],int n,STU st_pass[]){ 88 int i,count=0; 89 for(i=0;i<n;i++){ 90 st[i].sum=st[i].objective+st[i].subjective; 91 if(st[i].sum>=60){ 92 strcpy(st[i].result,"通过"); 93 count++; 94 } 95 else 96 strcpy(st[i].result,"不通过"); 97 } 98 return count; 99 }
结果


任务7
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<time.h> 5 6 #define N 100 7 8 typedef struct{ 9 long id; 10 char name[20]; 11 char class1[N]; 12 }stu; 13 14 stu st[N]; 15 16 int read(); 17 void output(); 18 19 int main(){ 20 printf("---------随机抽点名单---------\n"); 21 if(read()==0) 22 return 1; 23 24 printf("----------保存到文件-----------\n"); 25 int count=read(); 26 output(st,count); 27 printf("保存成功!"); 28 29 return 0; 30 } 31 int read(){ 32 int i=0,j,n; 33 int number=0; 34 35 FILE *fp; 36 fp=fopen("list.txt","r"); 37 38 if(fp==NULL){ 39 printf("fail to open file to read \n"); 40 return; 41 } 42 43 i = 0; 44 while(!feof(fp)) { 45 number = fscanf(fp,"%19ld %19s %49s",&st[i].id,st[i].name,st[i].class1); 46 if(number !=3) 47 break; 48 i++; 49 } 50 n = i; 51 52 53 for(j=0;j<i;j++){ 54 printf("%19ld %19s %49s\n",st[j].id,st[j].name,st[j].class1); 55 } 56 57 58 return i; 59 } 60 void output(const stu s[],int n){ 61 int i,j; 62 63 srand(time(NULL)); 64 65 stu t[N]; 66 67 68 for(i=n-1;i>0;i--){ 69 int j=rand()%(i+1); 70 stu t=st[i]; 71 st[i]=st[j]; 72 st[j]=t; 73 } 74 75 char txt[N]; 76 77 printf("请输入文件名:"); 78 scanf("%s",txt); 79 80 FILE *fp2=fopen(txt,"w"); 81 82 if(fp2==NULL){ 83 printf("无法创建文件"); 84 return ; 85 } 86 87 for(i=0;i<5&&i<n;i++){ 88 fprintf(fp2,"%19ld %19s %49s\n",st[i].id,st[i].name,st[i].class1); 89 printf("%19ld %19s %49s\n",st[i].id,st[i].name,st[i].class1); 90 } 91 92 fclose(fp2); 93 }
结果


浙公网安备 33010602011771号