任务4
1 #include<stdio.h> 2 3 void read_h(); 4 void read_z(); 5 6 int main(){ 7 printf("data4.txt统计结果:\n"); 8 printf("行数:"); 9 read_h(); 10 printf("字符数(不计算空白符):"); 11 read_z(); 12 return 0; 13 } 14 15 16 17 void read_h(){ 18 FILE *fp; 19 int t=0; 20 char ch; 21 22 fp=fopen("data4.txt","r"); 23 if(!fp) { 24 printf("fail to open file to read\n"); 25 return; 26 } 27 while(!feof(fp)) { 28 ch = fgetc(fp); 29 if(ch =='\n'||ch==EOF) 30 t++; 31 } 32 printf("%d\n",t); 33 fclose(fp); 34 } 35 36 void read_z(){ 37 FILE *fp; 38 int t=0; 39 char ch; 40 41 fp=fopen("data4.txt","r"); 42 if(!fp) { 43 printf("fail to open file to read\n"); 44 return; 45 } 46 while(!feof(fp)) { 47 ch = fgetc(fp); 48 if(ch != '\n'&&ch!=' '&&ch!=' '&&ch!=EOF) 49 t++; 50 } 51 printf("%d",t); 52 fclose(fp); 53 }
任务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 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU st[], int n) { 74 FILE *fp; 75 int i; 76 77 fp=fopen("list_pass.txt","w"); 78 if(!fp) { 79 printf("fail to open file to write\n"); 80 return; 81 } 82 fprintf(fp,"准考证号 姓名 客观题得分 操作题得分 总分 结果\n"); 83 for (i = 0; i < N; i++) 84 if(st[i].sum>=60) 85 fprintf(fp,"%-20ld%-10s\t%-20.2f%-20.2f%-20.2f%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 86 87 88 fclose(fp); 89 } 90 int process(STU st[], int n, STU st_pass[]) { 91 int i,t=0; 92 93 for(i=0;i<N;i++){ 94 st[i].sum=st[i].objective +st[i].subjective ; 95 if(st[i].sum>=60){ 96 strcpy(st[i].result,"通过"); 97 st_pass[t].id=st[i].id; 98 strcpy(st_pass[t].name,st[i].name); 99 st_pass[t].objective=st[i].objective; 100 st_pass[t].subjective=st[i].subjective; 101 st_pass[t].sum=st[i].sum; 102 strcpy(st_pass[t].result,st[i].result); 103 t++; 104 } 105 else 106 strcpy(st[i].result,"不通过"); 107 } 108 return t; 109 }
任务6.1
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<time.h> 5 #define N 80 6 #define M 5 7 8 typedef struct { 9 long id; 10 char name[20]; 11 char class[50]; 12 } STU; 13 14 void read(STU st[]); 15 void random(STU st[],STU st_x[]); 16 void write(STU st_x[],char x[]); 17 18 int main(){ 19 STU stu[N],stu_x[M]; 20 char t[80]; 21 22 read(stu); 23 printf("---------------随机抽点名单--------------\n"); 24 random(stu,stu_x); 25 printf("----------保存到文件----------\n"); 26 printf("输入文件名:"); 27 scanf("%s",t); 28 write(stu_x,t); 29 printf("保存成功!"); 30 return 0; 31 } 32 void read(STU st[]) { 33 int i; 34 FILE *fin; 35 36 fin = fopen("list.txt", "r"); 37 if (!fin) { 38 printf("fail to open file\n"); 39 return; 40 } 41 42 while (!feof(fin)) { 43 for (i = 0; i < N; i++) 44 fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class); 45 } 46 47 fclose(fin); 48 } 49 50 void random(STU st[],STU st_x[]){ 51 int x[M]; 52 int i,j,c; 53 54 srand(time(NULL)); 55 for(i=0;i<M;i++){ 56 x[i]=rand()%80+1; 57 for(j=0;j<i;j++){ 58 if(x[j]==x[i]){ 59 i--; 60 continue; 61 } 62 } 63 } 64 for(i=0;i<M;i++){ 65 c=x[i]; 66 st_x[i].id=st[c].id; 67 strcpy(st_x[i].name,st[c].name); 68 strcpy(st_x[i].class,st[c].class); 69 } 70 for(i=0;i<M;i++) 71 printf("%-20ld%-10s\t%-10s\n", st_x[i].id, st_x[i].name, st_x[i].class); 72 } 73 74 void write(STU st_x[],char t[]){ 75 FILE *fp; 76 int i; 77 78 fp=fopen(t,"w"); 79 if(!fp) { 80 printf("fail to open file to write\n"); 81 return; 82 } 83 for(i=0;i<M;i++) 84 fprintf(fp,"%-20ld%-10s\t%-10s\n", st_x[i].id, st_x[i].name, st_x[i].class); 85 86 fclose(fp); 87 }
任务6.2
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<time.h> 5 #define N 80 6 #define M 5 7 8 typedef struct { 9 long id; 10 char name[20]; 11 char class[50]; 12 } STU; 13 14 void read(STU st[]); 15 void random(STU st[],STU st_x[]); 16 void write(STU st_x[]); 17 18 int main(){ 19 STU stu[N],stu_x[M]; 20 21 read(stu); 22 printf("---------------随机抽点名单--------------\n"); 23 random(stu,stu_x); 24 printf("----------保存到文件----------\n"); 25 printf("输入文件名:"); 26 write(stu_x); 27 printf("保存成功!"); 28 return 0; 29 } 30 void read(STU st[]) { 31 int i; 32 FILE *fin; 33 34 fin = fopen("list.txt", "r"); 35 if (!fin) { 36 printf("fail to open file\n"); 37 return; 38 } 39 40 while (!feof(fin)) { 41 for (i = 0; i < N; i++) 42 fscanf(fin, "%ld %s %s", &st[i].id, st[i].name, st[i].class); 43 } 44 45 fclose(fin); 46 } 47 48 void random(STU st[],STU st_x[]){ 49 int x[M]; 50 int i,j,c; 51 52 srand(time(NULL)); 53 for(i=0;i<M;i++){ 54 x[i]=rand()%80+1; 55 for(j=0;j<i;j++){ 56 if(x[j]==x[i]){ 57 i--; 58 continue; 59 } 60 } 61 } 62 for(i=0;i<M;i++){ 63 for(j=0;j<M-i;j++){ 64 if(st[x[j+1]].id<st[x[j]].id){ 65 c=x[j]; 66 x[j]=x[j+1]; 67 x[j+1]=c; 68 } 69 } 70 } 71 for(i=0;i<M;i++){ 72 c=x[i]; 73 st_x[i].id=st[c].id; 74 strcpy(st_x[i].name,st[c].name); 75 strcpy(st_x[i].class,st[c].class); 76 } 77 78 for(i=0;i<M;i++) 79 printf("%-20ld%-10s\t%-10s\n", st_x[i].id, st_x[i].name, st_x[i].class); 80 } 81 82 void write(STU st_x[]){ 83 FILE *fp; 84 int i; 85 char t[20]; 86 time_t rawtime; 87 struct tm *ti; 88 89 time(&rawtime); 90 ti=localtime(&rawtime); 91 if(ti!=NULL) 92 strftime(t,20,"%Y%m%d",ti); 93 strcat(t,".txt"); 94 fp=fopen(t,"w"); 95 if(!fp) { 96 printf("fail to open file to write\n"); 97 return; 98 } 99 for(i=0;i<M;i++) 100 fprintf(fp,"%-20ld%-10s\t%-10s\n", st_x[i].id, st_x[i].name, st_x[i].class); 101 102 fclose(fp); 103 }标签:stu,int,void,st,STU,实验,printf From: https://www.cnblogs.com/ljhlll/p/18639358