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

实验7 文件应用编程

时间:2023-12-21 23:22:51浏览次数:36  
标签:printf 文件 int 编程 st STU 实验 pass t%

实验任务4

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

结果演示

实验任务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("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     int i,k=0;
 98     for(i=0;i<n;i++){
 99         st[i].sum=st[i].objective+st[i].subjective;
100         if(st[i].sum>=60){
101             st_pass[k++]=st[i];
102             strcpy(st[i].ans,"pass");
103         }else{
104             strcpy(st[i].ans,"fail");
105         }
106     }
107     return k;
108 }

结果演示

实验任务6

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

结果演示

 

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

相关文章

  • 实验7
    task4#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("fail\n");exit(0);}while(......
  • powershell 作服务端 响应网络(socket tcp)连接 提供文件夹大小查询服务
    包含:端口占用检测心跳包网络断线检测传入的数据是否为合法有效的目录路径检测读取计算文件夹大小(不含软链接|symlink)传回查询到的文件夹大小[cmdletbinding()]Param($Port=8888)$VerbosePreference="Continue"#值或取`SilentlyContinue`,此时需调用脚本时传入`-Ve......
  • 专利文件去哪里查?怎么看有没有授权!
    乱七八糟四处乱找附上网址:https://tysf.cponline.cnipa.gov.cn/am/#/user/login第一次登录需要注册喔。如何看专利是否授权?国内的专利就在上述网址中搜出来,然后点到详情里看是只有公开还是公开和授权都有。国外的专利就到WIPO上去查询https://www.wipo.int/portal/en/in......
  • 实验7
     ......
  • 实验七
    #include<stdio.h>#include<stdlib.h>#include<time.h>typedefstruct{longid;charname[20];charban[40];}xx;intmain(){xxa[5],x[80];inti,n;FILE*fp,*fps;fp=fopen("d:\\data\\......
  • 实验七
    #include<stdio.h>#defineN2intmain(){char*x[N]={"0123456789-0123456789","nuist2023-nuist2024"};inti;FILE*fp;fp=fopen("data4.txt","w");if(fp==......
  • lazaurs自建库文件系统
    以Linux_X86_64为例一、删除LazarusFPC\cross\lib\x86_64-linux下的所有文件。二、在Lazarus项目选项-编译器-配置和目标-选择Linux和X86-64。三、编译项目。1、会出现cannotfind-lpthread:Nosuchfileordirectory等等多个文件找不到。-lpthread说明少libpthread.so ......
  • 5. 类文件结构
    Class类文件的结构Class文件是一组以8个字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在文件之中,中间没有添加任何分隔符,这使得整个Class文件中存储的内容几乎全部是程序运行的必要数据,没有空隙存在当遇到需要占用8个字节以上空间的数据项时,则会按照高......
  • asp.net core 3.x 解决文件上传大小限制
    如在Kestrel服务器下可以选择在CreateHostBuilder方法中统一移除所有上传限制。也可在指定的action上添加[DisableRequestSizeLimit]属性解除限制。//第一种在CreateHostBuilder中添加配置。publicstaticIHostBuilderCreateHostBuilder(string[]args){......
  • java读取yaml文件
    应用场景自动化测试中,用例数据存放在yaml文件中 数据 person.yaml---id:1name:韧age:18---id:2name:qzcsbjage:19person2.yaml----id:3name:小韧age:18-id:4name:全栈测试笔记age:19person3.yaml----id:5......