“软件工程(C编码实践篇)”实验报告【实验三:内部模块化的命令行菜单小程序V2.0】

本实验通过创建命令行菜单小程序,实现了多种功能选项,包括帮助、问候语、计算器等。采用模块化设计,将业务逻辑与数据存储分离,提高了代码的可维护性和可读性。

实验资料

  1. 网易云课程地址:实验三:内部模块化的命令行菜单小程序V2.0
  2. 网易云课堂昵称:Natsukashiii
  3. 学号:SA17225129
  4. 我的github地址

实验要求

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

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

实验内容

  1. 利用git clone 远程克隆exp文件夹到Code中,并在Code中新建文件夹lab3, 将lab2中的menu.c拷贝到lab3中;
  2. 编辑lab3中的menu.c;
  3. 创建linklist.c和linklist.h,将定义的数据结构放在inklist.h文件中,函数实现的功能放在linklist.c文件中;
  4. 完成代码并运行,push至github上。

实验过程

1.利用git clone 远程克隆exp文件夹到Code中,并在Code中新建文件夹lab3, 将lab2中的menu.c拷贝到lab3中;
这里写图片描述

2.编辑lab3中的menu.c;

这里写图片描述

这里写图片描述

这里写图片描述

3.创建calculator.h,并进行编辑

这里写图片描述

4.创建calculator.c,并进行编辑

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

5.编译运行

这里写图片描述

这里写图片描述


实验代码

  • menu.c
//
//  main.c
//  menu.c
//
//  Created by Natsukashii on 2017/9/21.
//  Copyright © 2017年 Natsukashii. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "calculator.h"

int cmdHelp()
{
    printf(
            "1.\thelp\t\tShow this help list\t\t\t\n"
                    "2.\thello\t\tSay Hello\t\t\t\t\n"
                    "3.\tdate\t\tShow the time and date\n"
                    "4.\tpwd\t\t\tShow the working directory\n"
                    "5.\tls\t\t\tList the files\n"
                    "6.\tcalculator\tA simple calculator\t\t\t\n"
                    "7.\tgame\t\tGaming time\t\t\t\t\n"
                    "8.\tquit\t\tQuit the menu program\t\t\t\n"
                    "\t\t\t\t\t\t\t\t\n"
    );
    return 0;
}

int cmdHello()
{
    printf("WINNER WINNER ,CHICKEN DINNER!\n");
    return 0;
}

int cmdgame()
{
    printf("You should study now!\n");
    return 0;
}

void calculatorProc()
{
    /* 1、操作数与被操作数输入 */
    calculatorInput();

    /* 2、计算 */
    cmdCalculator();

    /* 3、结果输出 */
    calculatorOutPut();
}

int main()
{
    char cmd[128];
    while (1)
    {
        printf("Menu:");
        scanf("%s", cmd);
        if (strcmp(cmd, "help") == 0)
        {
            cmdHelp();
        }
        else if (strcmp(cmd, "ls") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "game") == 0)
        {
            cmdgame();
        }
        else if (strcmp(cmd, "pwd") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "date") == 0)
        {
            system(cmd);
        }
        else if (strcmp(cmd, "calculator") == 0)
        {
            calculatorProc();
        }
        else if (strcmp(cmd, "hello") == 0)
        {
            cmdHello();
        }
        else if (strcmp(cmd, "quit") == 0)
        {
            exit(0);
        }
        else
        {
            printf("Error: undefined command, please enter 'help' to get help\n");
        }
    }
}
  • calculator.c
//
// Created by Natsukashii on 2017/10/6.
//

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

PCalcInfo gpCalcInfo;

int cmdCalculator()
{
    FLOAT num1 = gpCalcInfo->operNum;
    FLOAT num2 = gpCalcInfo->opedNum;
    FLOAT sum = 0;
    CHAR oper = gpCalcInfo->oper;

    switch (oper) {
        case '+': sum = num1+num2; break;
        case '-': sum = num1-num2; break;
        case '*': sum = num1*num2; break;
        case '/': sum =(num2==0)?(0):(num1/num2); break;
        default:
            break;
    }

    gpCalcInfo->result = sum;

    return 0;
}

void calcInfoInit(FLOAT num, FLOAT numed, CHAR oper)
{
    gpCalcInfo = (PCalcInfo)malloc(sizeof(CalcInfo));

    gpCalcInfo->operNum = num;
    gpCalcInfo->opedNum = numed;
    gpCalcInfo->oper = oper;
}

void calcInfoDestory()
{
    free(gpCalcInfo);
}

void calculatorInput()
{
    FLOAT num,numed;
    CHAR oper;

    printf("Please Input operand,operanded and operation : ");

    scanf("%f%f%s",&num,&numed,&oper);

    calcInfoInit(num,numed,oper);
}

void calculatorOutPut()
{
    printf("%g%c%g=%g\n",gpCalcInfo->operNum,gpCalcInfo->oper,gpCalcInfo->opedNum,gpCalcInfo->result);

    calcInfoDestory();
}
  • calculator.h

typedef unsigned int UINT32;
typedef int          INT32;
typedef float        FLOAT;
typedef char         CHAR;

typedef struct CalcInfo
{
    FLOAT operNum;                  //操作数
    FLOAT opedNum;                  //被操作数
    CHAR  oper;                     //运算方式
    FLOAT result;
}CalcInfo;

typedef CalcInfo* PCalcInfo;

int cmdCalculator();

void calculatorInput();

void calculatorOutPut();

实验总结

代码开发的过程中,文件功能不单一会造成代码混乱,将功能单一的模块抽象出来形成接口供外界调用,可以提高代码的可维护性以及可读性。通过这次试验,了解了模块化的作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值