实验五:用callback增强链表模块来实现命令行菜单小程序V2.8


“软件工程(C编码实践篇)”实验报告
实验五:用callback增强链表模块来实现命令行菜单小程序V2.8
网易云课堂昵称: Arjen0130
《软件工程(C编码实践篇)》MOOC课程作业 http://mooc.study.163.com/course/USTC-1000002006
依照学术诚信条款,我保证此回答为本人原创,所有回答中引用的外部材料已经做了出处标记。
实验报告原链接地址:http://note.youdao.com/noteshare?id=6672f601bbef2c674ec6bfd8f9e81426&sub=0CD95CD8898748C19726346115E07480
1. 实验内容和要求
1.1 实验内容
在老师提供的代码的基础上,用callback增强链表模块来实现命令行菜单小程序V2.8,并且,需要符合接口的信息隐藏的相关要求。
1.2 实验要求
给lab5-1.tar.gz找bug,quit命令无法运行的bug;
利用callback函数参数使Linktable的查询接口更加通用;
注意接口的信息隐藏。
注: 本实验在提供的代码基础上进行。
2. 实验的思路和具体过程
2.1 实验的思路
看完实验五的相关学习视频以后,了解到可以通过使用函数指针,使得一个函数模块更加通用。同时,在定义接口时,需要注意对相关信息进行隐藏。原因在于,接口是被调用模块的程序员使用的,恰当的接口信息,不仅能够避免很多可能对调用者造成干扰的冗余信息,更有利于接口的安全。
依照视频讲解,并参照实验要求,从而完成本次实验。
2.2 实验的具体过程
1)使用实验1中创建好的本地仓库,添加lab5文件夹,把从 lab5-1.tar.gz中解压出的文件全部放入lab5文件夹中
2)找到导致quit命令无法执行的bug,并进行修复;
3)修改callback相关的函数,使程序的各个部分更加独立,增强模块性;
4)合理隐藏接口部分的相关信息;
5)编译、调试通过后,添加到git本地仓库,并上传到git远端仓库。
3. 关键代码
3.1 对menu.c文件做出的修改
... ...
#define CMD_MAX_LEN 128
#define DESC_LEN 1024
#define CMD_NUM 10

//char cmd[CMD_MAX_LEN]; //此处配合callback函数进行修改
... ...
/*
 * find the node is matched with the cmd or not.
 * @pLinkTableNode: a pointer to the node to be tested.
 * @cmd: a pointer to a string used to test the node.
 * @return: if matched
 */
int SearchCondition(tLinkTableNode * pLinkTableNode, char * cmd) //此处配合callback函数进行修改
{
    if((NULL == pLinkTableNode) || (NULL == cmd)) //添加输入参数合法性检查
    {
        return FAILURE;
    }
    tDataNode * pNode = (tDataNode *)pLinkTableNode;
    if(strcmp(pNode->cmd, cmd) == 0)
    {
        return SUCCESS; 
    }
    return FAILURE; 
}

/* 
 * find a cmd in the linklist and return the datanode pointer.
 * @head: a pointer to the head of a LinkTable.
 * @cmd: a poiner to a string which indicates the name of the command.
 * @return: a pointer to a tDataNode that matched the cmd.
 */
tDataNode* FindCmd(tLinkTable * head, char * cmd)
{
    return (tDataNode*)SearchLinkTableNode(head,SearchCondition, cmd); //此处配合callback函数进行修改
}
... ...
int main()
{
    InitMenuData(&head); 
/* cmd line begins */
    char cmd[CMD_MAX_LEN]; //此处配合callback函数进行修改
    while(1)
        ... ...
3.2 对linktable.h文件做出的修改
... ...
/*
 * LinkTable Node Type
 */
typedef struct LinkTableNode tLinkTableNode; //适当隐藏接口信息

/*
 * LinkTable Type
 */
typedef struct LinkTable tLinkTable; //适当隐藏接口信息
... ...
/*
 * Search a LinkTableNode from LinkTable
 * @pLinkTable: a pointer to the linkTable to be searched.
 * @int Conditon(tLinkTableNode * pNode): a pointer to a function used as judging condition.
 * @cmd: a pointer to a string used to test the node of the LinkTable.
 * @return: if find the matched node, then return it, else return NULL.
 */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, int Conditon(tLinkTableNode * pNode,char * cmd), char * cmd);//此处配合callback函数进行修改
... ...

3.3 对linktable.c文件做出的修改
... ...
//被隐藏的接口信息从.h文件转移到.c文件
/*
 * LinkTable Node Type
 */
typedef struct LinkTableNode
{
    struct LinkTableNode * pNext;
}tLinkTableNode;

/*
 * LinkTable Type
 */
typedef struct LinkTable
{
    tLinkTableNode *pHead;
    tLinkTableNode *pTail;
    int SumOfNode;
    pthread_mutex_t mutex;
}tLinkTable;
... ...
 /*
  * Search a LinkTableNode from LinkTable
  * @pLinkTable: a pointer to the linkTable to be searched.
  * @int Conditon(tLinkTableNode * pNode): a pointer to a function used as judging condition.
  * @cmd: a pointer to a string used to test the node of the LinkTable.
  * @return: if find the matched node, then return it, else return NULL.
  */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, int Conditon(tLinkTableNode * pNode, char * cmd), char * cmd)//此处配合callback函数进行修改
{
    if((NULL == pLinkTable) || (NULL == Conditon) || (NULL == cmd))
    {
        return NULL;
    }
    tLinkTableNode * pNode = pLinkTable->pHead;
//while(pNode != pLinkTable->pTail) //This test condition will stop the loop when reaching the tail node.
    while(NULL != pNode) //这里处理掉了quit命令无法使用的bug
    { 
        if(Conditon(pNode, cmd) == SUCCESS)
        {
            return pNode; 
        }
        pNode = pNode->pNext;
    }
    return NULL;
}
... ...

4. 相关截图
4.1 实验结果截图

4.2 关键代码截图
4.2.1 menu.c文件中的关键代码截图


4.2.2 linktabel.h文件中的关键代码截图



4.2.3 linktabel.c文件中的关键代码截图



4.3 操作过程截图
4.3.1 复制得到的头文件和源文件

4.3.2 将工作目录中修改后的文件添加并提交到Git本地仓库

4.3.3 将本地仓库的变化提交到远端仓库



4.4 复现操作截图

5. 实验过程中遇到的疑惑、困难及处理方法
本次实验中,第一次接触到了函数指针这个概念,刚开始的时候对这个概念不是很理解。后来,在网上查看了相关博客之后,有了初步的了解。这里,感慨一番网络的便捷和强大。
在查找quit命令无法执行的bug时,刚开始很没有头绪。随着对代码熟悉,通过逐步分析代码的执行过程,找到各个函数的调用关系,最终找到并修复了bug。
6. 实验总结
通过本次实验,初步了解了函数指针的概念和使用方法。学习了通过在一个函数中使用函数指针作为入口参数,从而使得这个函数更加通用的方法,这种实现方式和STL中的某些功能的实现机制很像。同时,还学习到了隐藏接口中不必要的信息,使得结构更加简洁、安全的方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值