首页 > 其他分享 >实验7

实验7

时间:2023-12-20 23:22:25浏览次数:32  
标签:int printf st STU 实验 pass t%

task4

代码

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

截图

 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     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 count = 0, i, j;
 98     STU t;
 99     FILE* fout;
100     fout = fopen("list_pass.txt", "r+");
101     
102     for (i = 0; i < n; i++)
103         st[i].sum = st[i].objective + st[i].subjective;
104 
105 
106     for(i = 0,j = 0;i < n;i++)
107     {
108         if(st[i].sum>=60)
109         {
110             strcpy(st[i].ans, "pass");
111             count++;
112             st_pass[j++] = st[i];
113         }
114                  
115         else
116             strcpy(st[i].ans, "fail");
117         
118     }
119 
120 
121     fclose(fout);
122     return count;
123 }

 task6

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 typedef struct{
 6     long id;
 7     char name[20];
 8     char ban[40];
 9 }xx;
10 int main()
11 {
12     xx a[5],x[80];
13     int i,n;
14     FILE *fp,*fps;
15     fp = fopen("d:\\date\\list.txt","r");
16     if(fp==NULL) {
17         printf("fail to open\n");
18         return 1;
19     }
20     srand((double)time(NULL));
21     for(i=0;i<80;i++)
22     fscanf(fp,"%ld%s%s",&x[i].id,x[i].name,x[i].ban);
23     for(i=0;i<5;i++)
24     {
25     n=rand()%(204942080-204942001+1);
26     a[i]=x[n];
27     printf("%-20ld%-20s%-20s\n",a[i].id,a[i].name,a[i].ban);
28     }
29     fclose(fp);
30     fps=fopen ("lucky.txt","w");
31     if(fps==NULL)
32     {
33         printf("fail to open\n");
34         return 1;
35     }
36     for(i=0;i<5;i++)
37     fprintf(fps,"%-20ld%-20s%-20s\n",a[i].id,a[i].name,a[i].ban);
38     fclose(fps);
39     return 0;
40 }

 

标签:int,printf,st,STU,实验,pass,t%
From: https://www.cnblogs.com/z00z/p/17917863.html

相关文章

  • 实验7 文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS1#include<stdlib.h>#include<stdio.h>#defineN100intmain(){FILE*fp;charn,*p;intcount=0;fp=fopen("data4.txt","r");while(feof(fp)==0){n=fgetc(fp);if(n!=''&......
  • 通用定时器及定时器中断实验
    定时器:顾名思义是用来定时的一个外设。stm32有八个定时器,分三类计数器计数模式又分为三种:向上,向下,向上/下计数。向上:计数器从零计数到自动加载值,然后重新从零开始并产生一个计数器溢出事件。向下:计数器从自动加载值计数到零,然后重新从自动加载值开始并产生一个计数器溢出事件......
  • 实验七
    task4#include<stdio.h>#include<string.h>#defineN5#defineM80intmain(){charsongs[N][M];FILE*fp;fp=fopen("data1.txt","r");if(fp==NULL){printf("failtoopenfile\n"......
  • 实验7
    task.4#include<stdio.h>intmain(){FILE*fp;intk=0;charch;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return1;}while(1){......
  • 实验7
    #include<stdio.h>#defineM80intmain(){FILE*fp;charch;fp=fopen("e:\\data4.txt","r");if(fp==NULL)printf("failtoopenfile\n");else{while(1){ch=fgetc(fp);i......
  • 实验七
    task4.c1#include<stdio.h>23intmain(){4FILE*fp;56intk=0;7charch;89fp=fopen("data4.txt","r");1011if(fp==NULL){12printf("failtoopenfile\n");13......
  • 实验七
    实验四源代码#include<stdio.h>#defineM80intmain(){ FILE*fp; charch; fp=fopen("e:\\data4.txt","r"); if(fp==NULL) printf("failtoopenfile\n"); else { while(1){ ch=fgetc(fp); if(ch==EOF) break;......
  • 实验7 文件应用编程
    实验任务4:文件简单应用程序源码1#include<stdio.h>23intmain(){4FILE*fp;5longsize;6intc;7intcount=0;8fp=fopen("C:\\data\\data4.txt","rb");9while((c=fgetc(fp))!=EOF){10......
  • 实验7 文件应用编程
    task4源代码1#include<stdio.h>2#include<stdlib.h>34intmain(){5FILE*fp;6charch;7intcnt=0;89fp=fopen("d:\\date\\data4.txt","r");10if(fp==NULL){11printf("......
  • 实验7
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<stdio.h>#include<string.h>#defineN100#defineM100intmain(){FILE*fp;fp=fopen("data4.txt","r");if(fp==......