软件工程实验三
SA17225491 张佳伟
实验目的
通过这次实验,实现一个命令行菜单,了解代码的基本结构,学会将业务逻辑和数据存储进行分离,增加代码的可扩展性。
实验过程
1.在github上创建一个名叫se_lab3的仓库,并克隆到本地仓库
2.创建三个文件分别命名为menu.c linklist.h linklist.c
其中menu.c用于实现业务逻辑,linklist.h用于实现数据结构与相应的操作,linklist.c用于操作的具体实现。
menu.c如下:
#include <stdio.h>
#include <stdlib.h>
#include "linklist.h"
#define MAX_CMD_LENGTH 128
#define DESC_LEN 1024
#define CMD_NUM 10
int Help();
int Quit();
int Add();
int Sub();
int Mult();
int Divide();
int Factorial();//阶乘运算
int Fibonaci();//斐波那契数列
static tDataNode head[]={
{"help", "|this is a help CMD\n", Help, &head[1]},
{"version", "|the version is 1.0.0\n", NULL, &head[2]},
{"add", "|add two integer.\n", Add, &head[3]},
{"subtract", "|do the subtract.\n", Sub, &head[4]},
{"mult", "|multiply two integers.\n", Mult, &head[5]},
{"divide", "|divide two integers.\n", Divide, &head[6]},
{"factorial", "|to do factorial(阶乘运算).\n", Factorial, &head[7]},
{"fibonaci", "|to do fibonaci array(斐波那契数列计算).\n", Fibonaci, &head[8]},
{"quit", "|to quit this CMD program.\n", Quit, NULL},
};
int main()
{
char CMD[MAX_CMD_LENGTH];
while(1)
{
printf("Please input a CMD.\n");
scanf("%s", CMD);
tDataNode *temp = FindCmd(head, CMD);
if(!temp)
{
printf("the wrong CMD~\n");
continue;
}
else if(temp->handle != NULL)
temp->handle();
}
}
int Help()
{
tDataNode *p = head;
ShowAllCmd(p);
return 0;
}
int Quit()
{
exit(0);
return 0;
}
int Add()
{
printf("pelease input two integers:\n");
int temp1,temp2;
scanf("%d%d",&temp1,&temp2);
printf("The result is :%d\n", temp1+temp2);
return 0;
}
int Sub()
{
int temp1,temp2;
printf("please input two integers:\n");
scanf("%d%d", &temp1,&temp2);
printf("The result is :%d\n", temp1-temp2);
return 0;
}
int Mult()
{
printf("pelease input two integers:\n");
int temp1,temp2;
scanf("%d%d",&temp1,&temp2);
printf("The result is :%d\n", temp1*temp2);
return 0;
}
int Divide()
{
printf("pelease input two integers:\n");
int temp1,temp2;
scanf("%d%d",&temp1,&temp2);
printf("The result is :%d\n", temp1/temp2);
return 0;
}
int Factorial()//阶乘运算;
{
printf("pelease input the number of integer:\n");
int count;
scanf("%d", &count);
if(count < 0)
return -1;
int i = 1;
int value = 1;
while(i <= count)
{
value *= i;
++i;
}
printf("The result is:%d\n", value);
return 0;
}
int Fibonaci()//斐波那契数列计算;
{
printf("pelease input the number of integer:\n");
int temp1=1,temp2=1;
int count;
scanf("%d", &count);
if(count < 0)
{
printf("The wrong number.\n");
return 0;
}
else if(count==1||count==2)
{
printf("The result is :1.\n");
return 0;
}
for(int i=3;i<=count;i++)
{
int temp;
temp = temp2;
temp2 += temp1;
temp1 = temp;
}
printf("The result is :%d\n", temp2);
return 0;
}
linklist.h 如下:
typedef struct DataNode
{
char * CMD;
char * desc;
int (*handle)();
struct DataNode * next;
}tDataNode;
int ShowAllCmd(tDataNode * head);
tDataNode *FindCmd(tDataNode * head, char * cmd);
linklist.c如下:
#include "linklist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int ShowAllCmd(tDataNode * head)
{
printf("Menu List :\n");
printf("**********************\n");
if(head == NULL)
return 0;
tDataNode *q = head;
while(q)
{
printf("%s,%s\n", q->CMD, q->desc);
q = q->next;
}
printf("**********************\n");
return 0;
}
tDataNode * FindCmd(tDataNode * head, char * cmd)
{
if(head == NULL||cmd == NULL)
return NULL;
tDataNode *q = head;
while(q)
{
if(strcmp(q->CMD, cmd) == 0)
{
return q;
}
q = q->next;
}
return NULL;
}
3.将三个源文件进行组合编译如下:
4.命令行小菜单演示如下:
总结
通过这次实验的经历,认真总结了代码的设计思路,知道了如何写出高质量可扩展的代码,为今后更庞大的代码结构打下了良好的基础。