实验7
实验任务4
1 #include<stdio.h> 2 #define N 100 3 int main() 4 { 5 FILE *fp; 6 int n=0,ch; 7 fp=fopen("data4.txt","r"); 8 if(fp == NULL) 9 { 10 printf("fail to open file to write\n"); 11 return 0; 12 } 13 char x[N]; 14 int count=0; 15 while(fgets(x, N, fp) != NULL) 16 { 17 count++; 18 for(int i = 0; x[i] != '\0'; i++) 19 { 20 if(x[i] != ' ' && x[i] != '\t' && x[i] != '\n' && x[i] != '\r') 21 { 22 n++; 23 } 24 } 25 } 26 printf("data4.txt统计结果:\n"); 27 printf("行数: %d\n",count); 28 printf("字符数(不计空白符): %d\n",n); 29 30 return 0 ; 31 }

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


实验任务6
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<string.h> 5 struct student 6 { 7 char id[10]; 8 char name[10]; 9 char cla[20]; 10 }; 11 12 int main() 13 { 14 time_t now; 15 char ccc[50]; 16 char filename[50]; 17 srand((unsigned int)time(NULL)); 18 int n,i[5],count=0,j,c; 19 while(count!=5) 20 { 21 i[count]=rand()%80+1; 22 c=0; 23 for(j=0;j<count;++j) 24 { 25 if(i[count]==i[j]) 26 c=1; 27 } 28 if(c==0) 29 { 30 count++; 31 } 32 } 33 FILE *fp; 34 struct student x[80]; 35 fp=fopen("list.txt","r"); 36 if(fp == NULL) 37 { 38 printf("fail to open file to write\n"); 39 return 0; 40 } 41 while(fscanf(fp, "%s%s%s",x[n].id,x[n].name, x[n].cla) != EOF) 42 ++n; 43 fclose(fp); 44 int a,b,t; 45 for(a=0;a<5;++a) 46 { 47 for(b=0;b<4;++b) 48 { 49 if(i[b]>i[b+1]) 50 { 51 t=i[b]; 52 i[b]=i[b+1]; 53 i[b+1]=t; 54 } 55 } 56 } 57 printf("-----------------------2025中奖名单------------------------\n"); 58 for(j = 0; j < 5; ++j) 59 printf("%-15s%-15s%-20s\n", x[i[j]].id, x[i[j]].name, x[i[j]].cla); 60 printf("输入文件名:"); 61 scanf("%s",ccc); 62 strcpy(filename,ccc); 63 fp=fopen(filename,"w"); 64 if(fp == NULL) 65 { 66 printf("fail to open file to write\n"); 67 return 0; 68 } 69 printf("\n文件保存成功"); 70 71 fprintf(fp,"-----------------------2025中奖名单------------------------\n"); 72 for(j = 0; j < 5; ++j) 73 fprintf(fp,"%-15s%-15s%-20s\n", x[i[j]].id, x[i[j]].name, x[i[j]].cla); 74 75 return 0 ; 76 }


1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 struct student 5 { 6 char id[10]; 7 char name[10]; 8 char cla[20]; 9 }; 10 11 int main() 12 { 13 time_t now; 14 char filename[50]; 15 struct tm *local; 16 time(&now); 17 local=localtime(&now); 18 int year = local->tm_year + 1900; 19 int month = local->tm_mon + 1; 20 srand((unsigned int)time(NULL)); 21 int n,i[5],count=0,j,c; 22 while(count!=5) 23 { 24 i[count]=rand()%80+1; 25 c=0; 26 for(j=0;j<count;++j) 27 { 28 if(i[count]==i[j]) 29 c=1; 30 } 31 if(c==0) 32 { 33 count++; 34 } 35 } 36 FILE *fp; 37 struct student x[80]; 38 fp=fopen("list.txt","r"); 39 if(fp == NULL) 40 { 41 printf("fail to open file to write\n"); 42 return 0; 43 } 44 while(fscanf(fp, "%s%s%s",x[n].id,x[n].name, x[n].cla) != EOF) 45 ++n; 46 fclose(fp); 47 sprintf(filename, "%d%d%d.txt",year,month,local->tm_mday); 48 fp=fopen(filename,"w"); 49 if(fp == NULL) 50 { 51 printf("fail to open file to write\n"); 52 return 0; 53 } 54 int a,b,t; 55 for(a=0;a<5;++a) 56 { 57 for(b=0;b<4;++b) 58 { 59 if(i[b]>i[b+1]) 60 { 61 t=i[b]; 62 i[b]=i[b+1]; 63 i[b+1]=t; 64 } 65 } 66 } 67 fprintf(fp,"-----------------------2025中奖名单------------------------\n"); 68 for(j = 0; j < 5; ++j) 69 fprintf(fp,"%-15s%-15s%-20s\n", x[i[j]].id, x[i[j]].name, x[i[j]].cla); 70 printf("-----------------------2025中奖名单------------------------\n"); 71 for(j = 0; j < 5; ++j) 72 printf("%-15s%-15s%-20s\n", x[i[j]].id, x[i[j]].name, x[i[j]].cla); 73 printf("\n文件保存成功"); 74 75 return 0 ; 76 }



浙公网安备 33010602011771号