首页 > 其他分享 >实验七

实验七

时间:2023-12-15 18:33:23浏览次数:26  
标签:int void lucky st STU 实验 fin

task4

 1 #include<stdio.h>
 2 int main(){
 3     FILE *fp;
 4     long count=0;
 5     char t;
 6     if((fp=fopen("data4.txt","r"))==NULL){
 7         printf("error\n");
 8         return 1;
 9     }
10     while(!feof(fp)){
11         t=fgetc(fp);
12         if(t==' '||t=='\n'||t=='\t')continue;
13         if(t==EOF)break;
14         count++;
15     }
16     printf("data4.txt中共包含字符数(不含空白符):%d",count);
17     return 0;
18 } 
View Code

task5

  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, N);      // 输出到屏幕
 35     foutput(stu, N);    // 输出到文件
 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 
 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 96 int process(STU st[], int n, STU st_pass[]) {
 97     int i,c=0;
 98     for(i=0;i<N;++i){
 99         st[i].sum=st[i].objective+st[i].subjective;
100         if(st[i].sum>=60){
101             strcpy(st[i].ans,"pass");
102             c++;
103         }
104         else
105             strcpy(st[i].ans,"fail");
106     }
107     return c;
108 }
View Code

task7

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #define N 5
 5 #define N1 1
 6 #define N2 80
 7 typedef struct {
 8     long number;
 9     char name[20];
10     char classname[100];
11 } STU;
12 void output(STU st[], int n);
13 void finput(STU st[], int n);
14 void randluck(STU st[], int n, STU lucky[]);
15 int main(){
16     STU stu[N2], lucky[N2];
17     finput(stu, N2);
18     randluck(stu,N,lucky);
19     output(lucky,N);
20     return 0;
21 }
22 void finput(STU st[], int n){
23     int i;
24     FILE *fin;
25     fin = fopen("list.txt", "r");
26     if (fin == NULL) {
27         printf("fail to open file\n");
28         exit(0);
29     }
30     while (!feof(fin)) {
31         for (i = 0; i < n; i++)
32             fscanf(fin, "%ld %s %s", &st[i].number, st[i].name, st[i].classname);
33     }
34     fclose(fin);
35 }
36 void randluck(STU st[], int n, STU lucky[]){
37     FILE *fout;
38     int i,j;
39     STU a;
40     // 保存到文件 
41     fout = fopen("lucky.txt", "w");
42     if (!fout) {
43         printf("error\n");
44         exit(0);
45     }
46     srand(time(0));
47     for(i = 0; i < n; ++i)
48         lucky[i].number=204942000+rand()%(N2 - N1 + 1) + N1;
49     for(i=0;i<n;++i)
50         for(j=0;j<N2;++j)
51             if(lucky[i].number==st[j].number)
52                 lucky[i]=st[j];
53     for(i=0;i<n-1;i++){
54         for(j=0;j<n-i-1;j++){
55             if(lucky[j].number>lucky[j+1].number){
56                 a=lucky[j];
57                 lucky[j]=lucky[j+1];
58                 lucky[j+1]=a;
59             }
60         }
61     }
62     for (i = 0; i < n; i++)
63         fprintf(fout, "%ld%s%s",lucky[i].number,lucky[i].name,lucky[i].classname);
64 
65     fclose(fout);
66 }
67 void output(STU st[], int n){
68     int i;
69     for(i=0;i<n;i++){
70         printf("%ld\t%s\t%s",st[i].number,st[i].name,st[i].classname);
71         printf("\n");
72         }
73 }
View Code

 

标签:int,void,lucky,st,STU,实验,fin
From: https://www.cnblogs.com/zjl-love-wsx/p/17904001.html

相关文章

  • 【腾讯云云上实验室】用向量数据库在金融信用数据库分析中的实战运用
    一、前言这篇文章将带领读者探索数据库的多样化解决方案及其演进历程,特别关注向量数据库的重要性和在实际项目中的应用。通过深入剖析腾讯云向量数据库及其在金融信用数据库分析中的实战运用,为读者提供全面而实用的指南,帮助他们理解、应用和掌握这一技术领域的关键要点。二、数......
  • 实验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;......
  • 实验六
    实验四 源代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count......
  • 什么是LIS系统?实验室信息管理系统是什么?
    在医疗信息化改革的这个大背景下,很多医院都在不断完善信息化建设,有些人还不知道医院实验室信息管理系统。本文章仅以个人的理解整理出来,本文从LIS系统定义、LIS系统实现了哪些功能、LIS系统解决了实验室实际工作中的哪些问题,进行分享;希望能帮助更多基础医疗单位了解【实验室信息管......
  • 实验6
     #include<stdio.h>#include<string.h>#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩doublemid;//期中成绩doublef......
  • 实验六
    task_4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voido......
  • 实验6
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#defineN10typedefstruct{charisbn[20];charname[80];charauthor[80];doublesales_price;intsales_count;}Book;voidoutput(Bookx[],intn);voi......
  • 实验七
    实验一//将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》&q......
  • 实验7
    四、实验结论1.实验任务1思考*:把line34改成n=i;,重新编译,再次运行程序,观察结果,对比运行结果是否有不同。如有不同,尝试分析原因。若改成n=1,则有以下运行截图:尝试分析原因:feof函数实际上是根据读取到最后一个位置的内容来判断的。所以要多走一字节。 2.实验任务2 3......