內部模塊化的命令行菜單小程序

實驗內容

實現內部模塊化的命令行菜單小程序,注意代碼的業務邏輯和數據存儲之間的分離,即將系統抽象爲兩個層級:菜單業務邏輯和菜單數據存儲。

實驗內容及截圖
1 創建文件夾 mkdir lab3
2 menu.c文件

#include<stdio.h>
#include<stdlib.h>
#include "linklist.h"

int menu();
int quit();

#define CMD_MAX_LEN 128
#define DESC_LEN 1024
#define CMD_NUM 10

static tDataNode head[]=
{
    //初始化賦值  鏈表
    {"menu", "this is menu cmd!", menu, &head[1]},
    {"version", "menu program v2.0", NULL, &head[2]},
    {"quit", "quit from menu", quit, NULL}
};

int main()
{
    while(1)
    {
        char cmd[CMD_MAX_LEN];
        printf("Please input a cmd number ->");
        scanf("%s",cmd);
        tDataNode *p = FindCmd(head, cmd);

        if(p == NULL)
        {
            printf("This is a wrong cmd!\n");
            continue;
        }

        printf("%s - %s\n", p->cmd, p->desc);

        if(p->handler != NULL)
        {
            p->handler();
        }
    }
}

int menu()
{
    ShowAllCmd(head);
    return 0;
}

int quit()
{
    exit(0);
}

3 linklist.c文件

/*這是一個拆分模塊程序 */

tDataNode* FindCmd(tDataNode * head, char * cmd)
{
    if(head == NULL || cmd == NULL)
    {
        return NULL;
    }

    tDataNode *p = head;

    while(p != NULL)
    {
        if(strcmp(p->cmd, cmd) == 0)
        {
            //作比較
            return p;
        }
        p = p->next;
    }

    return NULL;
}

int ShowAllCmd(tDataNode * head)
{
    printf("Menu List:\n");
    tDataNode *p = head;
    while(p != NULL)
    {
        printf("%s - %s\n", p->cmd, p->desc);
        p = p->next;
    }
    return 0;
}

4 linklist.h文件

//這是一個頭文件

//數據結構的定義
typedef struct DataNode
{
    char * cmd;
    char * desc;
    int (*handler)();   //函數指針
    struct DataNode * next;
}tDataNode;
//方法聲明
tDataNode * FindCmd(tDataNode * head, char * cmd);
int ShowAllCmd(tDataNode * head);

5 編譯

gcc menu.c linklist.c -o menu

6 運行

C:\Users\Je-vie\Desktop\workspace\lab3
menu

7 效果

Please input a cmd number ->menu
menu - this is menu cmd!
Menu List:
menu - this is menu cmd!
version - menu program v2.0
quit - quit from menu
Please input a cmd number ->version
version - menu program v2.0
Please input a cmd number ->quit
quit - quit from menu

8 提交Github

在實驗開始時候就已經在自己的git賬號上創建一個lab3的倉庫
並且clone到本地

λ git clone https://github.com/Je-vie/lab3
C:\Users\Je-vie\Desktop\workspace\lab3
λ ls
README.md  lab3  linklist.c  linklist.h  menu.c  menu.exe

所以最後直接提交

C:\Users\Je-vie\Desktop\workspace\lab3
λ git add menu.c linklist.h linklist.c

λ git commit -m"模塊化設計"
[master 23be817] 模塊化設計
 3 files changed, 118 insertions(+)
 create mode 100644 linklist.c
 create mode 100644 linklist.h
 create mode 100644 menu.c

λ git push
Username for 'https://github.com': je-vie
Password for 'https://[email protected]':
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 1.50 KiB | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/Je-vie/lab3
   991f396..23be817  master -> master

9 實驗總結
模塊化設計是程序設計很重要的思想,在今後的開發中要不斷思考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章