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

实验7_文件应用编程

时间:2023-12-17 19:23:12浏览次数:34  
标签:printf 文件 int 编程 st STU 实验 pass t%

4.task_4

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10000
 4 
 5 int main()
 6 {
 7     int count,i,t=0;
 8     char x[N];
 9     FILE *fp;
10     fp=fopen("D:\\data4.txt","r");
11     while (!feof(fp))
12     {
13         fgets(x,N,fp);
14         count=strlen(x);
15         if (x[count-1]=='\n')
16         {
17             x[count-1]='\0';
18             count--;
19         }
20         for (i=0;x[i]!='\0';i++)
21         {
22             if (x[i]==' ')
23                 count--;
24         }
25         t+=count;
26     }
27     fclose(fp);
28     printf("data4.txt中共包含字符数(不计空白字符):%d\n",t);
29     return 0;
30 }

 

5.task_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 void finput(STU st[], int n);
 17 void foutput(STU st[], int n);
 18 void output(STU st[], int n);
 19 int process(STU st[], int n, STU st_pass[]);
 20 
 21 int main() {
 22     STU stu[N], stu_pass[N];
 23     int cnt;
 24     double pass_rate;
 25 
 26     printf("从文件读入%d个考生信息...\n", N);
 27     finput(stu, N);
 28 
 29     printf("\n对考生成绩进行统计...\n");
 30     cnt = process(stu, N, stu_pass);
 31 
 32     printf("\n通过考试的名单:\n");
 33     output(stu, N);
 34     foutput(stu, N);
 35 
 36     pass_rate = 1.0 * cnt / N;
 37     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 38 
 39     return 0;
 40 }
 41 
 42 void output(STU st[], int n) {
 43     int i;
 44     
 45     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 46     for (i = 0; i < n; i++)
 47         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);
 48 }
 49 
 50 void finput(STU st[], int n) {
 51     int i;
 52     FILE *fin;
 53 
 54     fin = fopen("D:\\examinee.txt", "r");
 55     if (fin == NULL) {
 56         printf("fail to open file\n");
 57         exit(0);
 58     }
 59 
 60     while (!feof(fin)) {
 61         for (i = 0; i < n; i++)
 62             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 63     }
 64 
 65     fclose(fin);
 66 }
 67 
 68 void foutput(STU s[], int n) {
 69     FILE *fout;
 70     int i;
 71     
 72     fout = fopen("D:\\list_pass.txt", "w");
 73     if (!fout) {
 74         printf("fail to open or create list_pass.txt\n");
 75         exit(0);
 76     }
 77     
 78     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 79 
 80     for (i = 0; i < n; i++)
 81         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);
 82 
 83     fclose(fout);
 84 }
 85  
 86 int process(STU st[], int n, STU st_pass[]) {
 87     int i,j;
 88     for (i=0,j=0;i<n;i++)
 89     {
 90         st[i].sum=st[i].objective+st[i].subjective;
 91         if (st[i].sum>=60)
 92         {
 93             strcpy(st[i].ans,"pass");
 94             st_pass[j++]=st[i];
 95         }
 96         else
 97             strcpy(st[i].ans,"fail");
 98     }
 99     return j;
100 }

 

6.task_6

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #define N 100
 5 
 6 typedef struct
 7 {
 8     long int id;
 9     char name[N];
10     char class[N];
11 }STU;
12 
13 void finput(STU st[N],int n);
14 void foutput(STU st[N]);
15 
16 int main()
17 {
18     int i,k;
19     STU stu[N],lucky[N];
20     finput(stu,N);
21     for (i=0;i<5;i++)
22     {
23         k=rand()%80;
24         lucky[i]=stu[k];
25     }
26     foutput(lucky);
27     for (i=0;i<5;i++)
28         printf("%ld\t\t%s\t\t%s\n",lucky[i].id,lucky[i].name,lucky[i].class);
29     return 0;
30 }
31 
32 void finput(STU st[N],int n)
33 {
34     int i;
35     FILE *fin;
36 
37     fin = fopen("D:\\list.txt", "r");
38     if (fin == NULL) {
39         printf("fail to open file\n");
40         exit(0);
41     }
42 
43     while (!feof(fin)) {
44         for (i = 0; i < n; i++)
45             fscanf(fin, "%ld %s %s",&st[i].id,st[i].name,st[i].class);
46     }
47 
48     fclose(fin);
49     
50 }
51 
52 void foutput(STU st[N])
53 {
54     int i;
55     FILE *fout;
56     fout = fopen("D:\\lucky.txt", "w");
57     if (!fout) {
58         printf("fail to open or create list_pass.txt\n");
59         exit(0);
60     }
61     for (i=0;i<5;i++)
62         fprintf(fout,"%ld\t\t%s\t\t%s\n",st[i].id,st[i].name,st[i].class);
63     fclose(fout);
64 }

 

