4.实验4
1 #include<stdio.h> 2 #include<string.h> 3 #include <string.h> 4 #define M 999 5 int main(){ 6 char x[M]; 7 FILE *fp; 8 int len,t=0,a=0; 9 if((fp=fopen("E:\\data4.txt","r"))==NULL){ 10 return 0; 11 } 12 while(!feof(fp)&&!ferror(fp)){ 13 fgets(x,M,fp); 14 len=strlen(x); 15 if(x[len-1]=='\n'){ 16 x[len-1]='\0'; 17 len--; 18 } 19 for(int i=0;i<len;i++){ 20 if(x[i]==' ') 21 a++; 22 } 23 if(len==0) continue; 24 t+=len-a; 25 } 26 printf("data4.txt中包含字符数:%d",t); 27 fclose(fp); 28 return 0; 29 30 31 }
5.实验5
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; 11 float subjective; 12 float sum; 13 char ans[10]; 14 } STU; 15 16 void finput(STU st[], int n); 17 void foutput(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 finput(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); 34 foutput(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 void output(STU st[], int n) { 43 int i; 44 45 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 46 for (i = 0; i < n; i++) 47 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].ans); 48 } 49 50 void finput(STU st[], int n) { 51 int i; 52 FILE *fin; 53 54 fin = fopen("examinee.txt", "r"); 55 if (fin == NULL) { 56 printf("fail to open file\n"); 57 exit(0); 58 } 59 60 while (!feof(fin)) { 61 for (i = 0; i < n; i++) 62 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 63 } 64 65 fclose(fin); 66 } 67 68 void foutput(STU s[], int n) { 69 FILE *fout; 70 int i; 71 fout = fopen("list_pass.txt", "w"); 72 if (!fout) { 73 printf("fail to open or create list_pass.txt\n"); 74 exit(0); 75 } 76 77 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 78 79 for (i = 0; i < n; i++) 80 fprintf(fout, "%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].ans); 81 82 fclose(fout); 83 } 84 85 int process(STU st[], int n, STU st_pass[]) { 86 int i,j=0; 87 for(i=0;i<n;i++){ 88 st[i].sum=st[i].objective+st[i].subjective; 89 if(st[i].sum>=60){ 90 strcpy(st[i].ans,"pass"); 91 st_pass[j]=st[i]; 92 j++;} 93 else 94 strcpy(st[i].ans,"fail");} 95 return j; 96 }
6.实验6
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 100 6 typedef struct{ 7 long int id; 8 char name[N]; 9 char class[N]; 10 }STU; 11 void finput(STU st[N],int n); 12 void foutput(STU st[N]); 13 int main(){ 14 int i,a; 15 STU stu[N],lucky[N]; 16 finput(stu,N); 17 for(i=0;i<5;i++){ 18 a=rand()%80; 19 lucky[i]=stu[a]; 20 } 21 foutput(lucky); 22 for(i=0;i<5;i++) 23 printf("%ld\t\t%s\t\t%s\n",lucky[i].id,lucky[i].name,lucky[i].class); 24 return 0; 25 26 } 27 void finput(STU st[N],int n){ 28 int i; 29 FILE *fin; 30 fin=fopen("list.txt","r"); 31 if(fin==NULL){ 32 printf("fail to open file\n"); 33 exit(0); 34 } 35 while(!feof(fin)){ 36 for(i=0;i<n;i++) 37 fscanf(fin,"%ld %s %s",&st[i].id,st[i].name,st[i].class); 38 } 39 fclose(fin); 40 } 41 void foutput(STU st[N]){ 42 int i; 43 FILE *fout; 44 fout=fopen("lucky.txt","w"); 45 if(!fout){ 46 printf("fail to open or create lucky.txt"); 47 exit(0); 48 } 49 for(i=0;i<5;i++) 50 fprintf(fout,"%ld\t%s\t%s\n",st[i].id,st[i].name,st[i].class); 51 fclose(fout); 52 }
标签:include,int,st,STU,实验,pass,t% From: https://www.cnblogs.com/TangZhiXuan/p/17901657.html