实验三:内部模块化的命令行菜单小程序V2.0

课程来源: 【网易云课堂昵称 :风清扬pty+ 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006 】

一. 实验内容

  1. 观看了孟老师的视频课件,学习了相关的代码规范,如KISS原则(简单易懂),不和陌生人说话原则(一个函数、一个方法、一个代码块只做一件事),goto 的使用方法,使用合适的数据结构可以使程序更加简单高效,还有一定要有错误处理。
  1. 学习体会内部模块化编程的思想和方法。

二. 实验过程

linklist.h

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

typedef struct DataNode
{
    char* cmd;
    char* desc;
    int (*handler)();
    struct DataNode *next;
}tDateNode;

tDateNode *findCmd(tDateNode *head,char *cmd);

int showAllCmd(tDateNode *head);

linklist.c

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

tDateNode *findCmd(tDateNode *head, char *cmd)
{
    if(head == NULL || cmd == NULL)
    {
        return NULL;
    }
    tDateNode *p = head;
    while(p != NULL)
    {
        if( !strcmp(p->cmd, cmd))
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}

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

menu.c

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

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

int help();
int quit();


static tDateNode head[] =
{
    {"help", "This is help infomation!", help, &head[1]},
    {"version", "Menu Program for lab3", help, &head[2]},
    {"setting", "This is setting function", help, &head[3]},
    {"quit","Quit", quit, NULL}
};

int main()
{
    while(1)
    {
        char cmd[CMD_MAX_LEN];

        printf("please input the cmd > ");
        scanf("%s", cmd);

    tDateNode *p = findCmd(head, cmd);
        if(p == NULL)
        {
            printf("ERROR:cmd not found!\n");
            continue;
        }

    printf("%s : %s \n", p->cmd,p->desc);
        if(p->handler != NULL)
        {
            p->handler();
        }
    }

}

int help()
{
    showAllCmd(head);
    return 0;
}

int quit()
{
    exit(0);
}

三实验总结

     通过这次实验熟悉了一下模块化编程的一些基本方法,虽未能完全领悟模块化编程方法的精髓,但还是在实际操作中对老师在课堂上讲的一些东西有所感悟,希望在老师的带领下继续深入学习,不断提高。实验运行截图如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值