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

实验7 文件应用编程

时间:2023-12-18 14:12:49浏览次数:23  
标签:fp 文件 random int 编程 st 实验 numbers pass

四、实验结论

4. 实验任务4

task4.c

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

运行结果

5. 实验任务5

task5.c

  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, N);      // 输出到屏幕
 35     foutput(stu, N);    // 输出到文件
 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 
 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 96 int process(STU st[], int n, STU st_pass[]) {
 97     // 待补足
 98     // ×××
 99     int sum = 0;
100 
101     for (int i = 0; i < n; i++) {
102         st[i].sum = st[i].subjective + st[i].objective;
103         if (st[i].sum >= 60.0) {
104             strcpy(st[i].ans, "pass");
105             st_pass[sum] = st[i];
106             sum++;
107         }
108         else {
109             strcpy(st[i].ans, "fail");
110         }
111     }
112     return sum;
113 }

运行结果

6. 实验任务6(已实现必做与选作)

task6.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 typedef struct {
 6     char id[20];
 7     char name[20];
 8     char clazz[20];
 9 } stu;
10 
11 void swap(int *a, int *b) {
12     int temp = *a;
13     *a = *b;
14     *b = temp;
15 }
16 
17 void bubbleSort(int arr[]) {
18     for (int i = 0; i < 5 - 1; i++) {
19         for (int j = 0; j < 5 - i - 1; j++) {
20             if (arr[j] > arr[j + 1]) {
21                 swap(&arr[j], &arr[j + 1]);
22             }
23         }
24     }
25 }
26 
27 int is_duplicate(int number, int *array, int size) {
28     for (int i = 0; i < size; ++i) {
29         if (array[i] == number) {
30             return 1;  // 数字已经存在,是重复的
31         }
32     }
33     return 0;  // 数字不存在,不是重复的
34 }
35 
36 int main() {
37     srand(time(NULL));
38 
39     int random_numbers[5];
40     int generated_numbers = 0;
41 
42     while (generated_numbers < 5) {
43         int new_random = rand() % 80;  // 假设生成 0 到 79 之间的随机数
44 
45         if (!is_duplicate(new_random, random_numbers, generated_numbers)) {
46             random_numbers[generated_numbers] = new_random;
47             generated_numbers++;
48         }
49     }
50     bubbleSort(random_numbers);
51     
52     FILE *fp;
53     fp = fopen("list.txt", "r");
54 
55     if (fp == NULL) {
56         printf("fail to open file\n");
57         exit(0);
58     }
59     
60     stu st[80];
61     while (!feof(fp)) {
62         for (int i = 0; i < 80; i++)
63             fscanf(fp, "%s %s %s", st[i].id, st[i].name, st[i].clazz);
64     }
65 
66     fclose(fp);
67     
68     time_t t;
69     struct tm *tm_info;
70     char filename[80];
71 
72     time(&t);
73     tm_info = localtime(&t);
74 
75     strftime(filename, sizeof(filename), "%Y%m%d.txt", tm_info);
76 
77     fp = fopen(filename, "w");
78     
79     for (int i = 0; i < 5; i++) {
80         printf("%s %s %s\n", st[random_numbers[i]].id, st[random_numbers[i]].name, st[random_numbers[i]].clazz);
81         fprintf(fp, "%s %s %s\n", st[random_numbers[i]].id, st[random_numbers[i]].name, st[random_numbers[i]].clazz);
82     }
83     
84     fclose(fp);
85     
86     return 0;
87 }

运行结果

 

标签:fp,文件,random,int,编程,st,实验,numbers,pass
From: https://www.cnblogs.com/Cr2O3/p/17911059.html

相关文章

  • Git|Git推送代码到远端时发现文件冲突,该怎么办?(二)
    背景多人使用同一个远端仓库开发项目,这时候直接推送代码到同一远端仓库,然后就会出现一系列的文件修改冲突情况,接下来我们具体情况具体分析一下。本文的主要围绕着下面两种情况展开的,在阅读之前可以先自己思考一下问题的答案是什么?不同的人修改同一分支相同的文件的相同区域,你会怎么......
  • c# 更改快捷方式文件图标
    c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标c#更改快捷方式文件图标///<summary>///更改快捷方式文件图标///</summa......
  • c# 获取用户桌面选择的文件
     引用COM组件 Shell32 Shell32.ShellFolderViewdesktopFolderView;inthwnd;Shell32.ShelliShell=newShell32.Shell();SHDocVw.ShellWindowsiWindows=iShell.Windows();SHDocVw.InternetExploreriDesktop=iWindows.FindWindowSW(0,null,8,outhwnd,......
  • 网络编程之IO模型
    我们讨论网络编程中的IO模型时,需要先明确什么是IO以及IO操作为什么在程序开发中是很关键的一部分,首先我们看下IO的定义。IO的定义IO操作(Input/Output操作)是计算机系统中的一种重要操作,用于数据的输入和输出,通常涉及到计算机与外部设备(如硬盘、网卡、键盘、鼠标、打印机等)之间的......
  • Java | 多线程并发编程CountDownLatch实践
    关注:CodingTechWork引言  在一次数据割接需求中,数据需要通过编程的方式进行转移割接到新平台,此时若串行化方式,无疑会拉锯此次战斗,所以首当其冲要使用并发编程来降低割接时长。  本次主要考虑使用CountDownLatch工具类进行并发编程的控制。CountDownLatch概述  在并发编程过程......
  • 实验六
    #include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutput(B......
  • 48、Flink DataStream API 编程指南(1)- DataStream 入门示例
    文章目录Flink系列文章一、FlinkDataStreamAPI编程指南1、DataStream是什么?2、Flink程序剖析3、第一个完整示例4、入门示例1)、maven依赖2)、代码3)、验证本文介绍了FlinkDataStreamAPI的编程指南第一部分,即介绍flink的source、transformation和sink的编程过程以及入门示例......
  • 48、Flink DataStream API 编程指南(3)- 完整版
    文章目录Flink系列文章一、FlinkDataStreamAPI编程指南1、DataStream是什么?2、Flink程序剖析3、第一个完整示例4、入门示例1)、maven依赖2)、代码3)、验证5、DataSources1)、基于文件2)、基于套接字3)、基于集合4)、自定义6、DataStreamTransformations7、DataSinks8、Iteratio......
  • 实验七
    实验任务4源代码1#include<stdio.h>2#defineN1003#defineM1004intmain(){5FILE*fp;6chara[N][M];7inti,j=0,k=0,s=0;8fp=fopen("data4.txt","r");9if(fp==NULL){10printf("false......
  • (三十三)C#编程基础复习——C#接口(interface)
    接口可以看做是一个约定,其中定义了类或结构体继承接口后需要实现功能,接口的特点如下:接口是一个引用类型,通过接口可以实现多重继承;接口中只能声明“抽象”成员,所以不能直接对接口进行实例化;接口中可以包含方法、属性、事件、索引器等成员;接口名称一般习惯使用字母“I”作为开......