首页 > 编程语言 >实验七 文件应用编程

实验七 文件应用编程

时间:2024-06-17 15:32:08浏览次数:20  
标签:printf 文件 int void 编程 st STU 实验 pass

实验任务4

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int count = 0;
 5     int ch;
 6     FILE *fp;
 7 
 8     fp = fopen("data4.txt", "r");
 9     if (fp == NULL) {
10         printf("fail to open file\n");
11         return 1; 
12     }
13 
14     while ((ch = fgetc(fp)) != EOF) {
15         if (!(ch == ' ' || ch == '\n' || ch == '\t')) {
16             count++;
17         }
18     }
19 
20     printf("data4.txt中共包含字符数(不记空白符):%d\n", count);
21 
22     fclose(fp);
23 
24     return 0;
25 }

实验任务5

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long int id;
  9     char name[20];
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char ans[10];       // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void finput(STU st[], int n);
 18 void foutput(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     finput(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu_pass, cnt);      // 输出到屏幕
 35     foutput(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].ans);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void finput(STU st[], int n) {
 55     int i;
 56     FILE *fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (fin == NULL) {
 60         printf("fail to open file\n");
 61         exit(0);
 62     }
 63 
 64     while (!feof(fin)) {
 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 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void foutput(STU s[], int n) {
 75     FILE *fout;
 76     int i;
 77     
 78     // 保存到文件 
 79     fout = fopen("list_pass.txt", "w");
 80     if (!fout) {
 81         printf("fail to open or create list_pass.txt\n");
 82         exit(0);
 83     }
 84     
 85     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 86 
 87     for (i = 0; i < n; i++)
 88         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
 89 
 90     fclose(fout);
 91 }
 92 
 93 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 94 int process(STU st[], int n, STU st_pass[]) {
 95    // 待补足
 96    // ×××
 97     int i,count = 0;
 98 
 99     for (i = 0; i < n; i++){
100         st[i].sum = st[i].objective + st[i].subjective;
101         if(st[i].sum >= 60){
102             strcpy(st[i].ans,"pass");
103             st_pass[count++] = st[i];
104         }
105         else
106             strcpy(st[i].ans,"fail");
107     }
108 
109     return count;
110 }

实验任务6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define N 100
 6 
 7 struct STU {
 8     long int id;
 9     char name[N];
10     char cla[N];
11 } st[N], lucky[N];
12 
13 void func1(void);
14 int func2(void);
15 void func3(void);
16 
17 int main() {
18     int j, n;
19     func1(); 
20 
21 
22     srand((unsigned)time(NULL)); 
23 
24     for (j = 0; j < 5; j++) {
25         n = func2();
26         lucky[j] = st[n];
27         printf("%ld\t %s\t %s\t\n", lucky[j].id, lucky[j].name, lucky[j].cla);
28     }
29 
30     func3(); 
31 
32     getchar(); 
33 
34     return 0;
35 }
36 
37 void func1(void) {
38     int i;
39     FILE *fp;
40 
41     fp = fopen("list.txt", "r");
42 
43     if (fp == NULL) {
44         printf("Failed to open file\n");
45         return;
46     }
47 
48     i = 0;
49     while (fscanf(fp, "%ld\t %s\t %s\t", &st[i].id, st[i].name, st[i].cla) == 3 && i < N) {
50         ++i;
51     }
52 
53     fclose(fp);
54 }
55 
56 int func2(void) {
57     return rand() % 80; 
58 }
59 
60 void func3(void) {
61     int j;
62     FILE *fout;
63 
64     fout = fopen("lucky.txt", "w");
65 
66     if (fout == NULL) {
67         printf("Failed to open file\n");
68         return;
69     }
70 
71     for (j = 0; j < 5; j++) { 
72         fprintf(fout, "%ld %s %s\n", lucky[j].id, lucky[j].name, lucky[j].cla); 
73     }
74 
75     fclose(fout);
76 }

 

标签:printf,文件,int,void,编程,st,STU,实验,pass
From: https://www.cnblogs.com/silverbullet4869/p/18245597

相关文章

  • Win11系统提示找不到clretwrc.dll文件的解决办法
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个clretwrc.dll文件(挑选合适的版本文件)把它......
  • Win11系统提示找不到coadmin.dll文件的解决办法
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个coadmin.dll文件(挑选合适的版本文件)把它放......
  • Win11系统提示找不到commig.dll文件的解决办法
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个commig.dll文件(挑选合适的版本文件)把它放......
  • Win11系统提示找不到coloradapterclient.dll文件的解决办法
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个coloradapterclient.dll文件(挑选合适的版本......
  • codeArts Snap:辅助你的编程神器
    CodeArtsSnap:YourUltimateProgrammingAssistant**IntroductiontoCodeArtsSnap**CodeArtsSnapisaninnovativetooldesignedtostreamlineandenhanceyourprogrammingexperience.Whetheryouareabeginnerlearningtheropesoranexperienceddevelo......
  • 大模型辅助编程助手:『小浣熊 Raccoon』 如何使用?
    认识RaccoonRaccoon(Raccoonis Another Code CO-pilOt Navigator)是基于AI的代码助手,是商汤科技发布基于商汤自研大语言模型的智能编程助手,代码小浣熊Raccoon支持Python、Java、JavaScript、C++、Go、SQL等30+主流编程语言和VSCode、IntelliJIDEA等主流IDE......
  • EasyRecovery数据恢复软件电脑的超级救星,无论是误删除的文件、格式化的硬盘还是病毒攻
    EasyRecovery数据恢复软件,是我近期用过最神奇的产品之一了!它就像是电脑的超级救星,无论是误删除的文件、格式化的硬盘还是病毒攻击,都能轻松搞定!让我给大家详细介绍一下这个神器吧!EasyRecovery数据恢复软件的功能真的是非常强大!它可以扫描并恢复各种类型的文件,包括照片、视频、......
  • 申瓯通信 在线录音管理系统 download 任意文件读取漏洞复现
    0x01产品简介申瓯通信在线录音管理系统是一款功能强大的录音管理解决方案,旨在满足公司、集团单位在区域分布下对录音数据的集中管理需求。该系统基于SOC1900、SOC1600等系列录音盒开发,是一个多线路、多设备、多区域的录音统一管理平台。该系统将各个分点的录音盒的录音统一集......
  • AI辅助编程2 AI编码技术的工作原理
    2AI编码技术的工作原理在本章中,我们将揭开人工智能辅助编程工具的神秘面纱,了解它们的工作原理。我们将简要回顾一下历史,体验一下变换器模型和LLM,并演示OpenAIPlayground。然后,我们将获得一些关于如何评估LLM的建议。掌握这项强大的技术能做什么、不能做什么,将为在实际软件项目......
  • 【PL理论】(29) OOP:面向对象编程 | 案例研究:C++ 中的类 | 继承 | 继承和指针 | Object
    ......