c语言•设班里有n位同学,每位同学的数据包括以下内容:学号(长整型)、姓名(字符串)、数学成绩(整型)、程序设计(课程名称为:程序设计,整型)。设计程序完成以如下功能 •1) 成绩录入; •2) 显示学生成绩 •3) 按照总分排名(从高到低) •4)输入一个学生的学号能查找到学生的成绩。 注:输入数据时,要求学号不能相同,姓名可以相同。建议采用结构体数组。 •具体要求如下 程序运行时,首先显示主菜单如下 1)学生成绩录入 2)总分排序 3)查询 4)数据显示。 学号查询:输入学号后,若该学号存在则显示与其相关的所有信息,否则显示找不到的提示信息; (提示:查询到满足条件的结果后,查询即可结束)
时间: 2023-06-23 18:10:22 浏览: 191
以下是实现这个功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
long id;
char name[20];
int math_score;
int programming_score;
int total_score;
} Student;
void input(Student students[], int *num_students);
void display(Student students[], int num_students);
void sort(Student students[], int num_students);
void query(Student students[], int num_students);
int main() {
Student students[MAX_STUDENTS];
int num_students = 0;
int choice;
do {
printf("Main menu:\n");
printf("1) Input student scores\n");
printf("2) Sort by total score\n");
printf("3) Query by ID\n");
printf("4) Display all data\n");
printf("0) Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
input(students, &num_students);
break;
case 2:
sort(students, num_students);
display(students, num_students);
break;
case 3:
query(students, num_students);
break;
case 4:
display(students, num_students);
break;
case 0:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice, please try again.\n");
break;
}
} while (choice != 0);
return 0;
}
void input(Student students[], int *num_students) {
int i;
int found;
Student new_student;
printf("Enter student data:\n");
printf("ID: ");
scanf("%ld", &new_student.id);
found = 0;
for (i = 0; i < *num_students; i++) {
if (new_student.id == students[i].id) {
found = 1;
break;
}
}
if (found) {
printf("Error: student ID already exists.\n");
return;
}
printf("Name: ");
scanf("%s", new_student.name);
printf("Math score: ");
scanf("%d", &new_student.math_score);
printf("Programming score: ");
scanf("%d", &new_student.programming_score);
new_student.total_score = new_student.math_score + new_student.programming_score;
students[*num_students] = new_student;
(*num_students)++;
printf("Data entered successfully.\n");
}
void display(Student students[], int num_students) {
int i;
printf("%-15s %-20s %-15s %-15s %-15s\n", "ID", "Name", "Math score", "Programming score", "Total score");
for (i = 0; i < num_students; i++) {
printf("%-15ld %-20s %-15d %-15d %-15d\n", students[i].id, students[i].name, students[i].math_score,
students[i].programming_score, students[i].total_score);
}
}
void sort(Student students[], int num_students) {
int i, j;
Student temp;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[j].total_score > students[i].total_score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void query(Student students[], int num_students) {
long id;
int i;
int found = 0;
printf("Enter student ID: ");
scanf("%ld", &id);
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("%-15s %-20s %-15s %-15s %-15s\n", "ID", "Name", "Math score", "Programming score", "Total score");
printf("%-15ld %-20s %-15d %-15d %-15d\n", students[i].id, students[i].name, students[i].math_score,
students[i].programming_score, students[i].total_score);
found = 1;
break;
}
}
if (!found) {
printf("Student not found.\n");
}
}
```
这个程序使用了结构体数组存储学生数据,包括学号、姓名、数学成绩、程序设计成绩和总分。主菜单提供了四个选项:学生成绩录入、总分排序、查询和数据显示。在录入学生成绩时,程序会检查学号是否已经存在,如果存在则提示错误。在总分排序时,程序会先对学生数据进行排序,然后输出排名结果。在查询时,程序会要求用户输入学号,并且通过遍历结构体数组来查找学生数据。
阅读全文