实验资料
- 网易云课程地址:实验三:内部模块化的命令行菜单小程序V2.0
- 网易云课堂昵称:Natsukashiii
- 学号:SA17225129
- 我的github地址
实验要求
注意代码的业务逻辑和数据存储之间的分离,即将系统抽象为两个层级:菜单业务逻辑和菜单数据存储
具体要求
- 遵守代码风格规范,参考借鉴代码设计规范的一些方法;
- 代码的业务逻辑和数据存储使用不同的源文件实现,即应该有2个.c和一个.h作为接口文件。
实验内容
- 利用git clone 远程克隆exp文件夹到Code中,并在Code中新建文件夹lab3, 将lab2中的menu.c拷贝到lab3中;
- 编辑lab3中的menu.c;
- 创建linklist.c和linklist.h,将定义的数据结构放在inklist.h文件中,函数实现的功能放在linklist.c文件中;
- 完成代码并运行,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();
实验总结
代码开发的过程中,文件功能不单一会造成代码混乱,将功能单一的模块抽象出来形成接口供外界调用,可以提高代码的可维护性以及可读性。通过这次试验,了解了模块化的作用。