C语言定义并创建一个包含学生信息(学号,姓名,成绩)的顺序表,使其具有如下功能: (1)根据指定学生个数,逐个输入学生信息并以顺序结构存储; (2)逐个显示学生表中所有学生的相关信息; (3)根据姓名进行查找,返回此学生的学号和成绩; (4)根据指定的位置可返回相应的学生信息(学号,姓名,成绩)(5)给定一个学生信息,插入到表中指定的位置; (6)删除指定位置的学生记录; (7)统计表中学生个数
时间: 2023-06-02 21:02:01 浏览: 117
1_lower7y7_定义一个包含学生信息(学号_成绩)的顺序表和链表(二选一)_姓名_使其具有如下功能_
5星 · 资源好评率100%
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
struct Student {
int id;
char name[10];
int score;
};
struct StudentList {
struct Student student[MAX_SIZE];
int length;
};
void init(struct StudentList *list) {
list->length = 0;
}
void add(struct StudentList *list) {
int n, i;
printf("请输入学生个数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%d", &list->student[i].id);
printf("姓名:");
scanf("%s", list->student[i].name);
printf("成绩:");
scanf("%d", &list->student[i].score);
list->length++;
}
}
void show(struct StudentList *list) {
int i;
printf("学号\t姓名\t成绩\n");
for (i = 0; i < list->length; i++) {
printf("%d\t%s\t%d\n", list->student[i].id, list->student[i].name, list->student[i].score);
}
}
void find(struct StudentList *list) {
char name[10];
int i, flag = 0;
printf("请输入要查找的学生姓名:");
scanf("%s", name);
for (i = 0; i < list->length; i++) {
if (strcmp(list->student[i].name, name) == 0) {
printf("学号:%d,成绩:%d\n", list->student[i].id, list->student[i].score);
flag = 1;
break;
}
}
if (!flag) {
printf("未找到该学生!\n");
}
}
void get(struct StudentList *list) {
int index;
printf("请输入要查询的位置:");
scanf("%d", &index);
if (index < 1 || index > list->length) {
printf("位置不合法!\n");
} else {
printf("学号:%d,姓名:%s,成绩:%d\n", list->student[index - 1].id, list->student[index - 1].name, list->student[index - 1].score);
}
}
void insert(struct StudentList *list) {
int index, i;
struct Student s;
printf("请输入要插入的位置:");
scanf("%d", &index);
if (index < 1 || index > list->length + 1) {
printf("位置不合法!\n");
} else {
printf("请输入要插入的学生信息:\n");
printf("学号:");
scanf("%d", &s.id);
printf("姓名:");
scanf("%s", s.name);
printf("成绩:");
scanf("%d", &s.score);
for (i = list->length; i >= index; i--) {
list->student[i] = list->student[i - 1];
}
list->student[index - 1] = s;
list->length++;
}
}
void remove_student(struct StudentList *list) {
int index, i;
printf("请输入要删除的位置:");
scanf("%d", &index);
if (index < 1 || index > list->length) {
printf("位置不合法!\n");
} else {
for (i = index; i < list->length; i++) {
list->student[i - 1] = list->student[i];
}
list->length--;
}
}
void count(struct StudentList *list) {
printf("学生个数:%d\n", list->length);
}
int main() {
struct StudentList list;
init(&list);
int choice;
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");
printf("===================================\n");
printf("请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
add(&list);
break;
case 2:
show(&list);
break;
case 3:
find(&list);
break;
case 4:
get(&list);
break;
case 5:
insert(&list);
break;
case 6:
remove_student(&list);
break;
case 7:
count(&list);
break;
case 0:
printf("程序已退出!\n");
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
} while (choice != 0);
return 0;
}
阅读全文