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

实验7—文件应用编程

时间:2023-12-20 14:33:21浏览次数:29  
标签:printf 文件 int 编程 st STU 实验 pass t%

4、实验任务4

源代码

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

运行结果截图

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, 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("d:\\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 i,cnt=0;
100     for(i=0;i<n;i++){
101         st[i].sum=st[i].objective+st[i].subjective;
102         if(st[i].sum>=60)
103         {
104             strcpy(st[i].ans,"通过");
105             st_pass[cnt++]=st[i];
106         }
107         else
108             strcpy(st[i].ans,"未通过");
109     }
110     return cnt;
111 }

运行结果截图

6、实验任务6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 typedef struct{
 6     int id;
 7     char name[20];
 8     char classname[30];
 9 }st;
10 
11 int main(){
12     st stu[80],s[5];
13     
14     FILE *fp;
15     fp=fopen("d:\\list.txt","r");
16     if(fp==NULL){
17         printf("fail to open file.\n");
18         return 1;
19     }
20     
21     int i,m;
22     
23     while(!feof(fp)){
24         for(i=0;i<80;i++)
25             fscanf(fp,"%d\t%s\t%s",&stu[i].id,stu[i].name,stu[i].classname);
26     }
27     
28     i=0;
29     srand((double)time(NULL));
30     while(i<5){
31         m=rand()%80;
32         s[i++]=stu[m];    
33     }
34     
35     FILE *fin; 
36     fin=fopen("d:\\lucky.txt","w");
37     if(!fin){
38         printf("fail to open or create lucky.txt\n");
39         exit(0);
40 
41     }
42     
43     for(i=0;i<5;i++)
44     {
45         fprintf(fin,"%d\t%s\t%s\n",&s[i].id,s[i].name,s[i].classname);
46         printf("%d\t%s\t%s\n",s[i].id,s[i].name,s[i].classname);
47     }
48     
49     fclose(fp);
50     fclose(fin);
51     system("pause");
52     return 0;
53 }

运行结果截图

 

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

相关文章

  • 轻松管理CRM系统权限!判断文件路径类型,让你更安全
    随着企业客户关系管理(CRM)系统的普及,权限管理成为了系统安全的重要环节。在CRM系统中,我们有时需要设置部分用户账号对某个路径进行读取、写入或执行操作权限。为了实现这一功能,我们需要先判断文件路径是目录还是文件。本文将介绍如何使用Java实现这一功能。一、判断文件路径是目录......
  • 第七次实验
    task4#include<stdio.h>intmain(){ charch; FILE*fp; fp=fopen("data4.txt","r"); intcount=0; while(1) { ch=fgetc(fp); if(ch==EOF) break; if(ch==''||ch=='\n') continue; else cou......
  • Java学习之apache poi操作Excel文件
    创建Java项目,然后加入Maven依赖如下:读取指定的Excel文件写入既存的Excel文件 创建新的Excel,添加文件内容......
  • pyinstaller 打包 win32ctypes.pywin32.pywintypes.error: (225, '', '无法成功完成操
    背景:使用python写了一个程序,使用pyinstaller打包,不使用-w--noconsole的命令打包隐藏命令行窗口时,是正常的,但是使用-w或者--noconsole就会报错win32ctypes.pywin32.pywintypes.error:(225,'','无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。')在网上搜索了一些资讯,......
  • 深入了解Git LFS:高效管理大型文件的利器
    今天在使用CodeUp上传代码时,我为项目添加了一个大小超过300MB的文件。在进行push操作时,系统提示我“推送失败,以下文件大小超过单文件200MB的系统限额,大文件请使用Git-LFS管理”。于是我开始了解GitLFS。对于需要处理大型二进制文件的项目而言,Git的性能可能成为一个瓶颈。为了解决......
  • 使用redbaron删除删除一个.py文件的所有注释,输出到一个新.py文件,文件名加上_nocmts后
    BUG:目前输出的文件中可能会出现缩进错误,待修改。fromredbaronimportRedBarondefremove_comments_with_redbaron(source_py_file,keep_blank_lines):withopen(source_py_file,'r',encoding='utf-8')asfile:red=RedBaron(file.read())co......
  • filebeat配置采集多个文件(多索引)推送ES
     Filebeat根据不同的日志设置不同的索引 配置如下:filebeat.inputs:-type:logpaths:-/tmp/log/ecologyencoding:GB2312fields:type:ecology-type:logpaths:-/tmp/log/stderr.logencoding:GB2312fields:type:strerr-ty......
  • 如何解决excel导入大文件报错
    开发过程中我们经常会遇到Excel导入大文件报错的问题,经过不断的摸索,我发现我们可以利用缓存区来实现大文件的上传,下面是我本人封装的一个实现工具,有兴趣的小伙伴可以看看,希望能对大家有所帮助!publicclassExcelImportBigDataUtil{/***每次放入缓冲区最大行数......
  • .Net +Ajax大文件断点续传
    什么是断点续传大文件断点续传指的是在上传或下载大文件时,当传输中断或出现错误时,可以通过记录已经传输的数据和位置,下次从中断的位置继续传输,避免重新开始传输整个文件的过程,从而提高传输效率和稳定性。实现思路获取文件大小和已经传输的大小:在开始上传或下载文件之前,需要获取文......
  • 实验七
    task4源代码:#include<stdio.h>intmain(){charch;inti=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return1;}while(1){ch......