c语言编写文本文件读入学生信息,排序后输出 题目描述 某班开设有英语、数学、程序设计三门课程,该班同学信息保存在文本文件student.txt中,编写程序从文件读入该班同学信息,分别按总成绩和英语成绩排序。该班同学不多于50人。 输入描述 文本文件student.txt,每行数据描述一个同学信息,包括名字、英语、数学、程序设计成绩 输出描述 该班同学总成绩排序和英语成绩排序,成绩相同时按输入次序。 每项占12位。 样例输入 文本文件student.txt,内容格式如下 张三 80 90 88 李四 66 80 90 王五 50 78 87 Jack 68 86 70 Tom 60 88 96 样例输出 Name English Math C Total 张三 80 90 88 258 Tom 60 88 96 244 李四 66 80 90 236 Jack 68 86 70 224 王五 50 78 87 215 Name English Math C Total 张三 80 90 88 258 Jack 68 86 70 224 李四 66 80 90 236 Tom 60 88 96 244 王五 50 78 87 215
时间: 2024-01-06 21:04:14 浏览: 85
以下是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char name[20]; // 姓名
int english; // 英语成绩
int math; // 数学成绩
int c; // 程序设计成绩
int total; // 总成绩
} Student;
// 读取学生信息到数组
int readStudents(Student students[]) {
FILE *fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("Failed to open file!\n");
return -1;
}
int i = 0;
while (!feof(fp)) {
fscanf(fp, "%s %d %d %d", students[i].name, &students[i].english, &students[i].math, &students[i].c);
students[i].total = students[i].english + students[i].math + students[i].c;
i++;
}
fclose(fp);
return i;
}
// 按总成绩排序
void sortByTotal(Student students[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (students[i].total < students[j].total) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 按英语成绩排序
void sortByEnglish(Student students[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (students[i].english < students[j].english) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 输出学生信息
void printStudents(Student students[], int n) {
printf("%-12s%-12s%-12s%-12s%s\n", "Name", "English", "Math", "C", "Total");
for (int i = 0; i < n; i++) {
printf("%-12s%-12d%-12d%-12d%d\n", students[i].name, students[i].english, students[i].math, students[i].c, students[i].total);
}
}
int main() {
Student students[50];
int n = readStudents(students);
sortByTotal(students, n);
printStudents(students, n);
sortByEnglish(students, n);
printStudents(students, n);
return 0;
}
```
程序先定义了一个学生结构体,包含姓名、英语成绩、数学成绩、程序设计成绩和总成绩。然后定义了读取学生信息、按总成绩排序、按英语成绩排序和输出学生信息等函数。主函数中先调用读取学生信息函数,得到学生数量和学生数组。然后按总成绩排序并输出,再按英语成绩排序并输出。注意输出格式要求,可以使用`%-12s`和`%-12d`等格式控制符实现固定宽度输出。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)