首页 > 其他分享 >第七次实验

第七次实验

时间:2024-06-24 10:57:50浏览次数:3  
标签:int printf st STU 实验 pass t% 第七次

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

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_pass, cnt); // 输出到屏幕
35 foutput(stu_pass, cnt); // 输出到文件
36
37 pass_rate = 1.0 * cnt / N;
38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate
100);
39 system("pause");
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 int process(STU st[], int n, STU st_pass[]) {
95 int i=0,j=0;
96 for(;i<n;i++){
97 st[i].sum=st[i].objective+st[i].subjective;
98 if(st[i].sum>=60){
99 strcpy(st[i].ans,"pass");
100 st_pass[j++]=st[i];
101 }
102 else
103 strcpy(st[i].ans,"Fail");
104 }
105 return j;
106
107 }


1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #define N 100
5 typedef struct{
6 char num[N];
7 char name[N];
8 char cla[N];
9
10 }Inf;
11
12 int main()
13 {
14 int i,j,k=0,m,fla;
15 int flag[5]={80,80,80,80,80};
16 Inf x[N],y[N];
17 FILE fp1,fp2;
18 fp1=fopen("C://Users//Dell//Desktop//list.txt","r");
19 if(fp1NULL)
20 {
21 printf("fail to open file\n");
22 return 1;
23 }
24 for(i=0;i<80;i++)
25 {
26 fscanf(fp1,"%s%s%s",x[i].num,x[i].name,x[i].cla);
27 }
28 fclose(fp1);
29 srand(time(0));
30 j=rand()%80;
31 flag[0]=j;
32 y[0]=x[j];
33 for(i=1;i<5;i++)
34 {
35 fla=0;
36 j=rand()%80;
37 for(m=0;m<5;m++)
38 {
39 if(flag[m]
j)
40 {
41 fla=1;
42 break;
43 }
44 }
45 if(fla0)
46 {
47 y[i]=x[j];
48 flag[k++]=j;
49 }
50 else
51 i--;
52 }
53 fp2=fopen("C://Users//Dell//Desktop//lucky.txt","w");
54 if(fp2
NULL)
55 {
56 printf("fail to open file\n");
57 return 1;
58 }
59 for(i=0;i<5;i++)
60 fprintf(fp2,"%s %s %s\n",y[i].num,y[i].name,y[i].cla);
61 fclose(fp2);
62 for(i=0;i<5;i++)
63 printf("%s %s %s\n",y[i].num,y[i].name,y[i].cla);
64 return 0;
65 }

标签:int,printf,st,STU,实验,pass,t%,第七次
From: https://www.cnblogs.com/2658418351qwe/p/18264550

相关文章

  • 实验七
    4.task.4#include<stdio.h>intmain(){FILE*fp;charch;intcount=0;fp=fopen("d:\\data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return0;}while(!feof(fp))......
  • 高性能并行计算华为云实验五:
    目录一、实验目的二、实验说明三、实验过程3.1创建PageRank源码3.2makefile的创建和编译3.3主机配置文件建立与运行监测四、实验结果与分析4.1采用默认的节点数量及迭代次数进行测试4.2分析并行化下节点数量与耗时的变化规律4.3分析迭代次数与耗时的变化规律......
  • 实验7
    1#include<stdio.h>2#include<stdlib.h>3intmain()4{5intch=0;6inta=0;7FILE*fp;89fp=fopen("data4.txt","r");1011if(fp==NULL){12printf("failtoopen\n");......
  • 实验7
    #include<stdio.h>intmain(){intcnt=0;charch;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return0;}while((ch=fgetc(fp))!=......
  • 实验7
    `#include<stdio.h>include<stdlib.h>defineN80defineM7typedefstruct{charname[N];//书名charauthor[N];//作者}Book;//函数声明voidfunc1();voidfunc2();intmain(){func1();func2();system("pause");return0;}//函数fu......
  • 实验7_文件应用编程
    实验任务4#include<stdio.h>intmain(){FILE*fp;charch;intcount=0;fp=fopen("d:\\data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return0;}while(!feof(fp)){......
  • 实验七
    task4#include<stdio.h>intmain(){intcnt=0;charch;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return;}while((ch=fgetc(f......
  • 实验7
    task1//将图书信息写入文本文件data1.txt//再从文件中读取图书信息,打印输出到屏幕上,并显示行号#include<stdio.h>#defineN80#defineM100typedefstruct{ charname[N];//书名 charauthor[N];//作者}Book;//函数声明voidfunc1();voidfunc2()......
  • 实验 7
    task1点击查看代码//将图书信息写入文本文件data1.txt//再从文件中读取图书信息,打印输出到屏幕上,并显示行号#include<stdio.h>#include<stdlib.h>#defineN80#defineM100typedefstruct{ charname[N];//书名 charauthor[N];//作者}Book;//......
  • 我主编的电子技术实验手册(09)——并联电路
            本专栏是笔者主编教材(图0所示)的电子版,依托简易的元器件和仪表安排了30多个实验,主要面向经费不太充足的中高职院校。每个实验都安排了必不可少的【预习知识】,精心设计的【实验步骤】,全面丰富的【思考习题】。因此,对于开展电子技术教学犯愁的师生,本专栏应该能够......