标签:printf,文件,int,编程,st,STU,实验,pass,t%
From: https://www.cnblogs.com/zxy2004/p/17901655.html

相关文章

  • 实验6
    实验1代码1//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可以把这个数......
  • 实验三-电子公文传输系统1-个人贡献
    个人贡献1、简述你完成的工作部分前端代码及文档撰写github项目管理协助设计系统前端布局2、你们小组总共的代码行数,你贡献的代码行数?相关代码链接?贡献的代码行数:985https://github.com/hexaosf/codegramhttps://github.com/hexaosf/codegram/blob/608d2056e40859360e0ea6......
  • 人工智能-A*算法-最优路径搜索实验
    上次学会了《A*算法-八数码问题》,初步了解了A*算法的原理,本次再用A*算法完成一个最优路径搜索实验。 一、实验内容1.设计自己的启发式函数。2.在网格地图中,设计部分障碍物。3.实现A*算法,搜索一条最优路径。 二、A*算法实现步骤1.初始化:设置起始节点和目标节点,并创建一......
  • 2023-2024-1 20211327 实验三-电子公文传输系统1-个人贡献
    简述工作在项目前期,我撰写了部分博客,作为组长分配任务设计项目整体框架结构,完成vue全局过滤器的声明和相关设置撰写了普通用户(非管理员)增删改查部分的代码,对jsp和html等前端代码进行修改和完善对数据库部分相关代码进行补充进行系统测试任务摘录全局过滤器我们的公文传......
  • 实验三-电子公文传输系统1-个人贡献
      1简述你完成的工作作为组员,和其他组员相互配合,听从组长的安排,合理高效完成任务。参与组内文档的撰写工作。参与后端设计的代码编写以及前端设计的代码编写2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组......
  • 实验6 模板类、文件IO和异常处理
    四、实验结论1.实验任务4Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector(intsz=0,constT&value......
  • 实验三-电子公文传输系统1-个人贡献
    一、任务详情简述你完成的工作你们小组总共的代码行数,你贡献的代码行数?相关代码链接?你们小组总共的文档数?你贡献的文档数?相关链接?二、简述你完成的工作我主要负责前端的页面设计和与后端同学对接的工作。以及上传GitHub和每日总结绘制燃尽图。三、你们小组总共的代码行数,......
  • 实验三-电子公文系统1-个人贡献
    1简述你完成的工作作为组员,积极配合组长的工作,合理高效完成任务;参与组内文档的撰写工作;参与后端设计的代码编写以及数据库搭建的代码编写。2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组总共贡献了13139行......
  • ExeIconToFolder 提取exe程序图标,并设置exe所在文件夹图标
    ExeIconToFolder提取exe程序图标,并设置exe所在文件夹图标2023年12月17日提取exe程序图标,并设置exe所在文件夹图标所需第三方程序IconsExtract-EXE图标提取(nirsoft.net)RestartExplorer---重新启动资源管理器(sordum.org)代码@echooffsetlocalenabledelayed......
  • 实验六
    1#include<iostream>2#include"Vector.hpp"3usingnamespacestd;45template<typenameT>6voidoutput(constVector<T>&v){7for(autoi=0;i<v.get_size();i++){8cout<<v.at(i)<<&......