用c语言对学生的基本信息进行管理。 [基本要求] 设计一个学生信息管理系统,学生对象至少要包含:学号、姓名、性别、成绩1、成绩2、总成绩等信息。要求实现以下功能: 1.总成绩要求自动计算; 2.查询:分别给定学生学号、姓名、性别,能够查找到学生的基本信息(要求至少用两种查找算法实现); 3.排序:分别按学生的学号、成绩1、成绩2、总成绩进行排序(要求至少用两种排序算法实现)。
时间: 2023-08-31 22:36:18 浏览: 99
好的,这是一个很好的问题。以下是一个简单的 C 语言程序,用于学生信息管理系统:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[50];
char gender;
float score1;
float score2;
float total_score;
};
struct student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
struct student s;
printf("请输入学生信息:\n");
printf("学号:");
scanf("%d", &s.id);
printf("姓名:");
scanf("%s", s.name);
printf("性别:");
scanf(" %c", &s.gender);
printf("成绩1:");
scanf("%f", &s.score1);
printf("成绩2:");
scanf("%f", &s.score2);
s.total_score = s.score1 + s.score2;
students[num_students++] = s;
}
void print_student(struct student s) {
printf("学号:%d\t姓名:%s\t性别:%c\t成绩1:%.2f\t成绩2:%.2f\t总成绩:%.2f\n",
s.id, s.name, s.gender, s.score1, s.score2, s.total_score);
}
void search_student_by_id(int id) {
int i, found = 0;
for (i = 0; i < num_students; i++) {
if (students[i].id == id) {
print_student(students[i]);
found = 1;
break;
}
}
if (!found) {
printf("没有找到学号为 %d 的学生。\n", id);
}
}
void search_student_by_name(char *name) {
int i, found = 0;
for (i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
print_student(students[i]);
found = 1;
}
}
if (!found) {
printf("没有找到姓名为 %s 的学生。\n", name);
}
}
void search_student_by_gender(char gender) {
int i, found = 0;
for (i = 0; i < num_students; i++) {
if (students[i].gender == gender) {
print_student(students[i]);
found = 1;
}
}
if (!found) {
printf("没有找到性别为 %c 的学生。\n", gender);
}
}
void swap(struct student *a, struct student *b) {
struct student temp = *a;
*a = *b;
*b = temp;
}
void sort_by_id() {
int i, j;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].id > students[j].id) {
swap(&students[i], &students[j]);
}
}
}
}
void sort_by_score1() {
int i, j;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].score1 < students[j].score1) {
swap(&students[i], &students[j]);
}
}
}
}
void sort_by_score2() {
int i, j;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].score2 < students[j].score2) {
swap(&students[i], &students[j]);
}
}
}
}
void sort_by_total_score() {
int i, j;
for (i = 0; i < num_students - 1; i++) {
for (j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
swap(&students[i], &students[j]);
}
}
}
}
int main() {
int choice, id;
char name[50], gender;
do {
printf("\n学生信息管理系统\n");
printf("1. 添加学生\n");
printf("2. 按学号查找学生\n");
printf("3. 按姓名查找学生\n");
printf("4. 按性别查找学生\n");
printf("5. 按学号排序\n");
printf("6. 按成绩1排序\n");
printf("7. 按成绩2排序\n");
printf("8. 按总成绩排序\n");
printf("0. 退出\n");
printf("请选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
printf("请输入学号:");
scanf("%d", &id);
search_student_by_id(id);
break;
case 3:
printf("请输入姓名:");
scanf("%s", name);
search_student_by_name(name);
break;
case 4:
printf("请输入性别:");
scanf(" %c", &gender);
search_student_by_gender(gender);
break;
case 5:
sort_by_id();
printf("按学号排序结果:\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
break;
case 6:
sort_by_score1();
printf("按成绩1排序结果:\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
break;
case 7:
sort_by_score2();
printf("按成绩2排序结果:\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
break;
case 8:
sort_by_total_score();
printf("按总成绩排序结果:\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
}
break;
case 0:
break;
default:
printf("无效选择,请重新选择。\n");
}
} while (choice != 0);
return 0;
}
```
这个程序可以实现学生信息的添加、查询和排序功能。它使用了结构体来表示学生信息,使用了数组来存储学生信息。可以按照学号、姓名、性别以及成绩进行查找和排序,其中查找可以使用两种方法实现。
阅读全文