实验7
实验任务4
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define N 100 5 #define M 80 6 7 void write(); 8 int level(); 9 int character(); 10 11 int main(){ 12 int line,n; 13 write(); 14 line = level(); 15 n = character(); 16 printf("data4.txt统计结果:\n"); 17 printf("行数:\t\t%d\n",line); 18 printf("字符数(不计空白符):\t\t%d\n",n); 19 20 system("pause"); 21 return 0; 22 } 23 24 void write(){ 25 char *ptr[] = {"0123456789-0123456789", 26 "nuist2026", 27 "cosmos galaxy"}; 28 int i,n; 29 FILE *fp; 30 31 fp = fopen("data4.txt","w"); 32 if(fp == NULL){ 33 printf("fail to open file to write\n"); 34 return; 35 } 36 37 n = sizeof(ptr)/sizeof(ptr[0]); 38 39 for(i = 0;i < n;++i){ 40 fputs(ptr[i],fp); 41 fputs("\n",fp); 42 } 43 fclose(fp); 44 } 45 46 int level(){ 47 int i = 0; 48 char ch; 49 50 FILE *fp; 51 fp = fopen("data4.txt","r"); 52 if(fp == NULL){ 53 printf("fail to file to open\n"); 54 return 0; 55 } 56 57 while((ch = fgetc(fp))!= EOF){ 58 if(ch == '\n') 59 i++; 60 } 61 fclose(fp); 62 return i; 63 } 64 65 int character(){ 66 int i = 0; 67 char ch; 68 69 FILE *fp; 70 fp = fopen("data4.txt","r"); 71 if(fp == NULL){ 72 printf("fail to file to open\n"); 73 return -1; 74 } 75 while((ch = fgetc(fp))!= EOF){ 76 if(ch != ' ' && ch != '\t' && ch != '\n') 77 i++; 78 } 79 fclose(fp); 80 return i; 81 }
运行结果截图

实验任务5
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 6 #define N 10 7 8 typedef struct{ 9 long id; 10 char name[20]; 11 float objective; 12 float subjective; 13 float sum; 14 char result[10]; 15 }STU; 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 24 25 STU stu[N],stu_pass[N]; 26 int cnt; 27 double pass_rate; 28 29 printf("从文件读入%d个考生信息:已完成\n", N); 30 read(stu, N); 31 32 printf("\n对考生成绩进行统计:已完成\n"); 33 cnt = process(stu, N, stu_pass); 34 35 printf("\n所有考生完整信息:\n"); 36 output(stu,N); 37 38 printf("\n通过考试的名单写入文件:已完成!\n"); 39 write(stu_pass,cnt); 40 41 pass_rate = 1.0 * cnt / N; 42 printf("\n本次考试等级通过率:%.2f%%\n",pass_rate*100); 43 44 45 system("pause"); 46 return 0; 47 } 48 49 void output(STU st[],int n){ 50 int i; 51 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 52 for(i = 0;i <n;i++) 53 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); 54 55 } 56 57 void read(STU st[], int n){ 58 int i; 59 FILE *fin; 60 61 fin = fopen("examinee.txt","r"); 62 if(!fin){ 63 printf("fail to open file\n"); 64 return; 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 76 for(i = 0;i < n;i++){ 77 st[i].sum = st[i].objective + st[i].subjective; 78 if(st[i].sum >= 60){ 79 m++; 80 strcpy(st[i].result,"通过"); 81 st_pass[m] = st[i]; 82 }else{ 83 strcpy(st[i].result,"未通过"); 84 } 85 } 86 87 return m; 88 } 89 90 // 把通过考试的考生完整信息写入文件list_pass.txt 91 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 92 void write(STU st[],int n){ 93 int i; 94 FILE *fin; 95 96 fin = fopen("list_pass.txt","w"); 97 if(fin == NULL){ 98 printf("fail to open file\n"); 99 return; 100 } 101 fprintf(fin,"准考证号\t\t姓名\t客观题得分\t\t操作题得分\t\t总分\t\t结果\n"); 102 for(i = 0;i < n;i++){ 103 fprintf(fin,"%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); 104 } 105 106 fclose(fin); 107 }
屏幕输出截图

文件list_pass.txt内容截图

实验任务6
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<string.h> 5 6 #define N 80 7 #define M 5 8 9 typedef struct{ 10 char id[20]; 11 char name[20]; 12 char cls[30]; 13 }Student; 14 15 int read(Student stu[]); 16 void random(int sum,int lift[]); 17 void write(Student stu[],int idx[],int num,char filename); 18 19 int main(){ 20 Student stu[N]; 21 int i,n; 22 int sum = read(stu); 23 int idx[M]; 24 char filename[N]; 25 26 random(sum,idx); 27 28 printf("----------中奖名单----------\n"); 29 for(i = 0;i < M;i++){ 30 n = idx[i]; 31 printf("%s\t%s\t%s\n",stu[n].id,stu[n].name,stu[n].cls); 32 } 33 34 printf("----------保存到文件----------\n"); 35 printf("输入文件名:"); 36 scanf("%s",filename); 37 strcat(filename,".txt"); 38 39 write(stu,idx,M,filename); 40 41 printf("保存成功\n"); 42 43 system("pause"); 44 return 0; 45 } 46 47 int read(Student stu[]){ 48 int cnt = 0; 49 50 FILE *fp; 51 fp = fopen("list.txt","r"); 52 if(fp == NULL){ 53 printf("fail to open to file\n"); 54 return 1; 55 } 56 57 while(fscanf(fp,"%s %s %s",stu[cnt].id,stu[cnt].name,stu[cnt].cls) == 3){ 58 cnt++; 59 } 60 61 fclose(fp); 62 return cnt; 63 64 } 65 66 void random(int sum,int lift[]){ 67 int m[N] = {0}; 68 int i = 0,j; 69 srand((unsigned)time(NULL)); 70 while(i < M){ 71 j = rand()%sum; 72 if(m[j] == 0){ 73 m[j] = 1; 74 lift[i++] = j; 75 } 76 } 77 } 78 79 void write(Student stu[],int idx[],int num,char filename[]){ 80 int i; 81 int n; 82 83 FILE *fp; 84 fp = fopen(filename,"w"); 85 if(fp == NULL){ 86 printf("fail to open to file\n"); 87 return; 88 } 89 90 for(i = 0; i < num;i++){ 91 n = idx[i]; 92 fprintf(fp,"%s\t%s\t%s\t",stu[n].id,stu[n].name,stu[n].cls); 93 } 94 95 fclose(fp); 96 }
运行结果截图

浙公网安备 33010602011771号