首页 > 其他分享 >实验7

实验7

时间:2023-12-21 12:44:41浏览次数:26  
标签:int printf st STU 实验 pass t%

实验任务4

代码

 1 #include<stdio.h>
 2 #define N 100
 3 #define M 100
 4 int main(){
 5     FILE *fp;
 6     char a[N][M];
 7     int i,j=0,k=0,s=0;
 8     fp=fopen("data4.txt","r");
 9     if(fp==NULL){
10         printf("false\n");
11         return 1;
12     }
13     
14     while(!feof(fp)){
15         fscanf(fp,"%s",a[i]);
16         i++;
17     }
18     for(j=0;j<i;j++){
19         k=0;
20         while(a[j][k++]!='\0')
21             s++;
22     }
23     printf("data4.txt中共包括字符数(不计空白符):%d",s);
24     fclose(fp);
25     
26     return 0;
27 }

运行结果

 

实验任务5

代码

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

运行结果

 

 

实验任务6

代码

 1 #include<stdio.h>
 2 #define N 100
 3 typedef struct student{
 4     int ida;
 5     char name[20];
 6     char class[100];
 7 }stu;
 8 int main(){
 9     stu st[N],su[N];
10     FILE *fp;
11     FILE *fin;
12     int k,j,s=0,i=0;
13     int a[N];
14     fp=fopen("list.txt","r");
15     fin=fopen("lucky.txt","w");
16     if(fp==NULL){
17         printf("fail\n");
18         return 1;
19     }
20     while(!feof(fp)){
21         fscanf(fp,"%d\t%s\t%s",&st[i].ida ,st[i].name ,st[i].class );
22         i++;
23     }
24     k=i;
25     srand(time(NULL));
26     for(i=0;i<5;i++){
27         a[i]=rand()%81+204942001;
28     for(i=0;i<5;i++){
29         printf("%d\n",a[i]);
30     }
31     for(i=0;i<5;i++)
32         for(j=0;j<k;j++)
33             if(a[i]==st[j].ida )
34                 su[s++]=st[j];
35     for(i=0;i<5;i++){
36         fprintf(fin,"%d\t%s\t%s\n",su[i].ida ,su[i].name ,su[i].class );
37         printf("%d\t%s\t%s",su[i].ida ,su[i].name ,su[i].class);
38         printf("\n");
39     }
40     fclose(fp);
41     fclose(fin);
42     return 0;
43 }

实验结果

 

 

标签:int,printf,st,STU,实验,pass,t%
From: https://www.cnblogs.com/chenziming114514/p/17918726.html

相关文章

  • 实验七
    task1_1源代码://将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{ charname[N];//书名 charauthor[N];//作者}Book;intmain(){ Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》&q......
  • 实验7_文件应用编程
    task4.c#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){longi=0;charch;FILE*fp;fp=fopen("c://data//data4.txt","r");while(!feof(fp)){ch=fgetc(fp);if(ch!=''&&ch!='\n......
  • STM32 窗口看门狗(WWDG)实验
    ......
  • LAMP实验
    LAMP确保CentOS系统已经安装了Apache、MySQL和PHP。可以使用以下命令检查它们是否已安装:sudoyumlistinstalledhttpdmariadb-serverphpphp-mysqlnd如果它们没有安装,可以使用以下命令安装它们:sudoyuminstallhttpdmariadb-serverphpphp-mysqlnd-yApachehttpd-......
  • 实验7
    实验4#include<stdio.h>intmain(){FILE*fp;fp=fopen("d:\\data4.txt","r");if(fp==NULL){printf("failtoopenfile.\n");return1;}charch;intcnt=0;while((ch=fget......
  • 实验七
    task4code#include<stdio.h>#include<string.h>#defineN5#defineM80intmain(){charsongs[N][M];FILE*fp;fp=fopen("data1.txt","r");if(fp==NULL){printf("failtoopenfile\n......
  • 实验7
    task4代码1#include<stdio.h>23intmain()4{5FILE*fp;6fp=fopen("d:\\data4.txt","r");7if(fp==NULL){8printf("failtoopenfile.\n");9return1;10}1112ch......
  • 实验7 文件应用编程
    task4#define_CRT_SECURE_NO_WARNINGS1#include<stdlib.h>#include<stdio.h>#defineN100intmain(){FILE*fp;charn,*p;intcount=0;fp=fopen("data4.txt","r");while(feof(fp)==0){n=fgetc(fp);if(n!=''&......
  • 通用定时器及定时器中断实验
    定时器:顾名思义是用来定时的一个外设。stm32有八个定时器,分三类计数器计数模式又分为三种:向上,向下,向上/下计数。向上:计数器从零计数到自动加载值,然后重新从零开始并产生一个计数器溢出事件。向下:计数器从自动加载值计数到零,然后重新从自动加载值开始并产生一个计数器溢出事件......
  • 实验七
    task4#include<stdio.h>#include<string.h>#defineN5#defineM80intmain(){charsongs[N][M];FILE*fp;fp=fopen("data1.txt","r");if(fp==NULL){printf("failtoopenfile\n"......