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

实验7 文件应用编程

时间:2023-12-18 14:55:39浏览次数:26  
标签:fp 文件 name int 编程 st 实验 t% id

1. 实验任务1

【验证性实验】

2. 实验任务2

【验证性实验】

3. 实验任务3

【验证性实验】

4. 实验任务4

task4源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main(){
 5     FILE *fp;
 6     int t=0;
 7     char a;
 8     
 9     fp=fopen("D:/c语言/实验7/data4.txt.txt","r");
10     
11     if (fp==NULL){
12         printf("fail to open\n");
13         system("pause");
14         return 1;
15     }
16 
17     while(1){
18         a=fgetc(fp);
19         if(a==EOF)
20             break;
21         else if(a==' '||a=='\n')
22             continue;
23         else
24             t++;
25     }
26 
27     printf("data4.txt中共包含字符数(不计空白符):%d\n",t);
28 
29     fclose(fp);
30     
31     system("pause");
32     return 0;
33 }

task4运行截图:

 

5. 实验任务5

task5源代码:

  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     system("pause");
 41     return 0;
 42 }
 43 
 44 // 把通过考试的考生完整信息输出到屏幕上
 45 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 46 void output(STU st[], int n) {
 47     int i;
 48     
 49     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 50     for (i = 0; i < n; i++)
 51         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);
 52 }
 53 
 54 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 55 void finput(STU st[], int n) {
 56     int i;
 57     FILE *fin;
 58 
 59     fin = fopen("D:/c语言/实验7/examinee.txt", "r");
 60     if (fin == NULL) {
 61         printf("fail to open file\n");
 62         exit(0);
 63     }
 64 
 65     while (!feof(fin)) {
 66         for (i = 0; i < n; i++)
 67             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 68     }
 69 
 70     fclose(fin);
 71 }
 72 
 73 // 把通过考试的考生完整信息写入文件list_pass.txt
 74 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 75 void foutput(STU s[], int n) {
 76     FILE *fout;
 77     int i;
 78     
 79     // 保存到文件 
 80     fout = fopen("D:/c语言/实验7/list.txt", "w");
 81     if (!fout) {
 82         printf("fail to open or create list_pass.txt\n");
 83         exit(0);
 84     }
 85     
 86     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 87 
 88     for (i = 0; i < n; i++)
 89         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);
 90 
 91     fclose(fout);
 92 }
 93 
 94 
 95 
 96 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 97 int process(STU st[], int n, STU st_pass[]) {
 98     FILE *fp;
 99     int i,num=0;
100 
101     fp=fopen("D:/c语言/实验7/examinee.txt","r");
102 
103     if (fp==NULL){
104         printf("fail to open\n");
105         system("pause");
106         return 1;
107 }
108 
109     for(i=0;i<n;i++){
110         st[i].sum = st[i].objective + st[i].subjective;
111         if(st[i].sum>=60){
112             strcpy(st[i].ans,"pass");
113             num++;}
114         else
115             strcpy(st[i].ans,"fail");}
116 
117     return num;
118 }

task5运行截图:

 

6. 实验任务6

【必做部分】

