软件工程(C编码实践篇) 第三次作业实验报告

来源:互联网 发布:js设置input的值 编辑:程序博客网 时间:2024/05/21 18:35

网易云课堂昵称:sa17225159
《软件工程(C编码实践篇)》MOOC课程作业 http://mooc.study.163.com/course/USTC-1000002006

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

实验要求

  • 注意代码的业务逻辑和数据存储之间的分离,即将系统抽象为两个层级:菜单业务逻辑和菜单数据存储
  • 要求:1)遵守代码风格规范,参考借鉴代码设计规范的一些方法;2)代码的业务逻辑和数据存储使用不同的源文件实现,即应该有2个.c和一个.h作为接口文件。

实验内容

  • 模块化 软件工程(C编码实践篇) 第二次作业实验报告 中的命令行菜单小程序。将程序抽象出数据层和逻辑层。
  • 通过设计简单的命令行菜单细小程序,了解并掌握程序内部模块化设计的思想,实现菜单数据存储与菜单业务逻辑的分离.
  • 用一个静态链表来存储可变化的菜单命令及其对应函数,在主函数中实菜单命令逻辑

实验过程

1.建立远程仓库,并下载到本地,远程仓库地址:https://github.com/libaoquan95/seClass_lab3.git

git clone https://github.com/libaoquan95/seClass_lab3.git

这里写图片描述
2.进入版本库并创建 menu.c linklist.h linklist.c 文件
进入本地库
这里写图片描述
创建menu.c
这里写图片描述
创建linklist.h linklist.c
这里写图片描述
3.拆分源程序,分离出菜单业务逻辑和菜单数据存储
在一个头文件中定义链表数据结构及其操作原型,并在相应源文件中实现其操作
然后在主函数源文件中实现菜单命令。
3.1 linklist.h

#ifndef LINHLIST_H#define LINHLIST_H// struct of command listtypedef struct DataNode{    char * cmd;    char * desc;    int (*handler)();    struct DataNode *next;}tDataNode;// find a commandtDataNode * FindCmd(tDataNode *head, char *cmd);// print all command and it's informationint ShowAllCmd(tDataNode *head);#endif

3.2 linklist.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include "linklist.h"/** * find a command * @param head of command list, command * @return none */tDataNode* FindCmd(tDataNode *head, char *cmd){    if(head == NULL || cmd == NULL)    {        return NULL;    }    tDataNode *p = head;    while(p != NULL)    {        if(strcmp(p->cmd, cmd) == 0)        {            return p;        }        p = p->next;    }    return NULL;}/** * print all command and it's information * @param head of command list * @return none */int ShowAllCmd(tDataNode *head){    tDataNode *p = head;    printf("-------------------------------------------------------------------\n");    while(p !=NULL)    {        printf("\t\t %s: \t\t %s\n", p->cmd, p->desc);        p = p->next;    }    printf("-------------------------------------------------------------------\n");    return 0;}

3.3 menu.c

