【Jerryykt1464929958440 + 《软件工程(C编码实践篇)》MOOC课程作业http://mooc.study.163.com/course/USTC-1000002006 】
github地址:https://github.com/JerryLittleBear/experiment.git
【我学到了】:
1、软件工程代码规范,如:操作符前后加空格,循环语句内部代码加4个空格,等。
2、确定自己的代码命名风格:变量命名/函数名用lowCamel风格,类/类型采用Pascal风格
3、模块化设计的思想,通过模块化设计,提高自己的代码的可重用性,提高工作效率。
4、巩固C语言知识,如gets函数和scanf("%s")的区别:
4.1 scanf()
所在头文件:stdio.h
语法:scanf("格式控制字符串",变量地址列表);
接受字符串时:scanf("%s",字符数组名或指针);
4.2 gets()
所在头文件:stdio.h
语法:gets(字符数组名或指针);
两者在接受字符串时:
4.3 不同点:
4.3.1 能否接受空白字符:
scanf不能接受空格、制表符Tab、回车等;
而gets能够接受空格、制表符Tab和回车等;
4.3.2 返回值:
scanf返回的为接收到的变量值的个数;
gets(str) 在读取成功的时候返回str,str保存的是成功读入的内容 在失败的时候返回NULL,str的内容保持不变;
4.3.3 是否在缓冲区留下字符:
scanf :当遇到回车,空格和tab键会自动在字符串后面添加'\0',但是回车,空格和tab键仍会留在输入的缓冲区中。
gets:可接受回车键之前输入的所有字符,并用'\n'替代 '\0'.回车键不会留在输入缓冲区中。
4.4 相同点:
字符串接受结束后自动加'\0'。
【实验环境】:
ubuntu16.04、git2.0
【代码关键】:
请看第4行和第81行代码,分别使用lowCamel风格命名函数和变量。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//using Pascal style to name the functions
void knowAboutAuthor();
void help();
void quit();
void saySth();
void talkToMe();
void goToToilet();
void minus();
void multiply();
int main()
{
char cmd[100];
while(1)
{
printf("please input command: ");
scanf("%s", cmd);
getchar();
if(!strcmp(cmd, "help"))
{
help();
}
else if(!strcmp(cmd, "knowauthor"))
{
knowAboutAuthor();
}
else if(!strcmp(cmd, "toilet"))
{
goToToilet();
}
else if(!strcmp(cmd, "minus"))
{
minus();
}
else if(!strcmp(cmd, "multiply"))
{
multiply();
}
else if(!strcmp(cmd, "talk"))
{
talkToMe();
}
else if(!strcmp(cmd, "sayhi"))
{
saySth();
}
else if(!strcmp(cmd, "quit"))
{
quit();
}
else
{
printf("wrong command,reinput\n");
}
}
return 0;
}
void knowAboutAuthor()
{
printf("his name is Jerry, he loves helen\n");
}
void help()
{
printf("hi! you can use commands as follow:\n");
printf("help\nquit\nknowauthor\ntoilet\ntalk\nsayhi\ntoilet\nminus\nmultiply\n");
}
void quit()
{
printf("now, quit the program\n");
exit(0);
}
void saySth()
{
int i;
char sentenceDuplicate[128];//using the lowCamel style to name a variable
printf("no matter what you say, I will repeat your sentence 3 times, lol!\n");
scanf("%s", sentenceDuplicate);
getchar();//吸收输入缓冲区里的回车符
for(i = 0;i < 3;i++)
{
printf("No.%d round repetition: %s\n", i, sentenceDuplicate);
}
}
void talkToMe()
{
char answer[20];
printf("question No.1: if you are a boy or girl?\n");
scanf("%s", answer);
getchar();
printf("this is a pretty %s.\n", answer);
}
void goToToilet()
{
printf("out of the door, turn left, is the toilet\n");
}
void minus()
{
int integer1, integer2;
printf("please input first integer:\\");
scanf("%d", &integer1);
getchar();
printf("please input second integer:\\");
scanf("%d", &integer2);
getchar();
printf("the resule is %d\n", integer1 - integer2);
}
void multiply()
{
int integer1, integer2;
printf("please input first integer:\\");
scanf("%d", &integer1);
getchar();
printf("please input second integer:\\");
scanf("%d", &integer2);
getchar();
printf("the resule is %d\n", integer1 * integer2);
}
【实验流程】:
1、用标准代码风格写代码。
2、上传到github进行版本控制。
【实验截图】:
1、程序编译运行结果
运行程序后,首先输入help指令,查看命令列表,共8个命令,接着使用minus和multiply命令进行减法、乘法运算,最后用quit指令退出命令行。
2、上传到github