菜单式顺序表(简陋版)
作者:互联网
1.菜单页面
2.创建顺序表
3.顺序表的插入
4.顺序表的删除
5.源代码
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//宏定义
#define MAXSIZE 20
#define XS 35
#define YH 12
#define WIDTH 20
#define HEIGHT 20
//定义顺序表的结构体类型
typedef struct Node{
int *elem;
int length;
}SqList;
//函数的声明
void gotoxy( int x, int y); //坐标移动函数
void CreatList(SqList &L); //顺序表的创建函数
void window();//窗口
void menu(SqList &L); //菜单
void insert(SqList &L);//插入
void TraveList(SqList &L);//遍历打印
void deletList(SqList &L);//顺序表的删除操作
//坐标移动函数
void gotoxy( int x, int y)
{
COORD pos;
pos.X = x - 1;
pos.Y = y - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
//顺序表的创建函数
void CreatList(SqList &L){
window();
gotoxy(XS, YH - 9);
L.elem = (int *)malloc(sizeof(int) * MAXSIZE);
if(!L.elem){
printf("ERROR\n");
exit(0);
}
printf("please input you want length:\t");
scanf("%d", &L.length);
gotoxy(XS, YH - 8);
printf("please input %d number\n", L.length);
gotoxy(XS, YH - 7);
for (int i = 0 ; i < L.length ; i ++){
scanf("%d", &L.elem[i]);
}
gotoxy(XS, YH - 6);
printf("顺序表创建成功\n");
TraveList(L);
gotoxy(XS - 7, YH);
int flag;
printf("input 1 back to menu or input 0 output menu\n");
gotoxy(XS - 4, YH + 1);
scanf("%d", &flag);
if (flag == 0){
system("cls");
printf("GOODBYE");
exit(0);
} else {
system("cls");
menu(L);
}
}
//顺序表的插入
void insert(SqList &L){
if (!L.elem){
menu(L);
}
window();
int flag;
gotoxy(XS, YH - 9);
int p, num;
printf("please input insert position:\t");
scanf("%d", &p);
if (p > L.length + 1|| p < 1){
printf("ERROR");
system("cls");
menu(L);
} else {
gotoxy(XS, YH - 7);
printf("please input insert Data:\t");
scanf("%d", &num);
}
for (int i = L.length - 1; i >= p - 1 ; i --){
L.elem[i + 1] = L.elem[i];
}
L.elem[p - 1] = num;
L.length ++;
TraveList(L);
gotoxy(XS - 4, YH + 1);
printf("input 1 back to menu or input 0 output menu\n");
scanf("%d", &flag);
if (flag == 0){
system("cls");
printf("GOODBYE");
exit(0);
} else {
system("cls");
menu(L);
}
}
//顺序表的删除操作
void deletList(SqList &L){
if (!L.elem){
menu(L);
}
window();
gotoxy(XS, YH - 9);
int p;
printf("please input delet position:\t");
scanf("%d", &p);
if (p > L.length || p < 1){
printf("ERROR");
system("cls");
menu(L);
}
for (int i = p - 1 ; i < L.length ; i ++){
L.elem[i] = L.elem[i + 1];
}
L.length --;
TraveList(L);
int flag;
gotoxy(XS - 4, YH + 1);
printf("input 1 back to menu or input 0 output menu\n");
scanf("%d", &flag);
if (flag == 0){
system("cls");
printf("GOODBYE");
exit(0);
} else {
system("cls");
menu(L);
}
}
//菜单
void menu(SqList &L){
window();
int n = 10;
gotoxy(XS + n, YH - 8);
printf("1.创建顺序表");
gotoxy(XS + n, YH - 6);
printf("2.插入数据");
gotoxy(XS + n, YH - 4);
printf("3.删除数据");
gotoxy(XS + n, YH - 2);
printf("0.退出");
gotoxy(XS , YH + 6);
printf("please input \t");
int p;
scanf("%d", &p);
switch(p){
case 1:
system("cls");
CreatList(L);
break;
case 2:
system("cls");
insert(L);
break;
case 3:
system("cls");
deletList(L);
break;
case 0:
system("cls");
printf("GOODBYE");
exit(0);
}
}
//遍历打印
void TraveList(SqList &L){
gotoxy(XS + 40, YH - 10);
for (int i = 0 ; i < L.length ; i ++){
gotoxy(XS + 40, YH - 7 + i);
printf("%d", L.elem[i]);
}
}
//框框
void window(){
system("color 4");
int n = 10, m = 10;
gotoxy(XS - n, YH - m);
printf("★★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
for (int i = 0 ; i < HEIGHT ; i ++){
gotoxy(XS - n, YH - m + 1);
printf("★");
m --;
}
printf("★★★★★★★★★★★★★★★★★★★★★★★★★★★★");
m ++;
for (int i = 0 ; i < HEIGHT ; i ++){
gotoxy(XS + n * 4 - 2, YH - m + 1);
printf("★");
m ++;
}
for (int i = 0 ; i < HEIGHT ; i ++){
gotoxy(XS + n * 4 + 7, YH - m + 1);
printf("★");
m --;
}
}
//主函数
int main(){
SqList L;
menu(L);
}
标签:YH,菜单,int,简陋,gotoxy,顺序,menu,printf,XS 来源: https://blog.csdn.net/biancheng4/article/details/120668613