首页 > 其他分享 >实验6

实验6

时间:2022-12-27 12:33:55浏览次数:36  
标签:%- int void STU num 实验 include

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define N 80
 5 int main()
 6 {
 7     int i,count=0;
 8     char x[N];
 9     FILE *fp;
10     
11     fp = fopen("data4.txt","r");
12     if(fp==NULL)
13     {
14         printf("fail to open the file\n");
15         return 1;
16     }
17     
18     fread(x,N,1,fp);
19     
20     for(i = 0;x[i]!='\0';i++)
21         if(x[i]!=' '&&x[i]!='\n'&&x[i]!='\t')
22             count++;
23             
24     printf("data4.txt中共包含字符数(不包含空白符):%d\n",count);
25     
26     fclose(fp);
27     return 0;    
28     
29 }

  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 level[10];
 14 } STU;
 15 
 16 // 函数声明
 17 void input(STU s[], int n);
 18 void output(STU s[], int n);
 19 void process(STU s[], int n);
 20 
 21 int main() {
 22     STU stu[N];
 23 
 24     printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 25     input(stu, N);
 26 
 27     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 28     process(stu, N);
 29 
 30     printf("\n打印考生完整信息, 并保存到文件中");
 31     output(stu, N);
 32 
 33     return 0;
 34 }
 35 
 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 37 void input(STU s[], int n) {
 38     int i;
 39     FILE *fin;
 40 
 41     fin = fopen("examinee.txt","r");
 42     if (fin == NULL) {
 43         printf("fail to open file\n");
 44         exit(0);
 45     }
 46 
 47     while (!feof(fin)) {
 48         for (i = 0; i < n; i++)
 49             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 50     }
 51 
 52     fclose(fin);
 53 }
 54 
 55 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 56 // 不仅输出到屏幕上,还写到文本文件result.txt中
 57 void output(STU s[], int n) {
 58     FILE *fout;
 59     int i;
 60     
 61     // 输出到屏幕
 62     printf("\n");
 63     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 64     for (i = 0; i < n; i++)
 65         printf("%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].level);
 66     
 67     // 保存到文件 
 68     fout = fopen("result.txt", "w");
 69     if (!fout) {
 70         printf("fail to open or create result.txt\n");
 71         exit(0);
 72     }
 73     
 74     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 75 
 76     for (i = 0; i < n; i++)
 77         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].level);
 78 
 79     fclose(fout);
 80 }
 81 
 82 // 对考生信息进行处理:计算总分,排序,确定等级
 83 void process(STU s[], int n) {
 84     int i,j;
 85     STU t;
 86     for(i = 0;i < n;i++)
 87         s[i].sum = s[i].objective + s[i].subjective;
 88     
 89     for(i = 0;i < n-1;i++)
 90         for(j = 0;j < n-1-i;j++)
 91             if(s[j].sum < s[j+1].sum)
 92             {
 93                 t = s[j];
 94                 s[j] = s[j+1];
 95                 s[j+1] = t;
 96             }
 97     
 98     for(i = 0;i < n;i++)
 99     {
100         if(i < n*0.1)
101             strcpy(s[i].level,"优秀");
102         else if(i >= n*0.1&&i < n*0.5)
103             strcpy(s[i].level,"及格");
104         else
105             strcpy(s[i].level,"不及格");
106     }
107         
108 }

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

 

标签:%-,int,void,STU,num,实验,include
From: https://www.cnblogs.com/lyk946/p/17007806.html

相关文章

  • 实验5
    task3.c#include<stdio.h>#include<string.h>#include<stdlib.h>#defineN100typedefstruct{charnum[10];//学号ints1;//期末成绩ints2;......
  • 实验5
    #include<stdio.h>#include<string.h>#defineN3typedefstructstudent{intid;//学号charname[20];//姓名......
  • 实验6
    #include<stdio.h>#include<stdlib.h>intmain(){chara[2][100]={"nuist2022-nuist2023","FIFAWorldCup2022"};FILE*fp;inti,n=0;charch;......
  • 实验5 结构体应用编程
    1.实验任务1//P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>......
  • 《DFZU2EG_4EV MPSoc之FPGA开发指南》第三十三章 OV5640摄像头HDMI显示实验​
    OV5640摄像头HDMI显示实验​在OV5640摄像头RGB-LCD显示实验中,成功地在LCD屏上实时显示出了摄像头采集的图像。本章将使用FPGA开发板实现对OV5640的数字图像采集并在HDMI显示......
  • 实验内容:NTP的客户端实现
           ......
  • 实验六
    任务4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){charch;intcount=0;FILE*fp;fp=fopen("D:\\data......
  • 实验五
    任务3#include<stdio.h>#include<string.h>#defineN100typedefstruct{charnum[10];//学号ints1;//期末成绩ints2;......
  • 实验6 文件应用编程
    #include<stdio.h>#defineN7#defineM80typedefstruct{charname[M];//书名charauthor[M];//作者}Book;intmain(){Bookx[N]={{"《雕塑家》","......
  • 实验6
    实验6#include<stdio.h>#include<stdlib.h>intmain(){chara[2][100]={"nuist2022-nuist2023","FIFAWorldCup2022"};FILE*fp;inti,n=0;cha......