# 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错误的解决方法
阅读全文
0 0
- # Software-eng lab 4
- # Software-eng lab 1
- # Software-eng lab 2
- # Software-eng lab 3
- # Software-eng lab 5
- # Software-eng lab 6
- eng
- eng
- eng
- Lizcst Software Lab旗下品牌新秀上线!
- LAB 4
- Web client software factory – Hands-on-lab
- Lizcst Software Lab-Lizcst的软件实验室 全新产品
- Lizcst Software Lab官方微信平台开通!
- [Lab Week] 4->5
- Lab 4: Authentication
- Lab 4:尝试bootloader
- 嵌入式LAB 4:Bootloder
- 函数调用栈
- 2017.9.16队内互测——老年组Day1
- 解决fiddler抓取firefox网络包的问题
- Jquery统计表单文件总大小
- 3.1 链接加载原理及elf文件格式
- # Software-eng lab 4
- Django——登录后回到请求登录的页面
- unity代理委托的简单用法
- 数据结构入门---栈(下)
- 并发和并行的区别
- Scala算法
- 学不下去时坚持的方法
- leetcode--16. 3Sum Closest
- webpack3最新版本配置研究(三)webpack压缩,clean-webpack-plugin,webpack-manifest-plugin,define-plugin