C语言定义并创建一个包含学生信息(学号,姓名,成绩)的顺序表,使其具有如下功能: (1)根据指定学生个数,逐个输入学生信息并以顺序结构存储; (2)逐个显示学生表中所有学生的相关信息; (3)根据姓名进行查找,返回此学生的学号和成绩; (4)根据指定的位置可返回相应的学生信息(学号,姓名,成绩)(5)给定一个学生信息,插入到表中指定的位置; (6)删除指定位置的学生记录; (7)统计表中学生个数
时间: 2023-06-02 20:01:37 浏览: 118
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 50 // 学生信息表最大容量
// 学生信息结构体
typedef struct {
int id; // 学号
char name[20]; // 姓名
float score; // 成绩
} Student;
// 顺序表结构体
typedef struct {
Student data[MAXSIZE]; // 学生信息表
int length; // 学生信息表当前长度
} SqList;
// 初始化学生信息表
void InitList(SqList *L) {
L->length = 0;
printf("学生信息表已初始化!\n");
}
// 逐个输入学生信息并以顺序结构存储
void CreateList(SqList *L) {
int n;
printf("请输入学生个数:");
scanf("%d", &n);
if (n > MAXSIZE) {
printf("学生个数超过学生信息表最大容量!");
exit(1);
}
printf("请依次输入%d个学生的信息:\n", n);
for (int i = 0; i < n; i++) {
printf("第%d个学生:\n", i + 1);
printf("学号:");
scanf("%d", &L->data[i].id);
printf("姓名:");
scanf("%s", L->data[i].name);
printf("成绩:");
scanf("%f", &L->data[i].score);
L->length++;
}
printf("学生信息表已创建!\n");
}
// 逐个显示学生表中所有学生的相关信息
void ShowList(SqList L) {
if (L.length == 0) {
printf("学生信息表为空!\n");
return;
}
printf("学生信息表中所有学生的信息如下:\n");
printf("学号\t姓名\t成绩\n");
for (int i = 0; i < L.length; i++) {
printf("%d\t%s\t%.2f\n", L.data[i].id, L.data[i].name, L.data[i].score);
}
}
// 根据姓名进行查找,返回此学生的学号和成绩
void SearchByName(SqList L) {
if (L.length == 0) {
printf("学生信息表为空!\n");
return;
}
char name[20];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
for (int i = 0; i < L.length; i++) {
if (strcmp(L.data[i].name, name) == 0) {
printf("%s的学号是%d,成绩是%.2f\n", name, L.data[i].id, L.data[i].score);
return;
}
}
printf("找不到姓名为%s的学生!\n", name);
}
// 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)
void GetByPos(SqList L) {
if (L.length == 0) {
printf("学生信息表为空!\n");
return;
}
int pos;
printf("请输入要查找的学生位置:");
scanf("%d", &pos);
if (pos < 1 || pos > L.length) {
printf("位置超出学生信息表范围!\n");
return;
}
printf("第%d个学生的信息是:\n", pos);
printf("学号:%d,姓名:%s,成绩:%.2f\n", L.data[pos - 1].id, L.data[pos - 1].name, L.data[pos - 1].score);
}
// 给定一个学生信息,插入到表中指定的位置
void InsertList(SqList *L) {
if (L->length == MAXSIZE) {
printf("学生信息表已满,无法插入!\n");
return;
}
int pos;
printf("请输入要插入的位置:");
scanf("%d", &pos);
if (pos < 1 || pos > L->length + 1) {
printf("位置超出学生信息表范围!\n");
return;
}
Student s;
printf("请输入要插入的学生信息:\n");
printf("学号:");
scanf("%d", &s.id);
printf("姓名:");
scanf("%s", s.name);
printf("成绩:");
scanf("%f", &s.score);
for (int i = L->length; i >= pos; i--) {
L->data[i] = L->data[i - 1];
}
L->data[pos - 1] = s;
L->length++;
printf("学生信息插入成功!\n");
}
// 删除指定位置的学生记录
void DeleteList(SqList *L) {
if (L->length == 0) {
printf("学生信息表为空!\n");
return;
}
int pos;
printf("请输入要删除的位置:");
scanf("%d", &pos);
if (pos < 1 || pos > L->length) {
printf("位置超出学生信息表范围!\n");
return;
}
for (int i = pos - 1; i < L->length - 1; i++) {
L->data[i] = L->data[i + 1];
}
L->length--;
printf("学生信息删除成功!\n");
}
// 统计表中学生个数
void CountList(SqList L) {
printf("学生信息表中共有%d个学生!\n", L.length);
}
int main() {
SqList L;
InitList(&L); // 初始化学生信息表
CreateList(&L); // 逐个输入学生信息并以顺序结构存储
ShowList(L); // 逐个显示学生表中所有学生的相关信息
SearchByName(L); // 根据姓名进行查找,返回此学生的学号和成绩
GetByPos(L); // 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)
InsertList(&L); // 给定一个学生信息,插入到表中指定的位置
ShowList(L); // 逐个显示学生表中所有学生的相关信息
DeleteList(&L); // 删除指定位置的学生记录
ShowList(L); // 逐个显示学生表中所有学生的相关信息
CountList(L); // 统计表中学生个数
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)