task6源代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #define N 80
 6 
 7 typedef struct{
 8     int id;
 9     char name[10];
10     char classname[20];
11 }STU;
12 
13 int main(){
14     FILE *fp;
15     int i,m;
16     STU a[N];
17 
18     fp=fopen("D:/c语言/实验7/list2.txt","r");
19 
20     if(fp==NULL){
21         printf("fail to open\n");
22         system("pause");
23         return 1;}
24         
25     for(i=0;i<N;i++)
26         fscanf(fp,"%d%s%s",&a[i].id,a[i].name,a[i].classname);
27 
28     fclose(fp);
29 
30     fp=fopen("D:/c语言/实验7/lucky.txt.txt","w");
31 
32     srand((unsigned int)time(0));
33     for(i=0;i<5;i++){
34         //srand((unsigned int)time(0));
35         m=rand()%80+1;
36         fprintf(fp,"%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname);
37         printf("%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname);
38     }
39 
40     fclose(fp);
41 
42     system("pause");
43     return 0;
44 }

task6运行截图:

 

【选做部分】

源代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <time.h>
 5 #define N 80
 6 
 7 typedef struct {
 8     int id;
 9     char name[10];
10     char classname[20];
11 }STU;
12 
13 int main() {
14     FILE* fp;
15     int i, m, k, j;
16     int tum;
17     char tump[50];
18     STU a[N];
19     STU b[5];
20 
21     fp = fopen("D:/c语言/实验7/list2.txt", "r");
22 
23     if (fp == NULL) {
24         printf("fail to open\n");
25         system("pause");
26         return 1;
27     }
28 
29     for (i = 0; i < N; i++)
30         fscanf(fp, "%d%s%s", &a[i].id, a[i].name, a[i].classname);
31 
32     fclose(fp);
33 
34     fp = fopen("D:/c语言/实验7/lucky.txt.txt", "w");
35 
36     srand((unsigned int)time(0));
37     b[1].id=1;
38     b[2].id=2;
39     b[3].id=3;
40     b[4].id=4;
41     b[5].id=5;
42     for (i = 0; i < 5; i++) {
43         //srand((unsigned int)time(0));
44         m = rand() % 80 + 1;
45         /*fprintf(fp,"%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname);
46         printf("%d\t%s\t%s\n",a[m].id,a[m].name,a[m].classname);*/
47         b[i].id = a[m].id;
48         if (b[1].id == b[2].id||b[1].id == b[3].id||b[1].id == b[4].id||b[1].id == b[5].id||b[2].id == b[3].id||b[2].id == b[4].id||b[2].id == b[5].id||b[3].id == b[4].id||b[3].id == b[5].id||b[4].id == b[5].id){
49             i--;
50             continue;}
51         else {
52             strcpy(b[i].name, a[m].name);
53             strcpy(b[i].classname, a[m].classname);
54             }
55     }
56     for (i = 0; i < 5; i++) {
57         for (j = i; j < 5; j++) {
58             if (b[i].id > b[j].id) {
59                 tum = b[i].id;
60                 b[i].id = b[j].id;
61                 b[j].id = tum;
62 
63                 strcpy(tump, b[i].name);
64                 strcpy(b[i].name, b[j].name);
65                 strcpy(b[j].name, tump);
66 
67                 strcpy(tump, b[i].classname);
68                 strcpy(b[i].classname, b[j].classname);
69                 strcpy(b[j].classname, tump);
70             }
71         }
72     }
73 
74     for (i = 0; i < 5; i++) {
75         fprintf(fp, "%d\t%s\t%s\n", b[i].id, b[i].name, b[i].classname);
76         printf("%d\t%s\t%s\n", b[i].id, b[i].name, b[i].classname);
77     }
78     fclose(fp);
79 
80     system("pause");
81     return 0;
82 }

运行截图:

 /*以时间命名txt文件有待研究*/

标签:fp,文件,name,int,编程,st,实验,t%,id
From: https://www.cnblogs.com/sunria/p/17910876.html

相关文章

  • 浅谈 Socket.D 与响应式编程
    一、Socket.D的主要特性首先,Scoket.D是高效一个二进制的网络通讯协议(官方我讲法是:基于事件和语义消息流的网络应用协议),能够满足很多场景下使用。其次,Scoket.D是温和的响应式(采用回调风格)。1、三种通讯模式send只是发送(发送后不管了)发送一个请求,无需为这个请求发送答复报......
  • C# 文件上传及下载
    一、文件上传首先创建一个简单的上传页面,如图: 然后,在后台编写文件上传方法:1///<summary>2///上传文件3///</summary>4///<returns></returns>5publicvoidFileUp()6{7//获取上传文件8......
  • 实验六
    4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutput......
  • 实验7 文件应用编程
    四、实验结论4.实验任务4task4.c1#include<stdio.h>23intmain(){4FILE*fp;56intcount=0;78fp=fopen("data4.txt","r");910if(fp==NULL){11printf("failtoopenf......
  • 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......