实验7
实验任务4
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 int main(){ 5 FILE *fp; 6 char ch; 7 int line=0; 8 int char_count=0; 9 fp=fopen("C:\\Users\\Lenovo\\Desktop\\实验7数据文件及部分代码_gbk\\data4.txt","r"); 10 if(!fp){ 11 perror("data4.txt"); 12 return 1; 13 } 14 while((ch=fgetc(fp))!=EOF){ 15 if(ch=='\n'){ 16 line++; 17 } 18 if(ch!=' '&&ch!='\n'&&ch!='\t'){ 19 char_count++; 20 } 21 } 22 if(char_count>0){ 23 line++; 24 } 25 fclose(fp); 26 printf("data4.txt统计结果:\n"); 27 printf("行数:\t\t\t%d\n",line); 28 printf("字符数(不计空白符): %d\n",char_count); 29 30 return 0; 31 }
运行结果截图

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


实验任务6
源代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 100 5 6 int main(){ 7 srand((unsigned int)time(NULL)); 8 char s[N][N]; 9 char st[N][N]; 10 int i,n,a; 11 FILE *fp; 12 fp=fopen("C:\\Users\\Lenovo\\Desktop\\实验7数据文件及部分代码_gbk\\list.txt","r"); 13 if(!fp){ 14 perror("list.txt"); 15 return 1; 16 } 17 18 i=0; 19 while(fgets(s[i],N,fp)!=NULL) 20 ++i; 21 n=i; 22 for(i=0;i<n;i++){ 23 fgets(s[i],N,fp); 24 printf("%s",s[i]); 25 } 26 printf("---------中奖名单-----------\n"); 27 for(i=0;i<5;i++){ 28 a=rand()%n; 29 strcpy(st[i],s[a]); 30 } 31 for(i=0;i<5;i++){ 32 printf("%s",st[i]); 33 } 34 printf("----------保存到文件----------\n"); 35 time_t now=time(NULL); 36 char b[50]; 37 struct tm*local=localtime(&now); 38 strftime(b,sizeof(b),"%Y%m%d.txt",local); 39 fp=fopen(b,"w"); 40 if(!fp){ 41 perror("保存文件失败"); 42 return 1; 43 } 44 for(i=0;i<5;i++){ 45 fprintf(fp,"%s\n",st[i]); 46 47 } 48 fclose(fp); 49 printf("输入文件名:%s\n",b); 50 printf("保存成功!"); 51 52 return 0; 53 }
运行结果截图


浙公网安备 33010602011771号