首页 > 其他分享 >实验7

实验7

时间:2023-12-14 17:35:28浏览次数:27  
标签:name int st stu 实验 sts id

task4

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #define M 80
 5 int main() {
 6     char songs[N][M];
 7 
 8     FILE* fp;
 9     fp = fopen("data1.txt", "r");
10     if (fp == NULL) {
11         printf("fail to open file\n");
12         return 1;
13     }
14     int t = 0;
15     char h;
16     while (1) {
17         h = fgetc(fp);
18         if (h == EOF) {
19             break;
20         }
21         else if (h == ' ' || h == '\n') {
22             continue;
23         }
24         else {
25             t++;
26         }
27     }
28     
29     printf("文件内字符数为%d\n", t);
30 
31     fclose(fp);
32     return 0;
33 }

 

 

 

task5

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 #define N 10
 6 typedef struct {
 7     long int id;
 8     char name[20];
 9     float objective; // 客观题得分
10     float subjective; // 操作题得分
11     float sum; // 总分
12     char ans[10]; // 考试结果
13 } STU;
14 // 函数声明
15 void finput(STU st[], int n);
16 void foutput(STU st[], int n);
17 void output(STU st[], int n);
18 int process(STU st[], int n, STU st_pass[]);
19 int main() {
20     STU stu[N], stu_pass[N];
21     int cnt;
22     double pass_rate;
23     printf("从文件读入%d个考生信息...\n", N);
24     finput(stu, N);
25     printf("\n对考生成绩进行统计...\n");
26     cnt = process(stu, N, stu_pass);
27     printf("\n通过考试的名单:\n");
28     output(stu, N); // 输出到屏幕
29     foutput(stu, N); // 输出到文件
30     pass_rate = 1.0 * cnt / N;
31     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100);
32     return 0;
33 }
34 // 把通过考试的考生完整信息输出到屏幕上
35 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
36 void output(STU st[], int n) {
37     int i;
38     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
39     for (i = 0; i < n; i++)
40         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id,
41             st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans);
42 }
43 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
44 void finput(STU st[], int n) {
45     int i;
46     FILE* fin;
47     fin = fopen("examinee.txt", "r");
48     if (fin == NULL) {
49         printf("fail to open file\n");
50         exit(0);
51     }
52     while (!feof(fin)) {
53         for (i = 0; i < n; i++)
54             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name,
55                 &st[i].objective, &st[i].subjective);
56     }
57     fclose(fin);
58 }
59 // 把通过考试的考生完整信息写入文件list_pass.txt
60 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
61 void foutput(STU s[], int n) {
62     FILE* fout;
63     int i;
64     // 保存到文件
65     fout = fopen("list_pass.txt", "w");
66     if (!fout) {
67         printf("fail to open or create list_pass.txt\n");
68         exit(0);
69     }
70     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
71     for (i = 0; i < n; i++)
72         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id,
73             s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
74     fclose(fout);
75 }
76 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
77 int process(STU st[], int n, STU st_pass[]) {
78     FILE* fscore;
79     int cnt = 0;
80     fscore = fopen("list_pass.txt", "r");
81     for (int i = 0; i < n; i++) {
82         st[i].sum = st[i].objective + st[i].subjective;
83     }
84 
85     for (int i = 0; i < n; i++) {
86         if (st[i].sum >= 60) {
87             strcpy(st[i].ans, "pass");
88             cnt++;
89         }
90         else {
91             strcpy(st[i].ans, "fail");
92         }
93     }
94 
95     return cnt;
96 }

 

 

 

