se_lab3

软件工程实验三

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.命令行小菜单演示如下:

这里写图片描述
这里写图片描述

总结

通过这次实验的经历,认真总结了代码的设计思路,知道了如何写出高质量可扩展的代码,为今后更庞大的代码结构打下了良好的基础。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值