2017 Fall Software Engineering Learning (2)
My first C project
公开发表一篇实验报告,并在实验报告中注明【网易云课堂昵称 + 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006 】
一、实验需求分析
- 可以广泛通用的命令行菜单子系统组件,可方便地定制而嵌入到其他系统;
- 程序循环、接收用户的命令,如help、others等命令;
- 类似ftp的help目录或者bash的help目录;
- 实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函作为执行动作,实现的命令个数不少于8个;
首先循环、接受用户命令——运用while(1)循环
用scanf()语句将每次用户输入的命令存进变量,然后用if-else控制判断从而执行每一个指令的具体实现。
二、程序具体实现
1、程序分解
为了程序的可维护性与可扩展性,我们将程序分为两个部分
在forgebash.c文件里面是程序的主循环。
在command.c里面是每一条命令的具体实现。
command.h是函数声明。
2、具体实现
首先是主程序,依照《构建之法》4.1代码规范 和高质量C++/C编程指南(林锐) 上面讲解的代码规范方式,我们写出了如下程序
/**************************************************************************************************/
/* >File Name: forgebash.c */
/* >Author: AcrididCheng */
/* >Mail: chenghuaming@aliyun.com */
/**************************************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "command.h"
int main()
{
char cmd[128];
printf("Thank you for using the forgebash program, type help for commands that sopport.\n");
while(1)
{
scanf("%s",cmd);
if (strcmp(cmd, "exit") == 0)
{
exit(0);
}
else if(strcmp(cmd, "help") == 0)
{
printf("\n"); //command1's description.... and so on
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("exit ---- type 'exit' to terminal the forgebash program.\n");
}
else if(strcmp(cmd, "command1") == 0)
{
do_command1();
}
else if(strcmp(cmd, "command2") == 0)
{
do_command2();
}
else if(strcmp(cmd, "command3") == 0)
{
do_command3();
}
else if(strcmp(cmd, "command4") == 0)
{
do_command4();
}
else if(strcmp(cmd, "command5") == 0)
{
do_command5();
}
else if(strcmp(cmd, "command6") == 0)
{
do_command6();
}
else if(strcmp(cmd, "command7") == 0)
{
do_command7();
}
}
return 0;
}
上述程序将每一个命令封装起来了,可以在其他文件中实现,这样便于管理和维护。
接下来是命令的具体实现程序,当然,这个程序可以在一个文件里,也可以在多个文件中。现在,我们为了保持代码的可读性,全放在一个文件里
/**************************************************************************************************/
// >File Name: command.c
// >Author: AcrididCheng
// >Mail: chenghuaming@aliyun.com
/**************************************************************************************************/
#include <stdio.h>
void do_command1()
{
printf("command1 processing ...\n");
}
void do_command2()
{
}
void do_command3()
{
}
void do_command4()
{
}
void do_command5()
{
}
void do_command6()
{
}
void do_command7()
{
}
可以看到,中间有许多空白。由于这次实验只要求编写一个支持多命令的伪命令行,所以对于每个命令是什么和具体做什么内容,我们留空以待之后编写。
再就是,我们将函数声明放到了统一的头文件中,这样便于阅读。
#pragma once
void do_command1();
void do_command2();
void do_command3();
void do_command4();
void do_command5();
void do_command6();
void do_command7();
3、编写Makefile
跟在windows上用IDE编写程序不同,我们编写的多文件项目要自己手动进行编译和连接。但是如果文件很多层次很复杂,那么要输入的命令就非常多。为了解决这一问题,Linux提供了一个很好的命令make,通过编写一个Makefile然后执行make这一指令我们就能告诉操作系统该如何处理这一系列操作。关于make 的教程,网上存在很多,而且Makefile里面的语法变化迅速,所以我仅仅贴出我的makefile代码。
在clone到我的代码 之后,进入目录下执行
make
你就能看到forgebash这一可执行文件
如果再执行
make clean
你就会发现所有的生成的文件被清楚掉了,这样保持了源码的纯粹性。
以下是Makefile的代码
objects = forgebash.o command.o
cc = gcc
forgebash : $(objects)
cc -o forgebash $(objects)
forgebash.o : command.h
.PHONY : clean
clean :
rm forgebash $(objects)
三、实验结果展示
为了更好的演示实验过程,一下进行一下实验展示
如图,我们先从github上clone下我的代码 然后编译
这里由于我们仅实装了help和exit命令,所以我只展示了这一部分
运行完后清理一下项目
至此实验就结束了。
四、实验总结
这次实验帮我们整体了解了一个项目的架构与过程,在编写过程中参考代码规范让我们的代码清晰易读,更重要的是,重新温习了一下Makefile的编写。这非常有意义!