task6

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define N 80
 5 
 6 typedef struct {
 7     int id;
 8     char name[20];
 9     char class[100];
10 }info;
11 
12 
13 void finput(info stu[], int n) {
14 
15     FILE* fp;
16     fp = fopen("list.txt", "r");
17     while (!feof(fp)) {
18         for (int i = 0; i < N; i++) {
19             fscanf(fp, "%d%s%s", &stu[i].id, stu[i].name, stu[i].class);
20         }
21     }
22     fclose(fp);
23 }
24 
25 void output(info stu[], int n) {
26     for (int i = 0; i < n; i++)
27         printf("%d\t%s\t%s\n", stu[i].id, stu[i].name, stu[i].class);
28 }
29 
30 void translate(info stu[], info sts[], int n) {
31     srand((unsigned)time(NULL));
32     int judge[80] = { 0 };
33     for (int i = 0; i < 5; i++) {
34         int j = rand() % 80 + 1;
35         if (judge[j] == 1) {
36             i--;
37             continue;
38         }
39         sts[i].id = stu[j].id;
40         strcpy(sts[i].name, stu[j].name);
41         strcpy(sts[i].class, stu[j].class);
42         judge[j] = 1;
43     }
44 }
45 
46 void sort(info sts[], int n) {
47     for (int i = 0; i < 5; i++) {
48         for (int j = i; j < 5; j++) {
49             if (sts[i].id > sts[j].id) {
50                 int l = sts[i].id;
51                 sts[i].id = sts[j].id;
52                 sts[j].id = l;
53                 char p[100];
54                 strcpy(p, sts[i].name);
55                 strcpy(sts[i].name, sts[j].name);
56                 strcpy(sts[j].name, p);
57             }
58         }
59     }
60 }
61 
62 int main()
63 {
64     info stu[N], sts[5];
65     finput(stu, N);
66     output(stu, N);
67     printf("\n");
68     translate(stu, sts, N);
69     sort(sts, 5);
70     output(sts, 5);
71 
72     return 0;
73 }

 

标签:name,int,st,stu,实验,sts,id
From: https://www.cnblogs.com/VitaminC114514/p/17901637.html

相关文章

  • RK3568行业定制主板信号抗扰传导实验整改方案验证
    为了整改验证RK3568行业定制主板CAN口的抗干扰能力,在可靠性测试实验室内对定制主板进行了信号抗扰传导实验,其测试环境如下图所示。1. 实验目的图1.1测试实验环境 为了滤波,起到一个抗干扰的作用,需要对RK3568定制主板CAN口进行调整,如下图所示需要在L11位置的C82、C84增加两个......
  • 实验6 C语言结构体、枚举应用编程
    实验任务41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 3.1-华三-无线局域网的配置实验
    1.规划需求目的:搭建一个基本的无线局域网。AC上的配置:1.网段规划管理AP:vlan100IP:10.1.1.024业务地址:vlan1000IP:10.1.2.0242.做DHCP服务器,能分配地址给AP/客户端。(实际上,dhcp服务器,大多数是用微软windowsserver系统做DHCP服务器。第一步:模拟器画上好拓扑......
  • 大数据实验2
    实验内容与完成情况:向HDFS中上传任意文本文件,如果指定的文件在HDFS中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件; 从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名; 将HDFS中指定文件的内容输出到终端中; 显示HDF......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4:1.代码:vector.hpp:1#pragmaonce2#include<iostream>3#include<stdexcept>4usingstd::cout;5usingstd::endl;67template<typenameT>8classVector{9public:10Vector(Tn);11Vector(Tn,Tvalue);12Vector(c......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp#ifndefVECTOR_HPP#defineVECTOR_HPP#include<iostream>#include<stdexcept>template<typenameT>classVector;template<typenameU>voidoutput(constVector<U>&vec);template<typenameT&......
  • 实验6 C语言结构体、枚举应用编程
    1.实验任务1【验证性实验】2.实验任务2【验证性实验】3.实验任务3【验证性实验】4.实验任务4task4源代码:1#include<stdio.h>2#include<stdlib.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];......
  • 实验六
    任务四task4.cpp1#include<iostream>2#include"vector.hpp"3voidtest()4{5usingnamespacestd;6intn;7cin>>n;89Vector<double>x1(n);10for(autoi=0;i<n;++i)11......
  • 实验六
    #include<stdio.h>#include<string.h>#include<stdlib.h>typedefstruct{charname[20];//姓名charphone[12];//手机号intvip;//是否为紧急联系人,是取1;否则取0}Contact;//函数声明voidset_vip_contact(Contactx[],......
  • 实验7
    //P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstu......