首页 > 其他分享 >实验七

实验七

时间:2023-12-18 16:57:43浏览次数:34  
标签:include int st STU 实验 pass t%

4.实验4

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

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 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("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     fout = fopen("list_pass.txt", "w");
72     if (!fout) {
73         printf("fail to open or create list_pass.txt\n");
74         exit(0);
75     }
76     
77     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
78 
79     for (i = 0; i < n; i++)
80         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);
81 
82     fclose(fout);
83 }
84 
85 int process(STU st[], int n, STU st_pass[]) {
86     int i,j=0;
87     for(i=0;i<n;i++){
88         st[i].sum=st[i].objective+st[i].subjective;
89         if(st[i].sum>=60){
90             strcpy(st[i].ans,"pass");
91             st_pass[j]=st[i];
92             j++;}
93         else 
94             strcpy(st[i].ans,"fail");}
95     return j;
96 }

6.实验6

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

 

标签:include,int,st,STU,实验,pass,t%
From: https://www.cnblogs.com/TangZhiXuan/p/17901657.html

相关文章

  • 实验7 文件应用编程
    1.实验任务1【验证性实验】2.实验任务2【验证性实验】3.实验任务3【验证性实验】4.实验任务4task4源代码:1#include<stdio.h>2#include<stdlib.h>34intmain(){5FILE*fp;6intt=0;7chara;89fp=fopen("D:/c语言/实验7......
  • 实验六
    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......
  • 实验六
    #include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutput(B......
  • 实验七
    实验任务4源代码1#include<stdio.h>2#defineN1003#defineM1004intmain(){5FILE*fp;6chara[N][M];7inti,j=0,k=0,s=0;8fp=fopen("data4.txt","r");9if(fp==NULL){10printf("false......
  • 实验6
    #include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector():data(nullptr),size(0){}Vector(intn):size(n){data=newT[n......
  • 实验6
    #include<iostream>#include"Vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);for(autoi=0;i<n;++i)x1.at(i)=i*0.7;output(x1);V......
  • 实验6
      task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;v......
  • 实验6 C语言结构体、枚举应用编程
    task11//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可以把这个数组改......
  • 实验六
    task1.c//P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输......