首页 > 其他分享 >实验5

实验5

时间:2022-12-24 21:33:54浏览次数:37  
标签:level int sum ++ STU 实验 printf

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 3
 4 typedef struct student
 5 {
 6     int id;
 7     char name[20];
 8     char subject[20];
 9     double perf;
10     double mid;
11     double final;
12     double total;
13     char level[10];
14 }STU;//定义STU表示student的结构体类型,然后可用STU来说明结构体变量
15 void input(STU[], int); 
16 void output(STU[], int);
17 void calc(STU[], int); 
18 int fail(STU[], STU[], int); 
19 void sort(STU[], int); 
20 int main()
21 {
22     STU st[N], fst[N]; // 数组st记录学生信息,fst记录不及格学生信息
23     int k; // 用于记录不及格学生个数
24     printf("录入学生成绩信息:\n");
25     input(st, N);// 录入学生信息
26     printf("\n成绩处理...\n");
27     calc(st, N);// 计算总评和等级
28     k = fail(st, fst, N);// 返回不及格学生个数
29     sort(st, N);
30     printf("\n学生成绩排名情况:\n");
31     output(st, N); // 输出学生信息
32     printf("\n不及格学生信息:\n");
33     output(fst, k);
34     return 0;
35 
36 }
37 void input(STU s[], int n)
38 {
39     int i;
40     for(i=0;i<n;i++)
41         scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,&s[i].perf, &s[i].mid, &s[i].final);
42 }
43 void output(STU s[], int n)
44 {
45     int i;
46     printf("-----------------\n");
47     printf("学号 姓名 科目 平时 期中 期末 总评 等级\n");
48         for (i = 0; i < n; i++)
49             printf("%d %-6s %-4s %-4.0f %-4.0f %-4.0f %-4.1f % s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total, s[i].level);
50 }
51 void calc(STU s[], int n)
52 {
53     int i; 
54     for (i = 0; i < n; i++) 
55     {
56         s[i].total = s[i].perf * 0.2 +s[i].mid * 0.2 +s[i].final * 0.6;
57         if (s[i].total >= 90)
58             strcpy(s[i].level, "优");
59         else if (s[i].total >= 80 && s[i].total < 90)
60             strcpy(s[i].level, "良");
61         else if (s[i].total >= 70 && s[i].total < 80)
62             strcpy(s[i].level, "中");
63         else if (s[i].total >= 60 && s[i].total < 70)
64             strcpy(s[i].level, "及格");
65         else
66             strcpy(s[i].level, "不及格");
67     }
68 }
69 int fail(STU s[], STU t[], int n) 
70 {
71     int i, k = 0;
72     for (i = 0; i < n; i++)
73         if (s[i].total < 60)
74             t[k++] = s[i];
75     return k;
76 }
77 void sort(STU s[], int n) 
78 {
79     int i, j;
80     STU t;
81     for (i = 0; i < n - 1; i++)
82         for (j = 0; j < n - 1 - i; j++)
83             if (s[j].total < s[j + 1].total) 
84             {
85                 t = s[j];
86                 s[j] = s[j + 1];
87                 s[j + 1] = t;
88             }
89 }

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 #define M 80
 5 typedef struct
 6 {
 7     char name[M]; // 书名
 8     char author[M]; // 作者
 9 }book;
10 int main() {
11     book x[N] = { {"《一九八四》", "乔治.奥威尔"},
12     {"《美丽新世界》", "赫胥黎"},
13     {"《昨日的世界》", "斯蒂芬.茨威格"},
14     {"《万历十五年》", "黄仁宇"},
15     {"《一只特立独行的猪》", "王小波"},
16     {"《百年孤独》", "马尔克斯"},
17     {"《情人》", "玛格丽特.杜拉斯"},
18     {"《只是孩子》", "帕蒂.史密斯"},
19     {"《刀锋》", "毛姆"},
20     {"《沉默的大多数》", "王小波"} };
21     book* ptr;
22     int i;
23     char author[M];
24     // 使用指针遍历结构体数组
25     printf("所有图书信息: \n");
26     for (ptr = x; ptr < x + N; ++ptr)
27         printf("%-30s%-30s\n", ptr->name, ptr->author);
28     // 查找指定作者的图书
29     printf("\n输入作者名: ");
30     gets_s(author);
31     for (ptr = x; ptr < x + N; ++ptr)
32         if (strcmp(ptr->author, author) == 0) 
33         {
34             printf("%-30s%-30s\n", ptr->name, ptr->author);
35         }
36     return 0;
37 }

 1 #include <stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define N 100
 5 typedef struct
 6 {
 7     char num[10];
 8     int s1;
 9     int s2;
10     double sum;
11     char level[10];
12 }STU;
13 int fun(STU a[], int n, STU h[]);
14 int main() {
15     STU s[N] = { 
16     {"GA05", 85, 76},
17     {"GA03", 76, 90},
18     {"GA02", 69, 90},
19     {"GA04", 85, 56},
20     {"GA01", 91, 95},
21     {"GA07", 72, 80},
22     {"GA08", 64, 45},
23     {"GA06", 87, 98},
24     {"GA015", 85, 86},
25     {"GA013", 91, 97} }; // 原始学生成绩记录
26     STU h[N]; // 保存均分以上学生记录
27     int i, k, n = 10;
28     // 调用fun对学生成绩记录进行处理
29     k = fun(s, n, h);
30     // 输出均分以上学生记录
31     printf("There are :\n");
32     for (i = 0; i < k; i++)
33         printf("%s %d %d %.2f %s\n", h[i].num, h[i].s1, h[i].s2, h[i].sum,h[i].level);
34     return 0;
35 }
36 int fun(STU a[], int n, STU h[]) 
37 {
38     int i,k=0;
39     double m = 0.0;
40     for (i = 0; i < n; i++)
41     {
42         a[i].sum= a[i].s1 * 0.7 + a[i].s2 * 0.3;
43         m = m + a[i].sum;
44     }
45     m = m / n;
46     for (i = 0; i < n; i++)
47     {
48         if (a[i].sum > m)
49         {
50             strcpy(h[k].num, a[i].num);
51             h[k].s1 = a[i].s1;
52             h[k].s2 = a[i].s2;
53             h[k].sum = a[i].sum;
54             strcpy(h[k].level, "均分以上");
55             k++;
56         }
57     }
58     return k;
59 }

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 typedef struct student 
 5 {
 6     char name[10];
 7     int num;
 8     int maths;
 9     int computer;
10     int english;
11     int sum;
12     char level[10];
13 } STU;
14 void fun(STU a[], int n);
15 int main() {
16     STU s[6 * N] = { {"A001", 1, 34, 67, 80},
17     {"B003", 3, 78, 87, 90},
18     {"A002", 2, 90, 98, 99},
19     {"B002", 4, 56, 78, 98},
20     {"A005", 5, 35, 67, 79} };
21     int i;
22     fun(s, N);
23     for (i = 0; i < N; i++)
24         printf("%s %d %d %d %d %d %s\n", s[i].name, s[i].num, s[i].maths,
25             s[i].computer, s[i].english, s[i].sum, s[i].level);
26     return 0;
27 }
28 void fun(STU a[], int n)
29 {
30     int i,max=0,min=500;
31     for (i = 0; i < n; i++)
32     {
33         a[i].sum = a[i].maths + a[i].computer + a[i].english;
34         if (a[i].sum > max)
35             max = a[i].sum;
36         if (a[i].sum < min)
37             min = a[i].sum;
38     }
39     for (i = 0; i < n; i++)
40     {
41         if (a[i].sum == max)
42             strcpy(a[i].level, "优秀");
43         else if (a[i].sum == min)
44             strcpy(a[i].level, "不及格");
45         else
46             strcpy(a[i].level, "合格");
47     }
48 }

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 typedef struct student {
 5     long no;
 6     char name[20];
 7     int score;
 8 } STU;
 9 // 定义结构体类型struct student, 并定义STU为其别名
10 void input(STU s[], int n);
11 int find_min_list(STU s[], STU t[], int n);
12 void output(STU s[], int n);
13 int main() 
14 {
15     STU stu[N], min_list[N];
16     int count;
17     printf("录入%d个学生信息\n", N);
18     input(stu, N);
19     printf("\n统计最低分人数和学生信息...\n");
20     count = find_min_list(stu, min_list, N);
21     printf("\n一共有%d个最低分,信息如下:\n", count);
22     output(min_list, count);
23     return 0;
24 }
25 void input(STU s[], int n) 
26 {
27     int i;
28     for (i = 0; i < n; i++)
29         scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score);
30 }
31 
32 void output(STU s[], int n) 
33 {
34     int i;
35     for (i = 0; i < n; i++)
36         printf("%ld %s %d\n",s[i].no, s[i].name,s[i].score);
37 }
38 int find_min_list(STU s[], STU t[], int n) 
39 {
40     int i,count=0;
41     int min = 100;
42     for (i = 0; i < n; i++)
43     {
44         if (s[i].score < min)
45         {
46             min = s[i].score;
47         }
48     }
49     for (i = 0; i < n; i++)
50     {
51         if (s[i].score == min)
52         {
53             t[count].no = s[i].no;
54             strcpy(t[count].name,s[i].name);
55             t[count].score = s[i].score;
56             count++;
57         }
58     }
59     return count;
60 }

 

