jsjzy
1 #include<stdio.h> 2 #define N 5 3 #define M 80 4 5 typedef struct 6 { 7 char name[M]; 8 char author[M]; 9 } Book; 10 11 int main() 12 { 13 Book x[N]={{"一九八四","乔治·奥威尔"}, 14 {"美丽新世界","赫胥黎"}, 15 {"昨日的世界","斯蒂芬·茨格威"}, 16 {"万历十五年","黄仁宇"}, 17 {"一只特立独行的猪","王小波"} 18 }; 19 20 int i; 21 22 FILE *fp; 23 24 fp=fopen("data1.txt","w"); 25 if(fp==NULL) 26 { 27 printf("fail to open file\n"); 28 return 1; 29 } 30 31 for(i=0;i<N;i++) 32 { 33 fprintf(fp,"%-20s %-20s\n",x[i].name,x[i].author); 34 printf("%-20s %-20s\n",x[i].name,x[i].author); 35 } 36 37 fclose(fp); 38 39 return 0; 40 }
1.2
#include<stdio.h> #define N 5 #define M 80 typedef struct { char name[M]; char author[M]; }Book; int main() { Book x[N]; int i; FILE *fp; fp=fopen("data1.txt","r"); if(fp==NULL) { printf("fail to open file\n"); return 1; } for(i=0;i<N;i++) { fscanf(fp,"%s %s\n",x[i].name ,x[i].author); printf("%-20s %-20s\n",x[i].name,x[i].author); } fclose(fp); return 0; }
2.1
1 #include<stdio.h> 2 #define N 5 3 #define M 80 4 5 typedef struct 6 { 7 char name[M]; 8 char author[M]; 9 } Book; 10 11 int main() 12 { 13 Book x[N]={{"一九八四","乔治·奥威尔"}, 14 {"美丽新世界","赫胥黎"}, 15 {"昨日的世界","斯蒂芬·茨格威"}, 16 {"万历十五年","黄仁宇"}, 17 {"一只特立独行的猪","王小波"} 18 }; 19 20 int i; 21 22 FILE *fp; 23 24 fp=fopen("data2.dat","wb"); 25 if(fp==NULL) 26 { 27 printf("fail to open file\n"); 28 return 1; 29 } 30 31 fwrite(x,sizeof(Book),N,fp); 32 33 fclose(fp); 34 35 return 0; 36 }
2.2
1 #include<stdio.h> 2 #define N 5 3 #define M 80 4 5 typedef struct 6 { 7 char name[M]; 8 char author[M]; 9 }Book; 10 11 int main() 12 { 13 Book x[N]; 14 int i; 15 16 FILE *fp; 17 18 fp=fopen("data2.dat","rb"); 19 if(fp==NULL) 20 { 21 printf("fail to open file\n"); 22 return 1; 23 } 24 25 fread(x,sizeof(Book),N,fp); 26 for(i=0;i<N;++i) 27 { 28 printf("%-20s %-20s\n",x[i].name,x[i].author); 29 } 30 31 fclose(fp); 32 33 return 0; 34 }
3.2
1 #include<stdio.h> 2 3 int main() 4 { 5 FILE *fin; 6 char ch; 7 int count=0; 8 9 fin =fopen("data3_1.txt","r"); 10 if(fin==NULL) 11 { 12 printf("fail to open data3_1.txt\n"); 13 return 1; 14 } 15 16 while(!feof(fin)) 17 { 18 ch=fgetc(fin); 19 20 if(ch!=' '&&ch!='\n'&&ch!=' ') 21 count++; 22 } 23 24 fclose(fin); 25 printf("data3_1.txt中共包含字符数:%d",count-1); 26 27 return 0; 28 }
5
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 // 函数声明 18 void input(STU s[], int n); 19 void output(STU s[], int n); 20 void process(STU s[], int n); 21 22 int main() 23 { 24 STU stu[N]; 25 26 printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 27 input(stu, N); 28 29 printf("\n对考生信息进行处理: 计算总分,确定等级\n"); 30 process(stu, N); 31 32 printf("\n打印考生完整信息, 并保存到文件中"); 33 output(stu, N); 34 35 return 0; 36 } 37 38 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 39 void input(STU s[], int n) 40 { 41 int i; 42 FILE *fin; 43 44 fin = fopen("examinee.txt", "r"); 45 if (fin == NULL) 46 { 47 printf("fail to open file\n"); 48 exit(0); 49 } 50 51 while (!feof(fin)) 52 { 53 for (i = 0; i < n; i++) 54 fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); 55 } 56 57 fclose(fin); 58 } 59 60 //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 61 // 不仅输出到屏幕上,还写到文本文件result.txt中 62 void output(STU s[], int n) 63 { 64 FILE *fout; 65 int i; 66 67 // 输出到屏幕 68 printf("\n"); 69 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 70 for (i = 0; i < n; i++) 71 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); 72 73 // 保存到文件 74 fout = fopen("result.txt", "w"); 75 if (!fout) 76 { 77 printf("fail to open or create result.txt\n"); 78 exit(0); 79 } 80 81 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 82 83 for (i = 0; i < n; i++) 84 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); 85 86 fclose(fout); 87 } 88 89 // 对考生信息进行处理:计算总分,排序,确定等级 90 void process(STU s[], int n) 91 { 92 STU temp;int i,j; 93 94 for(i=0;i<n;i++) 95 { 96 s[i].sum=s[i].objective+s[i].subjective; 97 } 98 99 for(i=0;i<n;i++) 100 { 101 for(j=0;j<n;j++) 102 { 103 if(s[i].sum>s[j].sum) 104 { 105 temp.id=s[i].id; 106 s[i].id=s[j].id; 107 s[j].id=temp.id; 108 109 strcpy(temp.name,s[j].name); 110 strcpy(s[j].name,s[i].name); 111 strcpy(s[i].name,temp.name); 112 113 temp.objective=s[i].objective; 114 s[i].objective=s[j].objective; 115 s[j].objective=temp.objective; 116 117 temp.subjective=s[i].subjective; 118 s[i].subjective=s[j].subjective; 119 s[j].subjective=temp.subjective; 120 121 temp.sum=s[i].sum; 122 s[i].sum=s[j].sum; 123 s[j].sum=temp.sum; 124 } 125 } 126 } 127 128 for(i=0;i<n;i++) 129 { 130 if(i+1<=n*0.1) 131 { 132 strcpy(s[i].level,"优秀"); 133 } 134 else if(i<=n*0.5) 135 { 136 strcpy(s[i].level,"合格"); 137 } 138 else 139 { 140 strcpy(s[i].level,"不合格"); 141 } 142 } 143 }
6
1 #include<stdio.h> 2 #include<time.h> 3 #include<string.h> 4 5 #define N 5 6 7 typedef struct 8 { 9 long int id; 10 char name[10]; 11 char clname[30]; 12 }STU; 13 14 void pick(STU s[],int n); 15 void input(STU s[], int n); 16 void output(STU *s, int n); 17 18 int main() 19 { 20 int i,k; 21 STU stu[80],lucky[N]; 22 23 input(stu,80); 24 25 srand(time(0)); 26 k=rand()%(80)+1; 27 28 for(i=0;i<N;i++) 29 { 30 lucky[i]=stu[k]; 31 k=rand()%(80)+1; 32 } 33 34 output(lucky,N); 35 36 37 38 } 39 40 void input(STU s[], int n) 41 { 42 int i; 43 FILE *fin; 44 45 fin = fopen("list.txt", "r"); 46 if (fin == NULL) 47 { 48 printf("fail to open list.txt\n"); 49 return 1; 50 } 51 52 for (i = 0; i < n; i++) 53 fscanf(fin, "%ld %s %s", &s[i].id, s[i].name,s[i].clname); 54 55 fclose(fin); 56 } 57 58 void output(STU s[], int n) 59 { 60 int i; 61 FILE *fout; 62 63 fout = fopen("lucky.txt", "w"); 64 65 for (i=0;i<n;i++) 66 printf("%ld\t%s\t%s\n", s[i].id, s[i].name,s[i].clname); 67 68 if (fout == NULL) 69 { 70 printf("fail to create lucky.txt\n"); 71 return 1; 72 } 73 74 for (i = 0; i < n; i++) 75 fprintf(fout, "%ld\t%s\t%s\n", s[i].id, s[i].name,s[i].clname); 76 77 78 fclose(fout); 79 }