Experiment7
Task4:
1 #include <stdio.h> 2 #include <stdio.h> 3 #include <ctype.h> 4 5 int main() { 6 FILE *file = fopen("data4.txt", "r"); 7 if (file == NULL) { 8 printf("无法打开文件 data4.txt\n"); 9 return 1; 10 } 11 12 int line_count = 0; 13 int char_count = 0; 14 int c; 15 int in_line = 0; 16 17 while ((c = fgetc(file)) != EOF) { 18 if (c == '\n') { 19 line_count++; 20 in_line = 0; 21 } else { 22 in_line = 1; 23 if (!isspace(c)) { 24 char_count++; 25 } 26 } 27 } 28 if (in_line) { 29 line_count++; 30 } 31 32 fclose(file); 33 printf("data4.txt统计结果:\n"); 34 printf("行数:%d\n", line_count); 35 printf("字符数(不计空白符):%d\n", char_count); 36 37 return 0; 38 }

Task5:
1 #include <stdio.h> 2 #include <string.h> 3 #define N 10 4 typedef struct { 5 long id; // 准考证号 6 char name[20]; // 姓名 7 float objective; // 客观题得分 8 float subjective; // 操作题得分 9 float sum; // 总分 10 char result[10]; // 考试结果 11 } STU; 12 // 函数声明 13 void read(STU st[], int n); 14 void write(STU st[], int n); 15 void output(STU st[], int n); 16 int process(STU st[], int n, STU st_pass[]); 17 int main() { 18 STU stu[N], stu_pass[N]; 19 int cnt; 20 double pass_rate; 21 printf("从文件读入%d个考生信息...\n", N); 22 read(stu, N); 23 printf("\n对考生成绩进行统计...\n"); 24 cnt = process(stu, N, stu_pass); 25 printf("\n通过考试的名单:\n"); 26 output(stu, N); // 输出所有考生完整信息到屏幕 27 write(stu, N); // 输出考试通过的考生信息到文件 28 pass_rate = 1.0 * cnt / N; 29 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 30 return 0; 31 } 32 // 把所有考生完整信息输出到屏幕上 33 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 34 void output(STU st[], int n) { 35 int i; 36 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 37 for (i = 0; i < n; i++) 38 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, 39 st[i].objective, st[i].subjective, st[i].sum, st[i].result); 40 } 41 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 42 void read(STU st[], int n) { 43 int i; 44 FILE *fin; 45 fin = fopen("examinee.txt", "r"); 46 if (!fin) { 47 printf("fail to open file\n"); 48 return; 49 } 50 for (i = 0; i < n; i++) 51 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, 52 &st[i].subjective); 53 fclose(fin); 54 } 55 void write(STU s[],int n){ 56 int i; 57 FILE *fout; 58 fout = fopen("list_pass.txt","w"); 59 if(!fout){ 60 printf("fail to creat file\n"); 61 return; 62 } 63 fprintf(fout,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 64 for(i=0;i<n;i++){ 65 fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 66 s[i].id, 67 s[i].name, 68 s[i].objective, 69 s[i].subjective, 70 s[i].sum, 71 s[i].result); 72 } 73 fclose(fout); 74 printf("通过名单已成功写入文件 list_pass.txt\n"); 75 } 76 int process(STU st[],int n,STU st_pass[]){ 77 int i; 78 int pass_count=0; 79 for(i=0;i<n;i++){ 80 st[i].sum=st[i].objective+st[i].subjective; 81 if(st[i].sum>=60){ 82 strcpy(st[i].result,"通过"); 83 st_pass[pass_count]=st[i]; 84 pass_count++; 85 } 86 else{ 87 strcpy(st[i].result,"未通过"); 88 } 89 } 90 return pass_count; 91 }

Task6:
#include <stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define N 100 #define M 5 int main(){ char s[N][N]; char hit[M][N]; int has_hit[N]={0}; FILE *fin,*fout; int i,n,lucky_i,cnt=0; char filename[80]; fin = fopen("list.txt","r"); if(!fin){ perror("list.txt"); return 1; } i=0; while(fgets(s[i],N ,fin)!=NULL){ ++i;} n=i; srand(time(0)); cnt=0; for(i=0;i<M;++i){ lucky_i=rand()%n; if(has_hit[lucky_i]) continue; has_hit[lucky_i]=1; strcpy(hit[i],s[lucky_i]); cnt++; } printf("中奖名单:\n"); for(i=0;i<M;++i) printf("%s",hit[i]); printf("Enter fileName:"); gets(filename); fout=fopen(filename,"w"); if(!fout){ perror(filename); return 1; } for(i=0;i<M;++i) fprintf(fout,"%s",hit[i]); fclose(fin); fclose(fout); return 0; }



浙公网安备 33010602011771号