定义一个描述学生基本信息的结构体类型,包括姓名,学号,C语言课程成绩。并定义一个结构体数组。输入若干学生的基本信息;编写函数print(),输出全体学生记录信息;编写函数search(), 按学号检索指定学生信息并返回, 由主函数打印到屏幕上;编写函数modify(), 修改指定学号的学生的成绩;编写函数sort(), 按照学生学号或者成绩对结构体数组排序;编写函数insert(), 插入一条新的学生信息;编写函数delete(), 删除指定学号的学生信息;并改写为多文件形式
时间: 2024-02-23 07:58:11 浏览: 87
对于这个问题,我可以为您提供一个基本的解决方案,代码如下:
```c
// student.h 文件
#ifndef STUDENT_H
#define STUDENT_H
#define MAX_STUDENT_NUM 100
struct student {
char name[20]; // 姓名
char id[10]; // 学号
int score; // C语言课程成绩
};
typedef struct student STUDENT;
void print(STUDENT* students, int num);
STUDENT* search(STUDENT* students, int num, char* id);
void modify(STUDENT* students, int num, char* id, int score);
void sort(STUDENT* students, int num, int by);
void insert(STUDENT* students, int* num, STUDENT new_student);
void delete(STUDENT* students, int* num, char* id);
#endif /* STUDENT_H */
```
```c
// student.c 文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "student.h"
void print(STUDENT* students, int num) {
int i;
printf("姓名\t学号\t成绩\n");
for (i = 0; i < num; i++) {
printf("%s\t%s\t%d\n", students[i].name, students[i].id, students[i].score);
}
}
STUDENT* search(STUDENT* students, int num, char* id) {
int i;
for (i = 0; i < num; i++) {
if (strcmp(students[i].id, id) == 0) {
return &students[i];
}
}
return NULL;
}
void modify(STUDENT* students, int num, char* id, int score) {
STUDENT* p = search(students, num, id);
if (p != NULL) {
p->score = score;
}
}
void sort(STUDENT* students, int num, int by) {
int i, j;
STUDENT temp;
switch (by) {
case 0: // 按学号排序
for (i = 0; i < num - 1; i++) {
for (j = i + 1; j < num; j++) {
if (strcmp(students[i].id, students[j].id) > 0) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
break;
case 1: // 按成绩排序
for (i = 0; i < num - 1; i++) {
for (j = i + 1; j < num; j++) {
if (students[i].score > students[j].score) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
break;
default:
break;
}
}
void insert(STUDENT* students, int* num, STUDENT new_student) {
if (*num >= MAX_STUDENT_NUM) {
printf("已达到最大学生数目,无法继续添加\n");
return;
}
students[*num] = new_student;
(*num)++;
}
void delete(STUDENT* students, int* num, char* id) {
int i, j;
for (i = 0; i < *num; i++) {
if (strcmp(students[i].id, id) == 0) {
for (j = i; j < *num - 1; j++) {
students[j] = students[j + 1];
}
(*num)--;
return;
}
}
printf("未找到该学号对应的学生信息\n");
}
```
```c
// main.c 文件
#include <stdio.h>
#include "student.h"
int main() {
int num = 0, choice, by, score;
char id[10], name[20];
STUDENT students[MAX_STUDENT_NUM], new_student, *p;
do {
printf("请选择操作:\n");
printf("1. 输入学生信息\n");
printf("2. 输出全体学生记录信息\n");
printf("3. 检索学生信息\n");
printf("4. 修改学生信息\n");
printf("5. 排序学生信息\n");
printf("6. 插入学生信息\n");
printf("7. 删除学生信息\n");
printf("0. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("请输入学生信息:\n");
printf("姓名:");
scanf("%s", name);
printf("学号:");
scanf("%s", id);
printf("成绩:");
scanf("%d", &score);
new_student.score = score;
strcpy(new_student.name, name);
strcpy(new_student.id, id);
insert(students, &num, new_student);
break;
case 2:
print(students, num);
break;
case 3:
printf("请输入要检索的学号:");
scanf("%s", id);
p = search(students, num, id);
if (p != NULL) {
printf("姓名:%s,学号:%s,成绩:%d\n", p->name, p->id, p->score);
} else {
printf("未找到该学号对应的学生信息\n");
}
break;
case 4:
printf("请输入要修改的学号:");
scanf("%s", id);
printf("请输入新的成绩:");
scanf("%d", &score);
modify(students, num, id, score);
break;
case 5:
printf("请选择排序方式(0-按学号排序,1-按成绩排序):");
scanf("%d", &by);
sort(students, num, by);
printf("已排序\n");
break;
case 6:
printf("请输入学生信息:\n");
printf("姓名:");
scanf("%s", name);
printf("学号:");
scanf("%s", id);
printf("成绩:");
scanf("%d", &score);
new_student.score = score;
strcpy(new_student.name, name);
strcpy(new_student.id, id);
insert(students, &num, new_student);
break;
case 7:
printf("请输入要删除的学号:");
scanf("%s", id);
delete(students, &num, id);
break;
case 0:
printf("程序已退出\n");
break;
default:
printf("输入了错误的选项,请重新输入\n");
break;
}
} while (choice != 0);
return 0;
}
```
以上就是一个简单的学生信息管理系统的实现,包括了基本的增删改查和排序功能。其中,将每个功能的实现都封装在了一个函数中,并在头文件中声明了这些函数的原型,以便在其他文件中调用。
阅读全文