火页凡 + 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006
一. 实验过程
Clone github库github.com/wangyufan/SE.git到实验环境
1.进入实验目录并创建实验一文件夹lab3
2.编写.c文件.h文件,实验代码如下:
typedef struct DataNode
{
char* cmd;
char* desc;
int (*handler)();
struct DataNode* next;
} tDataNode;
tDataNode* FindCmd(tDataNode* head,char* cmd);
int ShowAllCmd(tDataNode* head);#include
#include
#include "linklist.h"
tDataNode* FindCmd(tDataNode* head,char* cmd)
{
if(head == NULL || cmd == NULL)
{
return NULL;
}
tDataNode* p = head;
while(p != NULL)
{
if(strcmp(p->cmd,cmd) == 0)
{
return p;
}
p = p->next;
}
return NULL;
}
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;
}#include
#include
#include
#include "linklist.h"
void help();
void upper();
void version();
void lower();
void add();
void sub();
void multi();
void quit();
#define CMD_MAX_LENGTH 128
static tDataNode head[] =
{
{"help", "cmd tips.", help, &head[1]},
{"version","show version of this menu.", version, &head[2]},
{"quit", "exit cmd.", quit, &head[3]},
{"upper", "change the case into uppercase Letters.", upper, &head[4]},
{"lower", "change the case into lowercase Letters.", lower, &head[5]},
{"sub", "subtraction of two numbers.", sub, &head[6]},
{"add", "addition of two numbers.", add, &head[7]},
{"multi", "multiplication of two numbers.", multi, NULL}
};
int main()
{
char cmd[CMD_MAX_LENGTH];
while(1)
{
printf("menu cmd-> ");
scanf("%s", cmd);
tDataNode* p = FindCmd(head, cmd);
if(p == NULL)
{
printf("error: Wrong command!\n");
}
else
{
p->handler();
}
}
return 0;
}
void help()
{
printf("+--------+---------------------------------+\n");
printf("+ help + cmd tips +\n");
printf("+ quit + exit cmd +\n");
printf("+ version+ show version +\n");
printf("+ upper + change the case into uppercase Letters +\n");
printf("+ lower + change the case into lowercase Letters +\n");
printf("+ sub + subtraction of two numbers +\n");
printf("+ add + addition of two numbers +\n");
printf("+ multi + multiplication of two numbers +\n");
printf("+--------+---------------------------------+\n");
}
void quit()
{
exit(0);
}
void upper()
{
int i;
char arr[100];
printf("please input contents:");
scanf("%s", arr);
for(i=0; arr[i]!='\0'; i++)
{
if(arr[i]>='A'&&arr[i]<='Z')
arr[i]+=32;
}
printf("%s\n", arr);
}
void lower()
{
int i;
char arr[100];
printf("please input contents:");
scanf("%s", arr);
for(i=0; arr[i]!='\0'; i++)
{
if(arr[i]>='a'&&arr[i]<='z')
arr[i]-=32;
}
printf("%s\n", arr);
}
void version()
{
printf("the version is 1.0.0\n");
}
void add()
{
double num1,num2,count;
printf("+-------*--------*--------*--------*-------+\n");
printf("please input two numbers:");
scanf("%lf %lf",&num1, &num2);
char a;
count = num1 + num2;
a = '+';
printf("%lf %c %lf = %lf \n",num1, a, num2, count);
}
void sub()
{
double num1,num2,count;
printf("+-------*--------*--------*--------*-------+\n");
printf("please input two numbers:");
scanf("%lf %lf",&num1, &num2);
char a;
count = num1 - num2;
a = '-';
printf("%lf %c %lf = %lf \n",num1, a, num2, count);
}
void multi()
{
double num1,num2,count;
printf("+-------*--------*--------*--------*-------+\n");
printf("please input two numbers:");
scanf("%lf %lf",&num1, &num2);
char a;
count = num1 * num2;
a = '*';
printf("%lf %c %lf = %lf \n",num1, a, num2, count);
}3实验结果
实现的命令8个:
help帮助
version版本号
quit退出
upper将字符串中大写字母变小写
lower将字符串中小写字母变大写
add
sub
mult 分别计算两个数的加减乘
4.提交代码到版本库
二. 实验总结
1掌握模块化方法:
数据结构极其操作FindCmd和ShowAllCmd放在linklist.h;
FindCmd和ShowAllCmd的具体实现放在linklist.c;
菜单业务逻辑层放在menu.c;
这样数据存储和功能业务分离,有利于分开维护,实现模块化。
2学习了对于扩展开放,对于修改封闭的开-闭原则
3解决了了实验过程中的bug:
gcc编译出错:initializer element is not constant,
原因: 全局变量的值不能在编译时确定,要在执行时确定
6万+

被折叠的 条评论
为什么被折叠?



