题目描述

屎山测试第二趴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
某校的惯例是在每学期的期末考试之后发放奖学金。发 放
的奖学金共有五种,每项奖学金获取的条件分别如下:
1) 院士奖学金:期末平均成绩高于 80 分(>80),并且在本学期内发表 1 篇或
1 篇以上论文的学生每人均可获得 8000 元;
2) 五四奖学金:期末平均成绩高于 85 分(>85),并且班级评议成绩高于 80
(>80)的学生每人均可获得 4000 元;
3) 成绩优秀奖:期末平均成绩高于 90 分(>90)的学生每人均可获得 2000 元;
4) 西部奖学金:期末平均成绩高于 85 分(>85)的西部省份学生每人均可获得
1000 元;
5) 班级贡献奖:班级评议成绩高于 80 分(>80)的学生干部每人均可获得 850
元;
只要符合上述条件就可获得相应的奖项,每项奖学金的获奖人数没有限制,每
名学生也可以同时获得多项奖学金。例如姚明的期末平均成绩是 87 分,班级
评议成绩 82 分,同时他还是一位学生干部,那么他可以同时获得五四奖学金
和班级贡献奖,奖金总数是 4850 元。
现在给出若干学生的相关数据(假设总有同学能满足获得奖学金的条件),
程序运行时,要先输入学生人数。提示:"Input n:",然后逐个输入学生信息:
输入学生姓名提示:"Input name:"
输入学生期末平均成绩提示:"Input final score:"
输入学生班级评议成绩提示:"Input class score:"
输入是否为学生干部提示:"Class cadre or not?(Y/N):"
输入是否为西部学生提示:"Students from the West or not?(Y/N):"
输入发表文章数量提示:"Input the number of published papers:"
1.请定义结构体类型 S
2. 编程输入所有学生的数据(定义 input 函数 )
3.将所有的学生按照姓名进行排序(定义 sort 函数 10 分)
4.后输出每个人的信息(print 函数)
5.计算每个同学获得的奖金总数(定义 addup 函数)
6.并找到奖金最高者(定义 findMax 函数 5 分),打印出他的名字和奖金数额。
7.主函数。注意请不要使用全局变量。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct S {
char name[50];
float final_score;
float class_score;
char is_cadre; // 'Y'表示是学生干部,'N'表示不是
char is_west; // 'Y'表示来自西部省份,'N'表示不是
int published_papers;
int scholarship;
};

// 定义输入函数
void input(struct S *s) {
printf("Input name: ");
scanf("%s", s->name);
printf("Input final score: ");
scanf("%f", &(s->final_score));
printf("Input class score: ");
scanf("%f", &(s->class_score));
printf("Class cadre or not? (Y/N): ");
scanf(" %c", &(s->is_cadre));
printf("Students from the West or not? (Y/N): ");
scanf(" %c", &(s->is_west));
printf("Input the number of published papers: ");
scanf("%d", &(s->published_papers));
}

// 定义排序函数(造个小轮子方便引用qsort)
int sort(const void *a, const void *b) {
return strcmp(((struct S *)a)->name, ((struct S *)b)->name);
}

void print(struct S *s) {
printf("Name: %s\n", s->name);
printf("Final score: %.2f\n", s->final_score);
printf("Class score: %.2f\n", s->class_score);
printf("Class cadre: %c\n", s->is_cadre);
printf("Students from the West: %c\n", s->is_west);
printf("Published papers: %d\n", s->published_papers);
printf("Scholarship: %d\n", s->scholarship);
printf("\n");
}

void addup(struct S *s) {
s->scholarship = 0;

if (s->final_score > 80 && s->published_papers >= 1) {
s->scholarship += 8000;
}
if (s->final_score > 85 && s->class_score > 80) {
s->scholarship += 4000;
}
if (s->final_score > 90) {
s->scholarship += 2000;
}
if (s->final_score > 85 && s->is_west == 'Y') {
s->scholarship += 1000;
}
if (s->class_score > 80 && s->is_cadre == 'Y') {
s->scholarship += 850;
}
}

struct S findMax(struct S *students, int n) {
struct S maxStudent = students[0];

for (int i = 1; i < n; i++) {
if (students[i].scholarship > maxStudent.scholarship) {
maxStudent = students[i];
}
}
return maxStudent;
}


int main() {
int n;
printf("Input n: ");
scanf("%d", &n);
struct S *students = (struct S *)malloc(n * sizeof(struct S));
for (int i = 0; i < n; i++) {
printf("\nEnter data for student %d:\n", i + 1);
input(&students[i]);
addup(&students[i]);
}

qsort(students, n, sizeof(struct S), sort);//按名字排序
printf("\nStudent information:\n");
for (int i = 0; i < n; i++) {
print(&students[i]);
}

struct S maxStudent = findMax(students, n);
printf("\nStudent with the highest scholarship:\n");
print(&maxStudent);
free(students);

return 0;
}