软件工程课程实验报告:实验四
来源:互联网 发布:弯矩计算软件 编辑:程序博客网 时间:2024/05/21 17:02
实验四:用可重用的链表模块来实现命令行菜单小程序V2.5
咖啡机《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006
新创建一个目录lab4完成实验。
一、实验要求
- 用可重用的链表模块来实现命令行菜单小程序,执行某个命令时调用一个特定的函数作为执行动作;
- 链表模块的接口设计要足够通用,命令行菜单小程序的功能保持不变;
- 可以将通用的Linktable模块集成到我们的menu程序中;
- 接口规范;
- 及时提交代码以防丢失
二、实验过程
1. 创建lab4文件夹
2. 编写程序
- linkable.h
// 防止重复定义#ifndef _LINK_TABLE_H_#define _LINK_TABLE_H_#define SUCCESS 0#define FAILURE (-1)// LinkTable Node Typetypedef struct LinkTableNode{ struct LinkTableNode * pNext;} tLinkTableNode;// LinkTable Typetypedef struct LinkTable{ tLinkTableNode *pHead; tLinkTableNode *pTail; int SumOfNode;} tLinkTable;// Interface// Create a LinkTabletLinkTable * CreateLinkTable();// Deleteint DeleteLinkTable(tLinkTable *pLinkTable);// Add a Node to LinkTableint AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode * pNode);// Delete a Node from LinkTableint DelLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode * pNode);// Get LinkTableHeadtLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable);// Get next LinkTableNodetLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode * pNode);#endif // !_LINK_TABLE_H_
- linkable.c
#include <stdio.h>#include <stdlib.h>#include "linkable.h"// Create a LinkTabletLinkTable * CreateLinkTable(){ tLinkTable *pTable = (tLinkTable*)malloc(sizeof(tLinkTable)); pTable->pHead = NULL; pTable->pTail = NULL; pTable->SumOfNode = 0; return pTable;}// Deleteint DeleteLinkTable(tLinkTable *pLinkTable){ free(pLinkTable); return 0;}// Add a Node to LinkTableint AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode * pNode){ if (pLinkTable == NULL) { printf("The table is empty!\n"); exit(0); } if (pNode == NULL) { printf("The node is empty!\n"); return 0; } if (pLinkTable->pHead == NULL) { pLinkTable->pHead = pNode; pLinkTable->pTail = pNode; pLinkTable->pTail->pNext = NULL; pLinkTable->SumOfNode = 1; } else { pLinkTable->pTail->pNext = pNode; pLinkTable->pTail = pNode; pLinkTable->pTail->pNext = NULL; pLinkTable->SumOfNode++; } return 0;}// Get LinkTableHeadtLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable){ if (pLinkTable == NULL) { printf("The table is empty!\n"); exit(0); } return pLinkTable->pHead;}// Get next LinkTableNodetLinkTableNode * GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode * pNode){ if (pLinkTable == NULL) { printf("The table is empty!\n"); exit(0); } if (pNode == NULL) { printf("The node is empty!\n"); exit(0); } return pNode->pNext;}
- menu.c
主程序如下:
int main (){ InitMenuData(&head); info(); printf("Plesea input a command:\n"); while(1) { char cmd[CMD_MAX_LEN]; printf(">>>"); scanf("%s",cmd); tDataNode *p = FindCmd(head,cmd); if (p == NULL) { printf("Error: Wrong command!\n"); printf("Type 'info' for available commands.\n"); continue; } printf("%s - %s\n", p->cmd, p->desc); if (p->handler != NULL) { p->handler(); } } return 0;}
集成在menu.c中的linktable:
#define CMD_MAX_LEN 128#define DESC_LEN 1024#define CMD_NUM 20//定义命令结构体typedef struct DataNode{ tLinkTableNode *pNext; char* cmd; //commands char* desc; //Descriptions of commands int (*handler)(); struct DataNode *next;}tDataNode;//从链表头部开始寻找指定命令tDataNode *FindCmd(tLinkTable *head, char *cmd){ tDataNode* pNode=(tDataNode*)GetLinkTableHead(head); while(pNode != NULL) { if(strcmp(pNode->cmd,cmd) == 0) { return pNode; } pNode=(tDataNode*)GetNextLinkTableNode(head,(tLinkTableNode*)pNode); } return NULL;}//显示所有指令int ShowAllCmd(tLinkTable* head){ tDataNode *pNode=(tDataNode*)GetLinkTableHead(head); while(pNode !=NULL) { printf("%s - %s\n",pNode->cmd,pNode->desc); pNode =(tDataNode*)GetNextLinkTableNode(head,(tLinkTableNode*)pNode); } return 0;}//初始化命令菜单int InitMenuData(tLinkTable ** ppLinkTable){ *ppLinkTable=CreateLinkTable(); tDataNode* pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="info"; pNode->desc="Command Informations"; pNode->handler=info; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="quit"; pNode->desc="close this program"; pNode->handler=quit; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="plus"; pNode->desc="result of a + b"; pNode->handler=plus; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="minus"; pNode->desc="result of a - b"; pNode->handler=minus; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="multiply"; pNode->desc="result of a * b"; pNode->handler=multiply; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="divide"; pNode->desc="result of a / b"; pNode->handler=divide; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="power"; pNode->desc="result of a ^ b"; pNode->handler=power; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="square"; pNode->desc="result of the square root of a"; pNode->handler=square; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="factorial"; pNode->desc="result of a!"; pNode->handler=factorial; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); pNode=(tDataNode*)malloc(sizeof(tDataNode)); pNode->cmd="absolute"; pNode->desc="result of |a|"; pNode->handler=absolute; AddLinkTableNode(*ppLinkTable,(tLinkTableNode *)pNode); return 0;}tLinkTable* head=NULL;
菜单命令包括以下9种:
- 介绍命令信息info()
- 加法运算plus()
- 减法运算minus()
- 乘法运算multiply()
- 除法运算divide()
- 幂运算power()
- 平方根运算square()
- 阶乘运算factorial()
- 绝对值运算absolute()
- 退出quit()
限制于报告篇幅,以上命令的具体实现代码就不在文中贴出,烦请移步github工程中查看。
https://github.com/973301529/se/tree/master/lab4
3. 编译执行
- 初始运行情况,info()介绍可用指令的相关信息
- 检测info()、plus()、power()、factorial()、absolute()、quit()等指令的运行情况
4. 将代码同步到github
三、实验总结
本次实验在上次实验的基础上更进一步,对可重用的链表模块来实现命令行菜单小程序进行实验,更加熟悉了链表的相关知识,并学习了链表模块的接口设计,初步了解了接口设计。在此次实验中,也感受到了自己在链表设计中的不足,以后的学习中会更加注重链表的学习。
阅读全文
0 0
- 软件工程课程实验报告:实验四
- 软件工程课程实验报告:实验二
- 软件工程课程实验报告:实验一
- 软件工程课程实验报告:实验三
- 软件工程课程实验报告:实验五
- 软件工程课程实验报告:实验七
- 软件工程课程实验报告:课程总结
- 高级软件工程实验报告四
- 操作系统课程实验报告(四)
- 软件工程实验报告一
- 软件工程导论实验报告
- 软件工程实验报告一
- 软件工程实验报告 一
- 软件工程实验报告二
- 软件工程实验报告 二
- 软件工程实验报告 三
- 软件工程实验报告 二
- 软件工程实验报告 一
- 【linux】/home/USR/.ssh目录里什么时候需要添加authorized_keys文件(1)
- 基于DragonBoard410c的智能照明系统
- Ghost Explore镜像文件查看器
- Linux环境下自动备份oracle数据库并传输到指定的备份存储器上
- 跨浏览器开发笔记
- 软件工程课程实验报告:实验四
- 端口号区间合并
- NEON 3x3 max pool示例代码
- Google EventBus 使用详解
- 第十一章ActiveMQ代理器的新特性【笔记】
- mysql+mmm+proxy实现mysql读写分离
- netty的tcp粘包与拆包
- linux基础(一)
- eclipse maven error: Archive for required library in project cannot be read or is not a valid ZIP fi