实验7

任务4

源代码

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main() {
 5     FILE *fp;
 6     char fname[] = "data4.txt";
 7     char ch;
 8     int lines = 0;
 9     int characters = 0;
10 
11     
12     fp = fopen(fname, "r");
13     if (fp == NULL) {
14         perror("fail to open the file to read\n");
15         return 0;
16     }
17 
18     
19     while (!feof(fp)) {
20         ch = fgetc(fp);
21         if (ch != ' ' && ch!= '\n' && ch != '\t') {
22             characters++;
23         }
24         if (ch == '\n') {
25             lines++;
26         }
27     }
28 
29     --characters;
30     
31     fclose(fp);
32 
33     
34     printf("data4.txt统计结果:\n");
35     printf("行数: %d\n", lines + 1);
36     printf("字符数(不计空白符): %d\n", characters);
37 
38     return 0;
39 }

 

任务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) == NULL) {
 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     int i;
 75     FILE *fp;
 76     fp=fopen("D:\\st2_pass.txt","w");
 77     if(!fp){
 78         printf("fail to open file\n");
 79         return;
 80     }
 81     fprintf(fp,"%-20s%-20s%-20s%-20s%-30s%-30s\n","准考证号","姓名","客观题得分","操作题得分","总分","结果");
 82     for(i=0;i<n;++i){
 83         if(s[i].sum>=60)
 84             fprintf(fp,"%-20ld\t%-10s\t%-20.2f\t%-20.2f\t%-20.2f\t%-20s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].result);
 85     }
 86     fclose(fp);
 87 
 88 }
 89 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 90 int process(STU st[], int n, STU st_pass[]) {
 91     int i,j=0;
 92     for(i=0;i<n;++i)
 93         st[i].sum=st[i].objective+st[i].subjective;
 94     for(i=0;i<n;++i)
 95         if(st[i].sum>=60){
 96             strcpy(st[i].result,"通过");
 97             st_pass[j]=st[i];
 98             j+=1;
 99     }
100     else
101         strcpy(st[i].result,"未通过");
102 
103     return j;
104 }

 

 

任务6

源代码

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<string.h>
 5 
 6 typedef struct{
 7     long id;
 8     char name[20];
 9     char class[20];
10 }STU;
11 
12 void read(STU st[]){
13     FILE *fp;
14     fp = fopen("list.txt", "r");
15     if(fp == NULL){
16         printf("fail to open the file\n");
17         return;
18     }
19     
20     int i, number;
21     while(!feof(fp)){
22         for(i = 0; i < 80; i++){
23             number = fscanf(fp, "%ld %s %s", &st[i].id, st[i].name, st[i].class);
24             if(number != 3)
25                 break;
26         }
27     }
28     fclose(fp);
29 }
30 
31 int main(){
32     int i,temp,count = 0;
33     int choice[5];
34     char s[50];
35     STU st[80];
36     
37     read(st);
38     
39     srand(time(NULL));
40     while(count < 5)
41     {
42         int sign = 1;
43         temp = rand() % 80;
44         for(i = 0;i<count;i++)
45         {
46             if(count == choice[i])
47             {
48                 sign=0;
49                 break;
50             }
51         }
52         if(sign != 0)
53         {
54             choice[i]=temp;
55             count++;
56         }
57     }
58     
59     printf("------------随机抽点名单------------\n");
60     for (i = 0; i < 5; i++) {
61         printf("%ld %-6s %s\n", st[choice[i]].id, st[choice[i]].name, st[choice[i]].class);
62     }
63     printf("------------保存到文件------------\n输入文件名:");
64     scanf("%s", s);
65     char list[100];
66     strcat(list, "D:\\");
67     strcat(list, s);
68     FILE *fp = fopen(list, "w");
69     if (fp == NULL) {
70         printf("fail to open file \n");
71         return;
72     }
73     for (i = 0; i < 5; i++) {
74         fprintf(fp, "%ld %-6s %s\n", st[choice[i]].id, st[choice[i]].name, st[choice[i]].class);
75     }
76     fclose(fp);
77     printf("保存成功!\n");
78     return 0;    
79 }

 

 

posted @ 2024-12-28 17:10  苏刘梁  阅读(30)  评论(0)    收藏  举报