首页 > 其他分享 >实验七

实验七

时间:2023-12-16 21:11:06浏览次数:18  
标签:fp STU int st stu 实验 pass

task4 code

 1 #include<stdio.h>
 2 
 3 int main() {
 4     FILE* fp;
 5     long cnt = 0, c = 0;
 6     char ch;
 7 
 8     fp = fopen("data4.txt", "r");
 9     if (fp == NULL) {
10         printf("failed to open");
11         return 1;
12     }
13 
14     fseek(fp, 0L, SEEK_END);
15     cnt = ftell(fp);
16     fseek(fp, 0L, SEEK_SET);
17 
18     while (1) {
19         ch = fgetc(fp);
20         if (ch == '\n' || ch == ' ')
21             c++;
22         if (ch == EOF)
23             break;
24     }
25     cnt--;
26     printf("data4.txt中共包含字符数(除去空白):%ld", cnt - c);
27     fclose(fp);
28 
29     return 0;
30 }
View Code

 task5 code

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

task5 result

task6 code

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

task6 result

 

标签:fp,STU,int,st,stu,实验,pass
From: https://www.cnblogs.com/nuist0415/p/17901880.html

相关文章

  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献1简述你完成的工作与组内成员相互配合协作,高效率完成任务参与组内文档的撰写工作负责了前端设计与数据库的建立2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组总共贡献......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>910classVector{11private:12T*data;13size_tsize;1415public:16......
  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 实验六
    task4源代码:1#pragmaonce23#include<iostream>4#include<stdexcept>5#include<cassert>6#include<iomanip>78usingnamespacestd;910template<typenameT>11classVector{12public:13Vecto......
  • 实验7
    //将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》",......
  • 实验7_文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoop......
  • 实验七
    task41#include<stdio.h>2intmain(){3FILE*fp;4longcount=0;5chart;6if((fp=fopen("data4.txt","r"))==NULL){7printf("error\n");8return1;9}10while(!f......
  • 【腾讯云云上实验室】用向量数据库在金融信用数据库分析中的实战运用
    一、前言这篇文章将带领读者探索数据库的多样化解决方案及其演进历程,特别关注向量数据库的重要性和在实际项目中的应用。通过深入剖析腾讯云向量数据库及其在金融信用数据库分析中的实战运用,为读者提供全面而实用的指南,帮助他们理解、应用和掌握这一技术领域的关键要点。二、数......
  • 实验6 c语言结构体,枚举应用编程
    task4源代码1#include<stdio.h>2#include<string.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];//书名8charauthor[80];//作者9doublesales_price;//......
  • 实验六
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......