网易云课堂昵称:风清扬pty +http://mooc.study.163.com/course/USTC-1000002006
一、实验要求
1、代码风格规范:
- 代码风格的原则:简明、易读、无二义性;
- 缩进、命名、注释等代码编排的风格规范;
- 《构建之法》4.1代码规范
2、具体要求(参照视频中的具体实验过程):
- 实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函数作为执行动作,实现的命令个数不少于8个;
- 类似ftp的help目录或者bash的help目录;
- 程序循环、接收用户的命令,如help、others等命令;
二、实验过程
1、更改vimrc设置
- <span style="font-size:14px;">set tabstop=4</span>
- <span style="font-size:14px;">set cindent</span>
- <span style="font-size:14px;">set hlsearch</span>
2、打开实验目录,新建并打开文件夹lab2
- cd documents/code
- mkdir lab2
- cd lab2
- cd documents/code
- mkdir lab2
- cd lab2
3、新建menu.c文件并进入vim编辑器
- vi menu.c
4、进行c语言编程,完成命令行的菜单小程序,完整代码如下
- #include <stdio.h>
- #include <stdlib.h>
- void help();
- void quit();
- void hello();
- void bigger();
- void add();
- void sub();
- void multi();
- void divi();
- int main()
- {
- char cmd[256];
- while (1)
- {
- scanf ("%s", cmd);
- if (strcmp(cmd, "help") == 0)
- {
- help();
- }
- else if (strcmp(cmd, "quit") == 0)
- {
- quit();
- }
- else if (strcmp(cmd, "hello") == 0)
- {
- hello();
- }
- else if (strcmp(cmd, "bigger") == 0)
- {
- bigger();
- }
- else if (strcmp(cmd, "add") == 0)
- {
- add();
- }
- else if (strcmp(cmd, "sub") == 0)
- {
- sub();
- }
- else if (strcmp(cmd, "multi") == 0)
- {
- multi();
- }
- else if (strcmp(cmd, "div") == 0)
- {
- divi();
- }
- else
- {
- printf("Error Command!\n\n");
- help();
- }
- }
- return 0;
- }
- void help()
- {
- printf("+--------+---------------------------------+\n");
- printf("+ name + desciption +\n");
- printf("+--------+---------------------------------+\n");
- printf("+ help + cmd tips +\n");
- printf("+ quit + exit cmd +\n");
- printf("+ hello + welcome +\n");
- printf("+ bigger + bigger of two numbers +\n");
- printf("+ add + addition of two numbers +\n");
- printf("+ sub + subtraction of two numbers +\n");
- printf("+ multi + multiplication of two numbers +\n");
- printf("+ div + division of two numbers +\n");
- printf("+--------+---------------------------------+\n");
- }
- void quit()
- {
- exit(0);
- }
- void hello()
- {
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Hi~My name is He Jinlong\n");
- printf("Thank you for use my cmd program!\n");
- }
- void bigger()
- {
- double num1,num2,bigger;
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Bigger!\nplease input two numbers:");
- scanf("%lf %lf",&num1,&num2);
- bigger = num1 > num2 ? num1 : num2;
- printf("%lf is bigger than the other\n",bigger);
- }
- void add()
- {
- double num1,num2,add;
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Addition!\nplease input two numbers:");
- scanf("%lf %lf",&num1, &num2);
- add = num1 + num2;
- printf("%lf + %lf = %lf \n",num1, num2, add);
- }
- void sub()
- {
- double num1,num2,sub;
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Subtraction!\n please input two numbers:");
- scanf("%lf %lf",&num1, &num2);
- sub = num1 - num2;
- printf("%lf - %lf = %lf\n",num1, num2, sub);
- }
- void multi()
- {
- double num1,num2,multi;
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Multiplcation!\n please input two numbers:");
- scanf("%lf %lf",&num1, &num2);
- multi = num1 * num2;
- printf("%lf * %lf = %lf\n",num1, num2, multi);
- }
- void divi()
- {
- double num1,num2,div;
- printf("+-------*--------*--------*--------*-------+\n");
- printf("Division!\n please input two numbers:");
- scanf("%lf %lf",&num1, &num2);
- if (num2 == 0)
- {
- printf("Error:divisor can not be zero!\n");
- }
- else
- {
- div = num1 / num2;
- printf("%lf / %lf = %lf\n",num1, num2, div);
- }
- }
5、程序代码部分截图