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
实现内部模块化的命令行菜单小程序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
相关文章推荐
- SA17225160_李伯威(libowei)_高级软工第四次实验报告
- SA17225160_李伯威(libowei)_高级软工第五次实验报告
- SA17225160_李伯威(libowei)_高级软工第一次实验报告
- 20155307 2016-2017 《Java程序设计》第三次实验报告
- C++第三次实验报告-本月有几天
- 软工实验报告一
- 高级软件工程实验报告(第二次实验)
- 20145240 《Java程序设计》第三次实验报告
- 20145316第三次实验报告(优化版)
- 高级软件工程实验报告七
- 高级软工项目选题报告(团队)
- 第三次实验报告
- C++第三次实验报告
- 第三次C程序设计实验报告
- 20145309java第三次实验报告
- 第三次实验报告
- 信息安全系统设计基础第三次实验报告
- 20135213 20135231 信息安全系统设计基础课程第三次实验报告
- 第三次上机实验报告
- 第三次实验报告