实验七

任务四

运行代码

 1 #include <stdio.h>
 2 int main()
 3 {
 4     FILE *fp;
 5     int line=1,str=0;
 6     char ch;
 7     fp=fopen("D:\\C练习\\实验课\\实验7\\实验7数据文件及部分代码_gbk\\data4.txt","r");
 8     if(fp==NULL){
 9     printf("fail to open file to write\n");
10     return 0;
11     }
12     while((ch=fgetc(fp))!=EOF)
13     {
14         if(ch=='\n')
15         line++;
16         if(ch!=' ' && ch !='\t' && ch!='\n')
17         str++;
18     }
19     fclose(fp);
20     printf("data4.txt统计结果:\n");
21     printf("行数:%d\n",line);
22     printf("字符数(不计空白符):%d\n",str);
23     return 0;
24 }

运行结果

屏幕截图 2026-06-22 192625

 

任务5

运行代码

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

 

运行结果

屏幕截图 2026-06-22 193653

 

任务6

运行代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 80
#define M 5
typedef struct{
    long id;
    char name[10];
    char cls[20];
} STU;
int main()
{
    STU all[N];
    STU lucky[M];
    FILE *fin,*fp;
    char  outfilename[N];
    int i,j,k=0,randIdx;
    int flag[N]={0};
    fin=fopen("D:\\C练习\\实验课\\实验7\\实验7数据文件及部分代码_gbk\\list.txt","r");
    if(!fin){
        printf("fail to open file\n");
        return 0;
    }
    while(fscanf(fin,"%ld%s%s",&all[k].id,all[k].name,all[k].cls)!=EOF)
    {
        k++;
    }
    fclose(fin);
    srand((unsigned)time(NULL));
    for(i=0;i<5;++i)
    {
        do{
            randIdx=rand()%80;
        }while(flag[randIdx]==1);
        flag[randIdx]=1;
        lucky[i]=all[randIdx];
    }
    printf("------------中奖名单------------\n");
    for(i=0;i<5;++i)
    {
        printf("%ld\t%s\t%s\n",lucky[i].id,lucky[i].name,lucky[i].cls);
    }
    printf("------------保存到文件-----------\n");
    printf("输入文件名:");
    scanf("%s",outfilename);
    fp=fopen(outfilename,"w");
    if(!fp)
    {
        printf("fail to open file\n");
        return 0;
    }
    for(i=0;i<5;++i)
    {
        fprintf(fp,"%ld\t%s\t%s\n",lucky[i].id,lucky[i].name,lucky[i].cls);
    }
    fclose(fp);
    printf("文件保存成功!");
    return 0;
}

运行结果

屏幕截图 2026-06-22 192322

 

posted @ 2026-06-22 19:39  永遠の夢  阅读(14)  评论(0)    收藏  举报