实验要求:
实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函数作为执行动作,实现的命令个数不少于8个。
代码风格规范:
代码风格的原则:简明、易读、无二义性;
缩进、命名、注释等代码编排的风格规范;
实验过程:
在项目文件夹下建立lab2文件夹,并在文件夹下建立menu.c文件;
文件内容如下(代码见文末):
命令行可执行help update load save delete others quit等功能,并会在执行非法操作时报错。
运行结果如下:
测试方法:
git clone https://git.coding.net/zhangxuri198/E1.git
cd E1
cd lab2
gcc -o menu menu.c
vi menu.c
可使用./menu测试功能
可使用命令有:
help update load save delete others quit
心得与感悟:
代码可读性是评价一段代码好坏的重要标准,可读性不高的代码是不好维护与管理的。规范代码格式是提高代码可读性的最便捷方法,在代码书写过程中,良好的书写习惯将会为以后的工作带来很多便利。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char cmd[128];
while(1)
{
scanf("%s", cmd);
if(strcmp(cmd,"help")==0)
{
printf("This is help command!\n");
}
else if(strcmp(cmd,"version")==0)
{
printf("This is version command!\n");
}
else if(strcmp(cmd,"update")==0)
{
printf("This is update command!\n");
}
else if(strcmp(cmd,"load")==0)
{
printf("This is load command!\n");
}
else if(strcmp(cmd,"save")==0)
{
printf("This is save command!\n");
}
else if(strcmp(cmd,"delete")==0)
{
printf("This is delete command!\n");
}
else if(strcmp(cmd,"others")==0)
{
printf("This is others command!");
}
else if(strcmp(cmd,"quit")==0)
{
exit(1);
}
else
{
printf("Wrong command!\n");
}
}
}