实验7
task4
程序
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(){ 5 FILE *fp; 6 int s1=0,s2=0; 7 char c; 8 fp=fopen("data4.txt","r"); 9 10 while(!feof(fp)){ 11 c=fgetc(fp); 12 if(c=='\n') 13 s1++; 14 if((c>='a' && c<='z')||(c>='A' && c<='Z')||(c>='0' && c<='9')||c=='-') 15 s2++; 16 } 17 printf("data4.txt统计结果:\n行数:\t\t\t%d\n字符数(不计空白字符):\t%d\n",s1+1,s2); 18 fclose(fp); 19 system("pause"); 20 return 0; 21 }
运行结果

task5
程序
1 #include <stdio.h> 2 #include <string.h> 3 #include<stdlib.h> 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 system("pause"); 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 s[], int n) { 74 FILE *fout; 75 int i; 76 77 fout = fopen("list_pass.txt", "w"); 78 if (!fout) { 79 printf("fail to create file\n"); 80 return; 81 } 82 83 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); 84 for (i = 0; i < n; i++) { 85 fprintf(fout, "%-10d%-10s%-15.2f%-15.2f%-15.2f%-10s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 86 } 87 fclose(fout); 88 } 89 90 int process(STU st[], int n, STU st_pass[]) { 91 int i, pass_cnt = 0; 92 for (i = 0; i < n; i++) { 93 94 st[i].sum = st[i].objective + st[i].subjective; 95 97 if (st[i].sum >= 60) { 98 strcpy(st[i].result, "通过"); 99 st_pass[pass_cnt] = st[i]; \ 100 pass_cnt++; 101 } 102 else { 103 strcpy(st[i].result, "不通过"); 104 } 105 } 106 return pass_cnt; 107 }
运行结果


task6
程序
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 80 5 6 typedef struct{ 7 int id; 8 char name[N]; 9 char c[N]; 10 }Student; 11 12 void random(Student x1[],Student x2[],int n); 13 void output(Student x[],int n); 14 void read(Student x[],int n); 15 void write(Student x[],int n,char f[]); 16 17 int main(){ 18 Student stu[N],luck[N]; 19 char f[N]; 20 read(stu,N); 21 random(stu,luck,N); 22 printf("-----------中奖名单------------\n"); 23 output(luck,5); 24 printf("-----------保存到文件----------\n"); 25 printf("输入文件名:"); 26 scanf("%s",&f); 27 write(stu,5,f); 28 system("pause"); 29 } 30 31 void read(Student x[],int n) 32 { 33 FILE *fin; 34 int i; 35 fin=fopen("list.txt","r"); 36 while(!feof(fin)){ 37 for(i=0;i<n;i++) 38 fscanf(fin,"%d %s %s",&x[i].id,x[i].name,x[i].c);} 39 fclose(fin); 40 } 41 void random(Student x1[],Student x2[],int n) 42 { 43 int m,i; 44 srand(time(NULL)); 45 for(i=0;i<5;i++){ 46 m=rand()%(n-1+1); 47 x2[i]=x1[m];} 48 } 49 void output(Student x[],int n) 50 { 51 int i; 52 for(i=0;i<n;i++) 53 printf("%-15d%-10s%-30s\n",x[i].id,x[i].name,x[i].c); 54 } 55 void write(Student x[],int n,char f[]) 56 { 57 FILE *fp; 58 int i; 59 fp=fopen(f,"w"); 60 for(i=0;i<n;i++) 61 fprintf(fp,"%-15d%-10s%-30s\n",x[i].id,x[i].name,x[i].c); 62 fclose(fp); 63 printf("保存成功!\n"); 64 }
运行结果


浙公网安备 33010602011771号