首页 > 其他分享 >实验七

实验七

时间:2024-06-22 15:46:26浏览次数:11  
标签:int printf st STU 实验 pass fin

任务4

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

任务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_pass, cnt);      // 输出到屏幕
 35     foutput(stu_pass, cnt);    // 输出到文件
 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 int process(STU st[], int n, STU st_pass[]) {
 95    int i,j=0;
 96    for(i=0;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    }
103    return j;
104 }

任务6

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

 

标签:int,printf,st,STU,实验,pass,fin
From: https://www.cnblogs.com/800225shi/p/18262375

相关文章

  • qt 简单实验 一个可以向右侧拖拽缩放的矩形
    1.概要目的是设置一个可以拖拽缩放的矩形,这里仅用右侧的一个边模拟这个过程。就是为了抓住核心,这个便解决了,其他的边也是一样的。而这个更能体现原理。2.代码2.1 resizablerectangle.h#ifndefRESIZABLERECTANGLE_H#defineRESIZABLERECTANGLE_H#include<QWidget>#in......
  • 【计算机网络仿真】b站湖科大教书匠思科Packet Tracer——实验6 生成树协议STP的功能
    一、实验目的1.验证以太网交换机生成树协议的功能;2.理解网络环路对网络的负面效应;3.理解生成树协议的作用。二、实验要求1.使用CiscoPacketTracer仿真平台;2.观看B站湖科大教书匠仿真实验视频,完成对应实验。三、实验内容1.构建网络拓扑;2.发送报文,理解生成树协议ST......
  • 【计算机网络仿真】b站湖科大教书匠思科Packet Tracer——实验7 虚拟局域网VLAN
    一、实验目的1.学习如何划分VLAN;2.验证划分VLAN的作用;3.学习如何用命令行创建VLAN,将端口划分到VLAN,设置端口类型。二、实验要求1.使用CiscoPacketTracer仿真平台;2.观看B站湖科大教书匠仿真实验视频,完成对应实验。三、实验内容1.构建网络拓扑;2.划分VLAN;3.发送ICM......
  • 高性能并行计算华为云实验一:MPI矩阵运算
    目录一、实验目的二、实验说明三、实验过程3.1创建矩阵乘法源码3.1.1实验说明3.1.2实验步骤3.2创建卷积和池化操作源码3.2.1实验说明3.2.2实验步骤3.3创建Makefile文件并完成编译3.4建立主机配置文件与运行监测四、实验结果与分析4.1矩阵乘法实验4.1.1......
  • c语言程序实验————实验报告十三
    c语言程序实验————实验报告十三实验项目名称:实验报告十三结构体运用程序设计实验项目类型:验证性实验日期:2024年5月30日一、实验目的1.掌握结构体类型变量的定义和使用2.掌握结构体类型数组的概念和应用3.掌握结构体类型指针的概念和应用4.掌握共用体的概念和......
  • c语言程序实验————实验报告十二
    c语言程序实验————实验报告十二实验项目名称:实验报告十二用指针处理函数与数组实验项目类型:验证性实验日期:2024年5月30日一、实验目的1.掌握指针变量的定义格式,会定义和使用指针变量2.能正确建立指针变量与数组(包括一维、两维和字符串数组)的联系,并正确使用指......
  • 实验七
    #include<stdio.h>intmain(){ charch; intc=0; FILE*fp; fp=fopen("data4.txt","r"); while(!feof(fp)) { ch=fgetc(fp); if(ch==EOF) break; if(ch!=''&&ch!='\n'&&ch!......
  • 实验7_文件应用编程
    #include<stdio.h>#include<string.h>#include<stdlib.h>intmain(){ intcnt=0; charc; FILE*fp; fp=fopen("data4.txt","r"); while(!feof(fp)) { c=fgetc(fp); if(c=='\n'||c==''||c==�......
  • 实验7
    test4#include<stdio.h>intmain(){intcount=0;charch;FILE*fp;//以读的方式打开文本文件data1.txtfp=fopen("C://Users//21020//Desktop//data4.txt","r");//如果打开文件失败,输出提示信息并返回if(fp==NULL)......
  • 实验7 文件应用编程
    task.4 #include<stdio.h>intmain(){FILE*fp;charch;intcount=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfile\n");return;}while((ch=fg......