#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#include "linklist.h"#define CMD_MAX_LEN 128int PrintCommand();int PrintSystemTime();int PrintCurrentWorkingDirectory();int Add();int Sub();int Mul();int Div();int Quit();/* menu program */static tDataNode head[] ={    {"help", "Print all command of menu",       PrintCommand,                   &head[1]},    {"time", "Show system time",                PrintSystemTime,                &head[2]},    {"pwd",  "Show current working directory",  PrintCurrentWorkingDirectory,   &head[3]},    {"add",  "Calculate the summarize of the two integer numbers",      Add,    &head[4]},    {"sub",  "Calculate the subtractions of the two integer numbers",   Sub,    &head[5]},    {"mul",  "Calculate the multiplication of the two integer numbers", Mul,    &head[6]},    {"div",  "Calculate the division of the two integer numbers",       Div,    &head[7]},    {"quit", "Exit menu program",               Quit,                           NULL}};int main (){    // store menu's commands    char cmd[CMD_MAX_LEN];    // processing commands    while(1)    {        // input a command        printf("$menu > ");        scanf("%s", cmd);        tDataNode *p = FindCmd(head, cmd);        if(p == NULL)        {            printf("ERROR: This is not a command, you can input 'help' to find command\n");            continue;        }        if(p->handler != NULL)        {            p->handler();        }    }    return 0;}/** * print all command and it's information * @param none * @return none */int PrintCommand(){    ShowAllCmd(head);    return 0;}/** * print current time * @param none * @return none */int PrintSystemTime(){    struct tm *ptr;    time_t it;    it = time(NULL);    ptr = localtime(&it);    printf("%4d年%02d月%02d日  %d:%d:%d\n", ptr->tm_year + 1900, ptr->tm_mon + 1, ptr->tm_mday,                                           ptr->tm_hour, ptr->tm_min,ptr->tm_sec);    return 0;}/** * print current working directory path * @param none * @return none */int PrintCurrentWorkingDirectory(){    char buf[256];    getcwd(buf, sizeof(buf));    printf("当前路径:  %s\n", buf);    return 0;}/** * calculate add of two interage numbers * @param none * @return calculate result */int Add(){    int num1 = 0, num2 = 0;    printf("input number1:");    scanf("%d", &num1);    printf("input number2:");    scanf("%d", &num2);    printf("%d + %d = %d\n", num1, num2, num1 + num2);    return 0;}/** * calculate sub of two interage numbers * @param none * @return calculate result */int Sub(){    int num1 = 0, num2 = 0;    printf("input number1:");    scanf("%d", &num1);    printf("input number2:");    scanf("%d", &num2);    printf("%d - %d = %d\n", num1, num2, num1 - num2);    return 0;}/** * calculate mul of two interage numbers * @param none * @return calculate result */int Mul(){    int num1 = 0, num2 = 0;    printf("input number1:");    scanf("%d", &num1);    printf("input number2:");    scanf("%d", &num2);    printf("%d * %d = %d\n", num1, num2, num1 * num2);    return 0;}/** * calculate div of two interage numbers * @param none * @return calculate result */int Div(){    int num1 = 0, num2 = 0;    printf("input number1:");    scanf("%d", &num1);    printf("input number2:");    scanf("%d", &num2);    if(num2 != 0)    {        printf("%d / %d = %d\n", num1, num2, num1 / num2);    }    else    {        printf("ERROR: don't division 0\n");    }    return 0;}/** * quit command * @param none * @return none */int Quit(){    exit(0);    return 0;}

4.编译程序并运行
4.1 编译

gcc linklist.h linklist.c menu.c -o menu./menu

这里写图片描述
4.2 运行
help 命令
这里写图片描述
time 命令
这里写图片描述
pwd 命令
这里写图片描述
add 命令
这里写图片描述
sub 命令
这里写图片描述
mul 命令
这里写图片描述
div 命令
这里写图片描述
错误的命令
这里写图片描述
quit 命令
这里写图片描述
5.上传至远程版本库
这里写图片描述
这里写图片描述


实验总结

远程仓库地址:https://github.com/libaoquan95/seClass_lab3.git

通过这次菜单程序模块化设计使我学会了许多的软件工程思想,体会到了模块化的巨大作用

模块化开发对扩展有极大的帮助,对C语言来说,仅将main函数作为入口,是较好的编程开发方法。

日常编程中,应该尽量将复用、独立的方法与功能,外置到.c文件或者其他模块包中,方便日后进行扩展与维护。

对于gcc编译器对include的文件处理,面对复杂程序,有大量的包与.c文件存在时,gcc提供了make命令,可以根据config手册自动编译。但复杂程序的config文件编写本身就十分复杂,因此,采用IDE集成开发环境对于复杂软件开发更快捷方便。

阅读全文
0 0
原创粉丝点击