对一个班某门课程成绩进行处理,用顺序表实现对一个班的一门课程成绩信息的显示、查询、插入、删除功能,并对已排好序(非递减有序)的两个班成绩合并,具体如下: 1. 定义成绩顺序表结构体。 typedef struct SqList { ElemType *elem; int length; int capacity; } SqList, *Ptr; typedef Ptr SqListPtr; 2. 实现循环菜单显示选择,并完成以下菜单功能:1.按位置查找2.按值查找3.插入4.删除5.输出6.合并有序表7.退出
时间: 2023-05-27 21:02:22 浏览: 194
以下为代码实现:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CAPACITY 100 // 最大容量
typedef int ElemType; // 成绩数据类型
typedef struct SqList {
ElemType *elem; // 动态数组存储成绩
int length; // 当前长度
int capacity; // 容量
} SqList, *Ptr;
typedef Ptr SqListPtr;
// 初始化顺序表
void initList(SqListPtr list, int capacity) {
list->elem = (ElemType*)malloc(sizeof(ElemType) * capacity);
if (list->elem == NULL) {
printf("内存分配失败!\n");
exit(EXIT_FAILURE);
}
list->length = 0;
list->capacity = capacity;
}
// 找到指定位置处的元素,并返回它的下标
int locateElem(SqList list, int pos) {
if (pos < 1 || pos > list.length) {
printf("查找位置超出范围!\n");
return -1;
}
return pos - 1;
}
// 按位置查找成绩
void findByPos(SqList list) {
int pos;
printf("请输入要查找的位置:");
scanf("%d", &pos);
int index = locateElem(list, pos);
if (index == -1) {
return;
}
printf("第%d个学生的成绩是:%d\n", pos, list.elem[index]);
}
// 按值查找成绩
void findByValue(SqList list) {
int value;
printf("请输入要查找的成绩:");
scanf("%d", &value);
int index = -1;
for (int i = 0; i < list.length; i++) {
if (list.elem[i] == value) {
index = i;
break;
}
}
if (index == -1) {
printf("未找到该成绩!\n");
return;
}
printf("第%d个学生的成绩是:%d\n", index + 1, value);
}
// 插入成绩
void insertValue(SqListPtr list) {
printf("请输入要插入的位置和成绩,以空格分隔:");
int pos, value;
scanf("%d %d", &pos, &value);
if (list->length == list->capacity) {
printf("顺序表已满,无法插入!\n");
return;
}
int index = locateElem(*list, pos);
if (index == -1) {
return;
}
for (int i = list->length - 1; i >= index; i--) { // 从最后一个元素开始向后移动
list->elem[i + 1] = list->elem[i];
}
list->elem[index] = value;
list->length++;
printf("插入成功!\n");
}
// 删除成绩
void deleteValue(SqListPtr list) {
printf("请输入要删除的位置:");
int pos;
scanf("%d", &pos);
int index = locateElem(*list, pos);
if (index == -1) {
return;
}
for (int i = index; i < list->length - 1; i++) { // 从要删除的元素位置开始向前移动
list->elem[i] = list->elem[i + 1];
}
list->length--;
printf("删除成功!\n");
}
// 输出所有成绩
void printAll(SqList list) {
if (list.length == 0) {
printf("成绩表为空!\n");
return;
}
printf("成绩列表如下:\n");
for (int i = 0; i < list.length; i++) {
printf("%d ", list.elem[i]);
}
printf("\n");
}
// 合并有序表
SqList mergeLists(SqList list1, SqList list2) {
SqList newList;
initList(&newList, list1.length + list2.length);
int p1 = 0, p2 = 0;
while (p1 < list1.length && p2 < list2.length) {
if (list1.elem[p1] <= list2.elem[p2]) {
newList.elem[newList.length++] = list1.elem[p1++];
} else {
newList.elem[newList.length++] = list2.elem[p2++];
}
}
while (p1 < list1.length) { // 将剩余元素依次加入新表中
newList.elem[newList.length++] = list1.elem[p1++];
}
while (p2 < list2.length) {
newList.elem[newList.length++] = list2.elem[p2++];
}
return newList;
}
// 显示菜单
void showMenu() {
printf("===================\n");
printf("1. 按位置查找\n");
printf("2. 按值查找\n");
printf("3. 插入\n");
printf("4. 删除\n");
printf("5. 输出\n");
printf("6. 合并有序表\n");
printf("7. 退出\n");
printf("===================\n");
}
int main() {
SqList list1, list2, newList;
initList(&list1, MAX_CAPACITY);
initList(&list2, MAX_CAPACITY);
// 初始化班级1和班级2的成绩
list1.elem[0] = 67;
list1.elem[1] = 72;
list1.elem[2] = 80;
list1.elem[3] = 85;
list1.elem[4] = 92;
list1.length = 5;
list2.elem[0] = 65;
list2.elem[1] = 70;
list2.elem[2] = 78;
list2.elem[3] = 86;
list2.elem[4] = 94;
list2.length = 5;
int choice;
do {
showMenu();
printf("请输入要执行的操作序号:");
scanf("%d", &choice);
switch (choice) {
case 1:
findByPos(list1);
break;
case 2:
findByValue(list1);
break;
case 3:
insertValue(&list1);
break;
case 4:
deleteValue(&list1);
break;
case 5:
printAll(list1);
break;
case 6:
printf("正在合并有序表,请稍等...\n");
newList = mergeLists(list1, list2);
printf("合并后的成绩列表如下:\n");
for (int i = 0; i < newList.length; i++) {
printf("%d ", newList.elem[i]);
}
printf("\n");
break;
case 7:
printf("程序已退出!\n");
break;
default:
printf("输入的操作序号无效,请重新输入!\n");
}
} while (choice != 7);
// 释放内存
free(list1.elem);
free(list2.elem);
if (newList.elem != NULL) {
free(newList.elem);
}
return 0;
}
阅读全文