实验7 文件应用编程

news/2024/9/21 20:26:42

4. 实验任务4:

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

5. 实验任务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. 实验任务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 %s %s", &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\t %s\t %s\t\n", lucky[j].id, lucky[j].name, lucky[j].cla);
73     }
74 
75     fclose(fout);
76 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/47224.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

电商现状的简单分析和后续的思考

目录前言我的经营策略商品特点商业模式:找大客户B2B不适合现在的电商目标用户不同流量费用过于昂贵我的破解方法:地推+自媒体地推自媒体 前言 我大概是从24年3月份开始搞电商的,开的是淘宝店,简单说一下我的感受 我的经营策略 商品特点 我家是做五金阀门的,所以我的商品的…

Java大文件上传、分片上传、多文件上传、断点续传、上传文件minio、分片上传minio等解决方案

根据不同场景使用不同方案进行实现尤为必要。通常开发过程中,文件较小,直接将文件转化为字节流上传到服务器,但是文件较大时,用普通的方法上传,显然效果不是很好,当文件上传一半中断再次上传时,发现需要重新开始,这种体验不是很爽,下面介绍几种好一点儿的上传方式。这…

CentOS 7安装Docker,并进行docker加速,拉取镜像

# step 1: 安装必要的一些系统工具 sudo yum install -y yum-utils device-mapper-persistent-data lvm2 # Step 2: 添加软件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3: 更新并安装 Docker-CE sud…

XAMPP Windows PHP-CGI 代码执行漏洞(CVE-2024-4577) | Goby漏洞预警

漏洞描述:PHP是一种在服务器端执行的脚本语言,在 PHP 的 8.3.8 版本之前存在命令执行漏洞,由于 Windows 的 “Best-Fit Mapping” 特性,在处理查询字符串时,非ASCII字符可能被错误地映射为破折号(-),导致命令行参数解析错误,当 php_cgi 运行在Windows平台上,且代码页为繁体…

SQL Server中如何把 科学记数法字符串 变为 数字字符串

当我们将Excel的数据导入SQL Server时,如果用SQL Server的字符串类型(VARCHAR和NVARCHAR等)来存储从Excel导入的数字,有时候这些数字会变成科学记数法字符串(例如1.23E+10实际上是12300000000)。 这并不是我们想要的结果,我们希望从Excel导入的数字还是数字字符串,而不…

探索虚拟线程:原理与实现

虚拟线程的引入与优势 在Loom项目之前,Java虚拟机(JVM)中的线程是通过java.lang.Thread类型来实现的,这些线程被称为平台线程。 然而,平台线程的创建和维护在资源使用上存在显著的开销。首先,创建成本不菲,因为每当操作系统需要创建一个新的平台线程时,它必须分配大量的…

玩转Spring状态机

说起Spring状态机,大家很容易联想到这个状态机和设计模式中状态模式的区别是啥呢?没错,Spring状态机就是状态模式的一种实现,在介绍Spring状态机之前,让我们来看看设计模式中的状态模式。 1. 状态模式 状态模式的定义如下: 状态模式(State Pattern)是一种行为型设计模式…

使用Framebuffer改造SPI_OLED驱动

使用Framebuffer改造SPI_OLED驱动本文是IMX6ULL开发板spi OLED驱动学习笔记,方便后面查看时快速的回顾,而不需要一点点的看视频 视频地址:https://www.bilibili.com/video/BV1Yb4y1t7Uj?p=144&spm_id_from=pageDriver&vd_source=1d93d6a5e22d4b223c6c3ac4f5727eb8 …