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

【李文静《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006 】

实验思路

依然是命令行菜单小程序升级版,本次实验重点是模块化,简单的说就是将一个复杂的长程序,按照一定的规则拆分成多个小的程序,这样让代码更加可读,每个程序的功能更加明确。

主要代码

代码较长,仅列出部分

头文件link_list.h,包含一些宏定义和自定义的数据结构

#define CMD_NUM 8

#define CMD_MAX_LEN 128

#define CMD_ANNO_LEN 1024

// data structure:

typedef struct link_node {

char cmd_name[CMD_MAX_LEN];

char cmd_annotation[CMD_ANNO_LEN];

int (*handler)();

struct link_node* next;

} link_list;

link_list* search_cmd(char cmd[], link_list* head);

int show_all_cmd(link_list* head);

link_list.c 从原来的程序中划分出来的功能实现的子函数,包括将输入的命令与命令行对比和help菜单的展现

#include

#include

#include

#include "link_list.h"

link_list* search_cmd(char cmd[], link_list* head)

{

link_list* ptr = head;

if (cmd == NULL || ptr == NULL)

{

return NULL;

}

while (ptr != NULL)

{

if (strcmp(ptr->cmd_name, cmd) == 0)

{

break;

}

ptr = ptr->next;

}

return ptr;

}

int show_all_cmd(link_list* head)

{

printf("Your can see the command list of menu program:\n");

printf("----------------------------------------------\n");

link_list* ptr = head;

while (ptr != NULL)

{

printf("%s\t-\t%s\n", ptr->cmd_name, ptr->cmd_annotation);

ptr = ptr->next;

}

printf("----------------------------------------------\n");

return 0;

}

cmd_menu.c主函数 处理用户输入以及调用

#include

#include

#include

#include "link_list.h"

#define CMD_NUM 10

#define CMD_MAX_LEN 128

#define CMD_ANNO_LEN 1024

int cmd_help();

int cmd_version();

int cmd_quit();

int cmd_others();

int cmd_open();

int cmd_put();

int cmd_ls();

int cmd_close();

int cmd_cd();

int cmd_get();

static link_list head[] = {

{ "help", "you can see help list.", cmd_help, &head[1] },

{ "version", "menu program v2.0.4.", cmd_version, &head[2] },

{ "quit", "you quit the menu program.", cmd_quit, &head[3] },

{ "others", "This is 'others' command.", cmd_others, &head[4] },

{ "open", "This is 'open' command.", cmd_open, &head[5] },

{ "put", "This is 'put' command.", cmd_put, &head[6] },

{ "ls", "This is 'ls' command.", cmd_ls, &head[7] },

{ "close", "This is 'close' command.", cmd_close, &head[8] },

{ "cd", "This is 'cd' command.", cmd_cd, &head[9] },

{ "get", "This is 'get' command.", cmd_get, NULL }

};

int main()

{

char cmd[CMD_MAX_LEN];

while (true)

{

printf("please input your command > ");

scanf("%s", cmd);

link_list* ptr = search_cmd(cmd, head);

if (ptr == NULL)

{

printf("ERROR command: '%s'\n", cmd);

}

else

{

printf("%s\n", ptr->cmd_annotation);

ptr->handler();

}

}

return 0;

}

运行和结果

多个程序编译生成可执行文件的命令是:gcc cmd_menu.c link_list.c link_list,h -o cmd_menu

实验结果见截图四

实验总结

搜索了linux模块化编译c就会看到一个叫makefile的东西,这个程序使用makefile 编译也是很方便的。

使用makefile修改会比较方便,直接用make可以自动编译。只是这个程序涉及的文件还比较少,不太需要用到。

c1fec42aa21f8699427ebe5b5f2cd806-wm

1d5448afbb8d605469d7bd7a75e730fe-wm

876a354c6cbbb84c94ffa15887c81f95-wm

17c5f6b2ec76665d6fb559d351621fc7-wm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值