软件工程c 语言实训总结,实验三:内部模块化的命令行菜单小程序V2.0

本文介绍了一个使用内部模块化设计的命令行菜单小程序。通过静态链表存储菜单命令及其对应的处理函数,实现了菜单数据与业务逻辑的分离。在`linklist.h`中定义链表结构和操作,在`linklist.c`中实现操作,在`menu.c`中实现主函数和菜单命令。程序提供帮助、显示所有命令和退出等功能,通过不断读取用户输入并调用相应处理函数来运行。实验强调了模块化设计的重要性,包括KISS原则、数据结构简化代码等原则,并展示了程序调试和维护的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1. 实验要求

通过设计简单的命令行菜单小程序,了解并掌握程序内部模块化设计的思想,实现菜单数据存储与菜单业务逻辑的分离。

2. 实验思路

用一个静态链表来存储可变化的菜单命令及其对应函数,在主函数中实菜单命令逻辑。

在一个头文件中定义链表数据结构及其操作原型,并在相应源文件中实现其操作;然后在主函数源文件中实现菜单命令。

3. 实验过程

实验代码如下:

3.1 linklist.h

typedef struct DataNode {

char* cmd;

char* desc;

int(*handler)();

struct DataNode *next;

}tDataNode;

tDataNode* FindCmd(tDataNode *head, char *cmd);

int ShowAllCmd(tDataNode * head);

3.2 linklist.c

#include

#include

#include "linklist.h"

tDataNode* FindCmd(tDataNode *head, char *cmd) {

if (head == NULL || cmd == NULL)

return NULL;

tDataNode *p = head;

while (p != NULL) {

if (strcmp(cmd, p->cmd) == 0)

return p;

p = p->next;

}

return NULL;

}

int ShowAllCmd(tDataNode *head) {

printf("Menu list:\n");

tDataNode *p = head;

while (p != NULL) {

printf("%s - %s\n", p->cmd, p->desc);

p = p->next;

}

return 0;

}

3.3 menu.c

#include

#include

#include "linklist.h"

#define CMD_MAX_LEN 128

#define DESC_LEN 1024

#define CMD_NUM 10

int Help();

int Quit();

/*menu program*/

static tDataNode head[] = {

{ "help","This is a help cmd.",Help,&head[1] },

{ "version","menu program v2.0.",NULL,&head[2] },

{ "quit","Quit form menu.",Quit,NULL }

};

int main() {

/*cmd line begin*/

while (1) {

char cmd[CMD_MAX_LEN];

printf("Please input a cmd:>");

scanf("%s", cmd);

tDataNode* p = FindCmd(head, cmd);

if (p == NULL) {

printf("This is a wrong cmd!");

continue;

}

printf("%s - %s\n", p->cmd, p->desc);

if (p->handler != NULL)

p->handler();

}

return 0;

}

int Help() {

ShowAllCmd(head);

return 0;

}

int Quit() {

exit(0);

}

4. 运行结果

5cd2b80fe165c317b6f3add1ac807a30.png

5. 实验总结

软件=程序+软件工程,程序=算法+数据结构

通过本次实验,我主要有以下几个方面的收获:

认识到模块化的重要性

了解了模块化程序设计的方法(KISS原则、设计与代码的一致性、不要和陌生人说话原则、用数据结构和控制结构来简化代码、错误处理)

程序的调试及维护

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值