标签:level,int,sum,++,STU,实验,printf
From: https://www.cnblogs.com/wr236/p/17003376.html

相关文章

  • 实验八-Web部署
    实验八-Web部署配置openEulercd/etc/yum.repos.d  viopenEuler_x86_64.repo  安装LAMP在shell中通过下面命令安装Apache:yuminstall-yhttpd  ......
  • 实验六
    实验任务四#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;intcount=0;fp=fopen("data4.txt","r"......
  • 实验八-web部署-20221308刘志伟
    实验步骤在华为云openEuler安装后,没有配置yum源,我们通过重新配置。输入命令以切换到rpos目录下cd/etc/yum.repos.d输入命令更换yum源viopenEuler_x86_64.repo增加......
  • 实验6
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){intcount=0;FILE*fp;fp=fopen("C:\\Users\\yuyee\\Des......
  • 实验6 文件应用编程
    实验4程序源码1#define_CRT_SECURE_NO_WARNINGS12#include<stdio.h>34intmain(){5FILE*fp;6fp=fopen("data4.txt","r");7if......
  • 实验6
    //将图书信息写入文本文件data1.txt#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#defineN7#defineM80typedefstruct{charname[M];//书名......
  • 实验5
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<string.h>#defineN3typedefstructstudent{intid;charname[20];charsubject[20......
  • 实验六
    实验任务4:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;if((fp=fopen("D:\\我的文件\\桌面......
  • 实验六
    #include<stdio.h>#include<string.h>#include<stdlib.h>#defineN10typedefstruct{longintid;charname[20];floatobjective;//客......
  • 实验6
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){charch;FILE*fp;fp=fopen("C:\\Users\\Mr.Yang\\Desktop\\c语言作业\\实验4\\dat......