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