点坐标管理系统和动态菜单

point.h

#ifndef _POINT_H_
#define _POINT_H_

typedef struct POINT {
    int row;
    int col;
    struct POINT *next;
}POINT;

typedef unsigned char Boolean;

#define TRUE  1
#define FLASE 0


#define	NOT_FOUND		-1

#endif // _POINT_H_

pointManger.h

#ifndef _POINT_MANGER_H_
#define _POINT_MANGER_H_

#include<stdio.h>
#include<malloc.h>

#include"Point.h"

#define ASC	1 //升序
#define DES 2 //降序

void initPointsLink(POINT *head);
void destoryPointsLink(POINT *head);
void showPoints(POINT *head);
void insertPoint(POINT *head);
Boolean delPointFromLink(POINT *head);
void sortPoint(POINT head,int sortType);
POINT *findPoint(POINT haed);

#endif // _POINT_MANGER_H_
pointManger.c

#include<stdio.h>
#include<malloc.h>

#include"mecPoint.h"
#include"mecPointManager.h"


static void showOnePoint(POINT node);
static POINT* findPrePoint(POINT *head,Point targetPoint);
static void insertPointAction(POINT *head,POINT *prePoint,POINT *newPoint);
static POINT* createOnePoint(const char *string);
static void setOnePointMessage(POINT *node,int row,int col);
static POINT inputOnePoint(const char *string);
static void swapPoint(POINT *one,POINT *another);
static POINT *findPointAction(POINT head,const char *string);

POINT *findPoint(POINT head) {
    return findPoint(head);
}
static POINT *findPointAction(POINT head) {
    POINT *prePoint;
    POINT *nowPoint;
    POINT targetPoint;

    targetPoint = inputOnePoint();
    prePoint = findPrePoint(head,targetPoint);

    if(prePoint == NULL) {
        return head.next;
    } else if(prePoint->next == NULL) {
        return NULL;
    }

    return prePoint->next;
}

static void swapPoint(POINT *one,POINT *anohter) {
    POINT tmp;
    tmp = *one;
    *one = *another;
    *another = tmp;
}

