实验7

任务4

源代码:

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

image

任务5

源代码:

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

image

任务6

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<string.h>
 5 
 6 #define N 80
 7 #define M 100
 8 
 9 typedef struct{
10     char info[M];
11 }stduent;
12 
13 int main(){
14     int count=0;
15     int t[N]={0};
16     int i,j,idx,repeat;
17 
18     FILE *fp=fopen("D:\\软件信息\\实验7数据文件及部分代码_gbk\\实验7数据文件及部分代码_gbk\\list.txt", "r");
19     if(fp==NULL){
20         printf("打开失败");
21         return 1;
22     }
23     stduent stus[N];
24     while(fgets(stus[count].info,M,fp) != NULL && count < N)
25     {
26         stus[count].info[strcspn(stus[count].info,"\n")]='\0';
27     if(strlen(stus[count].info) > 0)
28         count++;
29     }
30     fclose(fp);
31 
32     srand((unsigned)time(NULL));
33     int winidx[5];
34     for(i=0;i<5;i++){
35         do{
36             repeat = 0;
37             idx = rand()%count;
38             for(j=0;j<i;j++)
39                 if(winidx[j]==idx){
40                     repeat = 1;
41                     break;
42                 }
43         }while(repeat);
44         winidx[i]=idx;
45 }
46     printf("--------中奖名单-------\n");
47     for(i=0;i<5;i++){
48         puts(stus[winidx[i]].info);
49     }
50     printf("--------保存在文件------\n");
51     char name[N];
52     printf("输入文件名:");
53     scanf("%s",name);
54     FILE *fw=fopen(name, "w");
55     if(fw == NULL){
56         printf("创建失败");
57         return 2;
58     }
59     for(i=0;i<5;i++)
60         fprintf(fw,"%s\n", stus[winidx[i]].info);
61     fclose(fw);
62     printf("保存成功!\n");
63     system("pause");
64     return 0;
65 }

image

 

posted @ 2026-06-23 21:29  帕茹克  阅读(5)  评论(0)    收藏  举报