实验5
作者:互联网
1 #include <stdio.h> 2 3 #define N 5 4 #define M 80 5 6 typedef struct 7 { 8 char name[M]; 9 char author[M]; 10 }BOOK; 11 12 int main() 13 { 14 BOOK x[N]={{"一九八四","乔治·奥威尔"},{"美丽新世界","赫胥黎"},{"昨日的世界","斯提芬·茨威格"},{"万历十五年","黄仁宇"},{"一只特立独行的猫","王小波"}}; 15 int i; 16 17 FILE *fp; 18 19 fp=fopen("data1.txt","w"); 20 21 if(fp==NULL) 22 { 23 printf("fail to open file\n"); 24 return 1; 25 } 26 27 for(i=0;i<N;i++) 28 { 29 fprintf(fp,"%-20s %-20s\n",x[i].name,x[i].author); 30 printf("%-20s %-20s\n",x[i].name,x[i].author); 31 } 32 33 fclose(fp); 34 35 return 0; 36 }
task1-2
1 #include <stdio.h> 2 3 #define N 5 4 #define M 80 5 6 typedef struct 7 { 8 char name[M]; 9 char author[M]; 10 }BOOK; 11 12 int main() 13 { 14 BOOK x[N]; 15 int i; 16 17 FILE *fp; 18 19 fp=fopen("data1.txt","r"); 20 21 if(fp==NULL) 22 { 23 printf("fail to open file\n"); 24 return 1; 25 } 26 27 for(i=0;i<N;i++) 28 { 29 fscanf(fp,"%s %s\n",x[i].name,x[i].author); 30 printf("%-20s %-20s\n",x[i].name,x[i].author); 31 } 32 33 fclose(fp); 34 35 return 0; 36 }
task2-1
1 #include <stdio.h> 2 3 #define N 5 4 #define M 80 5 6 typedef struct 7 { 8 char name[M]; 9 char author[M]; 10 }BOOK; 11 12 int main() 13 { 14 BOOK x[N]={{"一九八四","乔治·奥威尔"},{"美丽新世界","赫胥黎"},{"昨日的世界","斯提芬·茨威格"},{"万历十五年","黄仁宇"},{"一只特立独行的猫","王小波"}}; 15 int i; 16 17 FILE *fp; 18 19 fp=fopen("data2.dat","wb"); 20 21 if(fp==NULL) 22 { 23 printf("fail to open file\n"); 24 return 1; 25 } 26 27 fwrite(x,sizeof(BOOK),N,fp); 28 29 fclose(fp); 30 31 return 0; 32 }
task2-2
1 #include <stdio.h> 2 3 #define N 5 4 #define M 80 5 6 typedef struct 7 { 8 char name[M]; 9 char author[M]; 10 }BOOK; 11 12 int main() 13 { 14 BOOK x[N]; 15 int i; 16 17 FILE *fp; 18 19 fp=fopen("data2.dat","rb"); 20 21 if(fp==NULL) 22 { 23 printf("fail to open file\n"); 24 return 1; 25 } 26 27 fread(x,sizeof(BOOK),N,fp); 28 29 for(i=0;i<N;i++) 30 printf("%-20s %-20s\n",x[i].name,x[i].author); 31 32 fclose(fp); 33 34 return 0; 35 }
task3
1 #include <stdio.h> 2 3 int main() 4 { 5 FILE *fin,*fout; 6 char ch; 7 8 fin=fopen("data3_1.txt","r"); 9 10 if(fin==NULL) 11 { 12 printf("fail to open data3_1.txt\n"); 13 return 1; 14 } 15 16 fout=fopen("data3_2.txt","w"); 17 18 if(fin==NULL) 19 { 20 printf("fail to open data3_2.txt\n"); 21 return 1; 22 } 23 24 while(!feof(fin)) 25 { 26 ch=fgetc(fin); 27 28 if(ch>='a'&&ch<='z') 29 ch-=32; 30 31 fputc(ch,fout); 32 } 33 34 fclose(fin); 35 fclose(fout); 36 37 return 0; 38 }
task5
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct 8 { 9 long int id; 10 char name[20]; 11 float objective; 12 float subjective; 13 float sum; 14 char level[10]; 15 } STU; 16 17 void input(STU s[], int n); 18 void output(STU s[], int n); 19 void process(STU s[], int n); 20 21 int main() 22 { 23 STU stu[N]; 24 25 printf("浠从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 26 input(stu, N); 27 28 printf("\n对考生信息进行处理: 计算总分,确定等级\n"); 29 process(stu, N); 30 31 printf("\n打印考生完整信息, 并保存到文件中"); 32 output(stu, N); 33 34 return 0; 35 } 36 37 void input(STU s[], int n) 38 { 39 int i; 40 FILE *fin; 41 42 fin = fopen("examinee.txt", "r"); 43 if (fin == NULL) 44 { 45 printf("fail to open file\n"); 46 exit(0); 47 } 48 49 while (!feof(fin)) 50 { 51 for (i = 0; i < n; i++) 52 fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); 53 } 54 55 fclose(fin); 56 } 57 58 void output(STU s[], int n) 59 { 60 FILE *fout; 61 int i; 62 63 printf("\n"); 64 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 65 for (i = 0; i < n; i++) 66 printf("%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].level); 67 68 fout = fopen("result.txt", "w"); 69 if (!fout) 70 { 71 printf("fail to open or create result.txt\n"); 72 exit(0); 73 } 74 75 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 76 77 for (i = 0; i < n; i++) 78 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].level); 79 80 fclose(fout); 81 } 82 83 void process(STU s[], int n) 84 { 85 int i,j; 86 STU temp; 87 char level[4][10]={"优秀","合格","不合格"}; 88 89 for(i=0;i<n;i++) 90 s[i].sum=s[i].objective+s[i].subjective; 91 92 for(i=0;i<n-1;i++) 93 for(j=n-1;j>i;j--) 94 if(s[j].sum>s[j-1].sum) 95 { 96 temp=s[j]; 97 s[j]=s[j-1]; 98 s[j-1]=temp; 99 } 100 101 for(i=1;i<=n;i++) 102 { 103 if(i<=0.1*n) 104 strcpy(s[i-1].level,level[0]); 105 else if(i<=0.5*n) 106 strcpy(s[i-1].level,level[1]); 107 else strcpy(s[i-1].level,level[2]); 108 } 109 }
task6
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define N 80 7 #define M 5 8 9 typedef struct 10 { 11 long int id; 12 char name[20]; 13 char classes[20]; 14 } STU; 15 16 void input(STU s[], int n); 17 void output(STU s[], int n); 18 void process(STU s[], int n, STU z[], int m); 19 20 int main() 21 { 22 STU stu[N],lucky[M]; 23 24 input(stu, N); 25 26 process(stu, N, lucky, M); 27 28 output(lucky, M); 29 30 return 0; 31 } 32 33 void input(STU s[], int n) 34 { 35 int i; 36 FILE *fin; 37 38 fin = fopen("list.txt", "r"); 39 if (fin == NULL) 40 { 41 printf("fail to open file\n"); 42 exit(0); 43 } 44 45 while (!feof(fin)) 46 { 47 for (i = 0; i < n; i++) 48 fscanf(fin, "%ld %s %s", &s[i].id, s[i].name, s[i].classes); 49 } 50 51 fclose(fin); 52 } 53 54 void output(STU s[], int n) 55 { 56 FILE *fout; 57 int i; 58 59 printf("\n"); 60 printf("学号\t\t姓名\t班级\n"); 61 for (i = 0; i < n; i++) 62 printf("%ld\t%s\t%s\n", s[i].id, s[i].name, s[i].classes); 63 64 fout = fopen("lucky.txt", "w"); 65 if (!fout) 66 { 67 printf("fail to open or create lucky.txt\n"); 68 exit(0); 69 } 70 71 fprintf(fout, "学号\t\t姓名\t班级\n"); 72 73 for (i = 0; i < n; i++) 74 fprintf(fout, "%ld\t%s\t%s\n", s[i].id, s[i].name, s[i].classes); 75 76 fclose(fout); 77 } 78 79 void process(STU s[], int n, STU z[], int m) 80 { 81 srand(time(NULL)); 82 int num[m], i, j; 83 84 for(i = 0; i < m; i++) 85 { 86 num[i] = rand() % (n-1); 87 for(j = i; j > 0; j--) 88 if(num[j-1] == num[i]) 89 i--; 90 } 91 for(i = 0; i < m; i++) 92 { 93 j=num[i]; 94 z[i]=s[j]; 95 } 96 }
标签:fp,int,char,STU,实验,include,fin 来源: https://www.cnblogs.com/tamotamo/p/16350335.html