void sortPoint(POINT head,int sortType) {
    POINT *one;
    POINT *another;
    POINT *tmp;

    for(one = head.next;one;one = one->next) {
        for(another = one.next,another;another = another->next) {
            if(ASC == sortType && one->row > another->row || DES == sortType && one->row < another->row) {
				swapPoint(one, another);

				tmp = another->next;
				another->next = one->next;
				one->next = tmp;
        }
    }
}
static POINT inputOnePoint(const char* string) {
    int row;
    int col;
    POINT result;

    printf("%s",string);
    scanf("%d %d",&row,&col);
    setOnePointMessage(&result,row,col);

    result POINT;
}

Boolean delPointFromLink(POINT *head) {
    int row;
    int col;
    POINT targetPoint;
    POINT *prePoint;
    POINT *p;

    scanf("%d %d",&row,&col);
    targetPoint = inputOnePoint();
    prePoint = findPrePoint(head,targetPoint);

    if(NULL == preNode) {
		preNode = head;
	} else if(NULL == preNode->next) {
		return FALSE;
	}
	p = preNode->next;
	preNode->next = p->next;
	free(p);

	return TRUE;
}

static void setOnePointMessage(POINT *node,int row,int col) {
    node->col = col;
    node->row = row;
}

static POINT* createOnePoint(const char* string) {
    POINT *node = NULL;

    node =(POINT *)calloc(sizeof(POINT),1);

    *node = inputOnePoint(string);
    node->next = NULL;

    return node;
}

static void insertPointAction(POINT *head,POINT *prePoint,POINT *newPoint) {
    if(prePoint == NULL) {
        newPoint->next = head->next;
        head->next = newPoint;
    } else {
        newPoint->next = prePoint->next;
        prePoint->next = newPoint;
    }
}

void insertPoint(POINT *head) {
    int newRow;
    int newCol;
    int oldRow;
    int oldCol;
    POINT *newPoint = NULL;
    POINT *oldPont = NULL;
    POINT *prePoint;

    newPoint = createOnePoint(newRow,newCol);
    setOnePointMessage(OldPoint,oldRow,oldCol);
    prePoint = findPrePoint(head,oldPoint);

    insertPointAction(head,prePoint,newPoint);
}
static POINT* findPrePoint(POINT *head,Point targetPoint) {
    POINT *prePoint;
    point *nowPoint;

    for(nowPoint = head->next;(nowPoint->col != targetPoint->col) && ((nowPoint->row != targetPoint->row)) && nowPoint;nowPoint = nowPoint->next) {
        prePoint = nowPoint;
    }

    return prePoint;
}

static void showOnePoint(POINT node) {
    printf("%d %d ",node.row,node.col);
}


void showPoints(POINT *head) {
    POINT *curPoint;

    printf("\n点坐标如下:\n");
    for(curPoint = head->next;curPoint;curPoint = curPoint->next) {
        showOnePoint(*curPoint);
    }
    printf("\n");
}

void destoryPointsLink(POINT *head) {
    POINT *node;

    while(head->next) {
        node = head->next;
        head->next = node->next;
        free(node);
    }
}

void initPointsLink(POINT *head) {
    int row;
    int col;
    POINT *newPoint;
    POINT *lsatNode;

    if(*head == NULL) {
        printf("已经初始化");
        return;
    }

    printf("输入点的坐标");
    scanf("%d %d",row,col);
    while(row && col) {
        newPoint = (POINT *)calioc(sizeof(POINT),1);

        setOnePointMessage(newPoint,row,col);
        newPoint->next = NULL;

        if(head->next = NULL) {
            haed->next = newPoint;
        } else {
            lastNode->next = newPoint;
        }
        lastNode = newPoint;

        printf("输入点的坐标");
        scanf("%d %d",row,col);
    }
}

menu.h

#ifndef _MENU_
#define _MENU_

#define SCREEN_LENTH  80

//MENU infoStatus
#define INFO_EMPTY		0
#define INFO_NORMAL		1
#define INFO_FULL		2

//MENU_ITEM  status
#define SHOW            1
#define HIDDEN          0

#define	BAD_CHOICE		-1

typedef struct MENU_ITEM {
	const char *item;
	int status[3];
	int hotKey;
}MENU_ITEM;

typedef struct MENU {
	const char *menuTopic;
	MENU_ITEM *menuItem;
	int itemCount;
	int infoStatus;
}MENU;

void setMenuTopic(MENU menu,const char *menuTopic);
void initMenu(MENU *menu);
void showMenu(MENU);
void setMenuItem(MENU *menu,Menu_Item *menuItem,int itemCount);
void setInfoStatus(MENU *menu,int infoStatus);
int userSelected(MENU menu);

#endif // _MENU_
menu.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>

#include"menu.h"

static void printOnCenter(const char* message);
static void printAt(int left, const char *message);

int userSelected(MENU menu) {
    int choose;
    int chooseSave;
    int i;

    printf("\n");
    printOnCenter("请选择");

    chooseSave = getche();
    choose = islower(chooseSave) ? toupper(chooseSave) : chooseSave;
    for(i = 0;i < menu.itemCount;i++) {
        if(SHOW == menu.menuItem[i].status[menu.infotatus] && menu.menuItem[i].hotKey == choose) {
            return i;
        }
    }

    printf("\n 无效的选择:[%c],按任意键继续",chooseSave);
    getch();

    return BAD_CHOICE;
}
void setInfoStatus(MENU *menu,int infoStatus) {
    menu->infoStatus = infoStatus;
}

void setMenuItem(MENU *menu,Menu_Item *menuItem,int itemCount) {
    if(NULL == menu)
        return;
    if(itemCount <= 0 || itemCount > 19) {
        return;
    }

    menu->menuItem = menuItem;
    menu->itemCount = itemCount;
}

void showMenu(MENU menu) {
    int i;
    int left;
    int row;

    system("cls");

    row = (25 - 2 - 4 - menu.itemCount) / 2;
    left = (80 - getMaxLength(menu)) / 2;
    for(i = 0;i < row;i++) {
        printf("\n");
    }
    printOnCenter(menuTopic);
    for(i = 0;i < menu.itemCout;i++) {
        if(SHOW == menu.muneItem[i].status[menu.infoStatus]) {
            printfAt(left,menuItem[i].item);
        }
        printf("\n");
    }
}

void initMenu(MENU *menu) {
	if(NULL == menu)
        return;

	menu->menuTopic = NULL;
	menu->menuItem = NULL;
	menu->itemCount = 0;
	menu->infoStatus = INFO_EMPTY;
}

void setMenuTopic(MENU *menu, const char *menuTopic) {
	if(NULL == menu) return;

	menu->menuTopic = menuTopic;
}

int getMaxLength(menu) {
    int maxLen = 0;
    int i;

    for(i = 0;i < menu.itemCount;i++) {
        int itemLen = strlen(menu.menuItem[i].item);
        if(itemLen > maxLen) {
            maxLen = itemLen;
        }
    }
    return maxLen;
}

void printAt(int left, const char *message) {
	int spaceCount;
	char format[10];

	spaceCount = left;
	sprintf(format, "%%%ds%%s", spaceCount);
	printf(format, "", message);
}

void printOnCenter(const char* message) {
    int len;
    int spaceCount;
    char format[10];
    len = strlen(message);

    if(len > SCREEN_WIDTH) {
        return;
    }

    spaceCount = (SCREEN_WIDTH - messageLen) / 2;
	sprintf(format, "%%%ds%%s", spaceCount);
	printf(format, "", message);
}
system.c

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

#include"Menu.h"
#include"Point.h"
#include"PointManager.h"


void sayGoodBye();
void showMessage(const char *message);
void pressAnyKeyToContinue();
void showFunctionTitle(const char *title);
void beforeDoneFunction(const char *functionName);
void functionInputPoints(POINT *point, MENU *menu);
void functionShowPoints(POINT *point, MENU *menu);
void functionInsertPoint(POINT *point, MENU *menu);
void functionRemovePoint(POINT *point, MENU *menu);
void functionSearchPoint(POINT *point, MENU *menu);
void functionSortByRowASC(POINT *point, MENU *menu);
void functionSortByRowDES(POINT *point, MENU *menu);

typedef void (*Fun)(POINT *, MENU *);

void functionSortByRowDES(POINT *point, MENU *menu) {
	showFunctionTitle("按行降序排列");

	sortByRow(*point, DES);

	beforeDoneFunction("按行降序排列");
}

void functionSortByRowASC(POINT *point, MENU *menu) {
	showFunctionTitle("按行升序排列");

	sortByRow(*point, ASC);

	beforeDoneFunction("按行升序排列");
}

void functionSearchPoint(POINT *point, MENU *menu) {
	POINT *node;
	showFunctionTitle("查找点");

	node = findPoint(*point);
	if(NULL == node) {
		showMessage("点不存在,查找失败!!");
	} else {
		printf("\n找到点,如下:\n");
		showOnePoint(*node);
	}

	beforeDoneFunction("查找点");
}

void functionRemovePoint(POINT *point, MENU *menu) {
	showFunctionTitle("删除点");

	showPoints(*point);
	if(TRUE == removePoint(point)) {
		showMessage("删除成功!");
	} else {
		showMessage("删除失败!");
	}
	setInfoStatus(menu, NULL == point->next ? INFO_EMPTY : INFO_NORMAL);

	beforeDoneFunction("删除点");
}

void functionInsertPoint(POINT *point, MENU *menu) {
	showFunctionTitle("插入点");

	insertPoint(point);
	setInfoStatus(menu, NULL == point->next ? INFO_EMPTY : INFO_NORMAL);

	beforeDoneFunction("插入点");
}

void beforeDoneFunction(const char *functionName) {
	char endString[80] = {0};

	sprintf(endString, "<<%s>>功能完成!", functionName);
	printf("\n");
	showMessage(endString);
	pressAnyKeyToContinue();
}

void showFunctionTitle(const char *title) {
	char star[80];
	int titleLen = strlen(title);
	char titleString[80] = {0};
	int i;

	for(i = 0; i < titleLen + 12; i++) {
		star[i] = '*';
	}
	star[i] = 0;
	sprintf(titleString, "  <<%s>>功能  ", title);

	system("cls");

	showMessage(star);
	showMessage(titleString);
	showMessage(star);
	printf("\n");
}

void functionShowPoints(POINT *point, MENU *menu) {
	showFunctionTitle("显示点信息");

	showPoints(*point);

	beforeDoneFunction("显示点信息");
}

void functionInputPoints(POINT *point, MENU *menu) {
	showFunctionTitle("录入点信息");

	inputPoint(point);
	setInfoStatus(menu, NULL == point->next ? INFO_EMPTY : INFO_NORMAL);

	beforeDoneFunction("录入点信息");
}

void pressAnyKeyToContinue() {
	showMessage("按任意键继续... ...");
	getch();
}

void showMessage(const char *message) {
	printOnCenter(message);
	printf("\n");
}

void sayGoodBye() {
	int i;

	for(i = 0; i < 2; i++) {
		showMessage("是否真的离开?");
		showMessage("是否真的离开?");
		showMessage("好吧,再见!");
	}
}

int main(void) {
	MENU menu;
	MENU_ITEM items[] = {
		"1.录入点信息", 	SHOW,				HIDDEN, 		HIDDEN,		'1',
		"2.插入点",			 	SHOW,				SHOW,	 			HIDDEN,		'2',
		"3.删除点", 			HIDDEN,			SHOW, 			SHOW,			'3',
		"4.显示点信息",		HIDDEN,			SHOW,				SHOW,			'4',
		"5.查找点",				HIDDEN,			SHOW,				SHOW,			'5',
		"6.按行升序排列",	HIDDEN,			SHOW,				SHOW,			'6',
		"7.按行降序排列",	HIDDEN,			SHOW,				SHOW,			'7',
		"X.退出",					SHOW,				SHOW,				SHOW,			'X',
	};
	Fun function[] = {
		functionInputPoints,
		functionInsertPoint,
		functionRemovePoint,
		functionShowPoints,
		functionSearchPoint,
		functionSortByRowASC,
		functionSortByRowDES,
	};
	int itemCount = sizeof(items) / sizeof(MENU_ITEM);
	boolean finished = FALSE;
	int choose;

	POINT head = {0};

	initMecMenu(&menu);
	setMenuTopic(&menu, "屏幕点坐标信息管理系统");
	setMenuItem(&menu, items, itemCount);
	while(!finished) {
		showMenu(menu);
		choose = userSelected(menu);
		if(choose == itemCount-1) {
			finished = TRUE;
		} else {
			function[choose](&head, &menu);
		}
	}

	sayGoodBye();

    return 0;
}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值