C语言对所有学生按照总分进行排名,如果总分相同,则按照核心课总分高低进行排名。
时间: 2024-05-10 13:19:12 浏览: 6
可以使用结构体来实现这个功能,具体步骤如下:
1. 定义一个学生结构体,包含学生的姓名、总分和核心课总分。
```c
struct student {
char name[20];
int total_score;
int core_score;
};
```
2. 声明一个数组来存储学生信息。
```c
struct student stu[100];
```
3. 编写输入学生信息的函数。
```c
void input_student_info(struct student stu[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的姓名、总分和核心课总分:", i + 1);
scanf("%s %d %d", stu[i].name, &stu[i].total_score, &stu[i].core_score);
}
}
```
4. 编写按总分和核心课总分排序的函数。
```c
void sort_students(struct student stu[], int n) {
int i, j;
struct student temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (stu[j].total_score < stu[j + 1].total_score) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
} else if (stu[j].total_score == stu[j + 1].total_score) {
if (stu[j].core_score < stu[j + 1].core_score) {
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
}
```
5. 编写输出学生信息的函数。
```c
void output_student_info(struct student stu[], int n) {
int i;
printf("姓名\t总分\t核心课总分\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t%d\n", stu[i].name, stu[i].total_score, stu[i].core_score);
}
}
```
6. 在主函数中调用这些函数。
```c
int main() {
int n;
printf("请输入学生人数:");
scanf("%d", &n);
input_student_info(stu, n);
sort_students(stu, n);
output_student_info(stu, n);
return 0;
}
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)