您的位置:首页 > 其它

SA17225160_李伯威(libowei)_高级软工第三次实验报告

2017-10-04 16:19 204 查看
一、实验要求

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

注意代码的业务逻辑和数据存储之间的分离,即将系统抽象为两个层级:菜单业务逻辑和菜单数据存储

要求:
遵守代码风格规范,参考借鉴代码设计规范的一些方法;
代码的业务逻辑和数据存储使用不同的源文件实现,即应该有2个.c和一个.h作为接口文件。

二、实验过程
1.clone并进入共享文件夹



2.建立指定的lab3文件夹



3.用vim编辑linklist.c文件











#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"

static tDataNode head[] =
{
{"help","This is Help",help,&head[1]},
{"add","This is Add input 2 integers!",add,&head[2]},
{"sub","This is Sub input 2 integers!",sub,&head[3]},
{"multiple","This is Multiple input 2 integers!",multiple,&head[4]},
{"divide","This is Divide input 2 integers!",divide,&head[5]},
{"quit","This is Quit will quit from program!",quit,NULL}
};

tDataNode* FindCmd(tDataNode* head,char* cmd)
{
if(head==NULL||cmd==NULL)
{
return NULL;
}
tDataNode* p=head;
while(p!=NULL&&strcmp(p->cmd,cmd)!=0)
{
p=p->next;
}
if(p==NULL)
{
return NULL;
}
return p;
}

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;
}

int Help()
{
ShowAllCmd(head);
}

int Add()
{
printf("please input two integers\n");
int x,y;
scanf("%d%d",&x,&y);
int result=x+y;
printf("%d\n",result);
return 0;
}

int Sub()
{
printf("please input two integers\n");
int x,y;
scanf("%d%d",&x,&y);
int result=x-y;
printf("%d\n",result);
return 0;
}

int Multiple()
{
printf("please input two integers\n");
int x,y;
scanf("%d%d",&x,&y);
int result=x*y;
printf("%d\n",result);
return 0;
}

int Divide()
{
printf("please input two integers\n");
int x,y;
scanf("%d%d",&x,&y);
int result=x/y;
printf("%d\n",result);
return 0;
}

int Quit()
{
exit(0);
}


4.编辑linklist.h文件



#include <stdio.h>
#include <stdlib.h>

typedef struct DataNode
{
char* cmd;
char* desc;
int(*handler)();
struct DataNode* next;
}tDataNode;

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

int help();
int quit();
int add();
int sub();
int multiple();
int divide();5.编写menu.c文件





#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"

static tDataNode head[] =
{
{"help","This is Help",help,&head[1]},
{"add","This is Add input 2 i
9bcc
ntegers!",add,&head[2]},
{"sub","This is Sub input 2 integers!",sub,&head[3]},
{"multiple","This is Multiple input 2 integers!",multiple,&head[4]},
{"divide","This is Divide input 2 integers!",divide,&head[5]},
{"quit","This is Quit will quit from program!",quit,NULL}
};

int main()
{
while(1)
{
char cmd[128];
printf("please put in a cmd\n");
scanf("%s",cmd);
tDataNode *p=FindCmd(head,cmd);
if(p==NULL)
{
printf("wrong command!\n");
continue;
}
printf("%s:%s\n",p->cmd,p->desc);
if(p->handler!=NULL)
{
p->handler();
}
}
}

6.编译menu.c以及linklist.c文件



7.运行结果



8.上传github







9.github的上传情况



10.我的github地址
https://github.com/lili0001002/cs122/tree/master/lab3
总结:

学习了模块化的编程思想
提高了menu程序的重用性
掌握学习了c程序的编译调试方法
KISS(keep it simple & stupid)
using design to frame the code(matching design with implementation)
including pseuducode
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: