# Software-eng lab 4

来源:互联网 发布:乌鲁木齐 中亚 知乎 编辑:程序博客网 时间:2024/04/28 15:14

Riderppp 《软件工程(C编码实践篇)》MOOC课程作业

http://mooc.study.163.com/course/USTC-1000002006

实验结果

这里写图片描述

这期是用可重用的链表完成命令行菜单小程序。在前两期中用的是函数调用来完成小程序,扩展性较差,链表很好地解决了这个问题。

实验代码

/* *  linklist.h *  主要是数据节点结构的定义以及寻找命令和显示全部命令的函数声明 *  by Riderppp from USTC */#ifndef _LINK_LIST_H_#define _LINK_LIST_H_#include "linktable.h" typedef struct DataNode{    tLinkTableNode *pNext;    char*    cmd;    char*    desc;    int      (*handler)();} tDataNode;tDataNode * FindCmd(tLinkTable *head, char *cmd);int ShowAllCmd(tLinkTable *head);#endif 
/* *  linklist.c *  FindCmd函数和ShowAllCmd函数的定义 *  by Riderppp from USTC */#include <stdio.h>#include <stdlib.h>#include "linklist.h"tDataNode* FindCmd(tLinkTable * head, char* cmd){     if (head == NULL || cmd == NULL)     {          return NULL;     }     tDataNode *pNode = (tDataNode*)GetLinkTableHead(head);     while (pNode != NULL)     {          if (strcmp(p->cmd, cmd) == 0)          {               return pNode;          }          p = (tDataNode*)GetNextLinkTableNode(head, (tLinkTableNode*)pNode);      }      return NULL;} int ShowAllCmd(tLinkTable * head){     printf("Menu List:\n");     tDataNode *pNode = (tDataNode*)GetLinkTableHead(head);     while (pNode != NULL)     {          printf("%s - %s\n", p->cmd, p->desc);      pNode = (tDataNode*)GetNextLinkTableNode(head, (tLinkTableNode*)pNode);     }     return 0;}
/* *  linktable.h *  这个文件主要是链表基本函数的声明 *  by Riderppp from USTC */#ifndef _LINK_TABLE_H_#define _LINK_TABLE_H_#define SUCCESS 0#define FAILURE (-1)#include <stdio.h>#include <stdlib.h>typedef struct LinkTableNode{     struct LinkTableNode *pNext;}tLinkTableNode;typedef struct LinkTable{     tLinkTableNode *pHead;     tLinkTableNode *pTail;     int         SumOfNode;}tLinkTable;tLinkTable * CreateLinkTable();int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable);tLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);#endif 
/* *  linktable.c *  链表基本操作函数的定义 *  by Riderppp from USTC */#include "linktable.h"tLinkTable * CreateLinkTable(){     tLinkTable *pTable = (tLinkTable*)malloc(sizeof(tLinkTable));     if(pTable == NULL){          return NULL;     }     pTable->pHead = NULL;     pTable->pTail = NULL;     pTable->SumOfNode = 0;     return pTable;}int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode){     if (pLinkTable == NULL)      {          printf("The table is empty and cannot add this node!\n");         exit(0);      }     if (pNode == NULL)     {          printf("The node is empty and cannot add this node!\n");         return 0;     }     if (pLinkTable->pHead == NULL)     {          pLinkTable->pHead = pNode;         pLinkTable->SumOfNode = 1;     }     if (pLinktable -> pTail == NULL){          pLinkTable -> pTail = pNode;     }     else     {          pLinkTable->pTail->pNext = pNode;         pLinkTable->pTail = pLinkTable -> pTail -> pNext;     }     pLinkTable->SumOfNode++;      return 0;}tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable){     if (pLinkTable == NULL || pLinkTable -> pHead == NULL)      {          return NULL;      }     return pLinkTable->pHead;}tLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode){     if (pLinkTable == NULL || pNode == NULL)      {          return NULL;     }     tLinkTableNode * p = pLinkTable -> pHead;     while(p != NULL){          if(p == pNode)               return p -> pNext;          p = p -> pNext;     }     return NULL;}
/* *  menu.c *  主函数,初始化功能界面以及调用命令函数 *  by Riderppp from USTC */#include <stdio.h>#include <stdlib.h>#include "linklist.h"#include "linktable.h"int Help();int Quit();#define CMD_MAX_LEN 128#define DESC_LEN    1024#define CMD_NUM     10typedef struct DataNode{    tLinkTableNode * pNext;    char * cmd;    char * desc;}DataNode;int InitMenuData(tLinkTable ** ppLinkTable){     //四个功能     *ppLinkTable = CreateLinkTable();     tDataNode * pNode = (tDataNode*)malloc(sizeof(tDataNode));     pNode->cmd = "help";     pNode->desc = "Menu List:";     pNode->handler = Help;     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);     pNode = (tDataNode*)(tDataNode*)malloc(sizeof(tDataNode));     pNode->cmd = "version";     pNode->desc = "Menu Program V2.5";     pNode->handler = NULL;     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);     pNode = (tDataNode*)malloc(sizeof(tDataNode));     pNode->cmd = "quit";     pNode->desc = "Bye~";     pNode->handler = Quit;     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);     pNode = (tDataNode*)malloc(sizeof(tDataNode));     pNode->cmd = "hello";     pNode->desc = "hello :)";     pNode->handler = NULL;     AddLinkTableNode(*ppLinkTable, (tLinkTableNode*)pNode);     return 0; }tLinkTable * head = NULL;int main (){    InitMenuData(&head);    while (1)    {     char cmd[CMD_MAX_LEN];     printf("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 Help(){     ShowAllCmd(head);     return 0;}int Quit(){    exit(0);    return 0;}

最后将实验的结果git一下

这里写图片描述

实验心得

简单实践了一下链表的操作吧,也感受到了可重用模块的重要性

遇到的问题

编译时报错undefined reference,百度了一下发现是链接时候的顺序不对,调整一下就好了

ref: undefined reference to错误的解决方法

原创粉丝点击