“软件工程(C编码实践篇)”实验报告【实验二:命令行菜单小程序V1.0】

来源:互联网 发布:淘宝店铺重开 编辑:程序博客网 时间:2024/05/18 01:31

实验资料

  1. 网易云课程地址:实验二:命令行菜单小程序V1.0
  2. 网易云课堂昵称:Natsukashiii
  3. 学号:SA17225129
  4. 我的github地址

实验要求

代码风格规范:

  • 代码风格的原则:简明、易读、无二义性;
  • 缩进、命名、注释等代码编排的风格规范;

具体要求

  • 实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函数作为执行动作,实现的命令个数不少于8个;
  • 类似ftp的help目录或者bash的help目录;
  • 程序循环、接收用户的命令,如help、others等命令;
  • 可以广泛通用的命令行菜单子系统组件,可方便地定制而嵌入到其他系统;

实验过程

实验代码

这里写图片描述
这里写图片描述

//  main.c//  menu.c////  Created by Natsukashii on 2017/9/21.//  Copyright © 2017年 Natsukashii. All rights reserved.//#include <stdio.h>#include <stdlib.h>#include <string.h>int cmdHelp(){    printf(           "1.\thelp\t\tShow this help list\t\t\t\n"           "2.\thello\t\tSay Hello\t\t\t\t\n"           "3.\tdate\t\tShow the time and date\n"           "4.\tpwd\t\t\tShow the working directory\n"           "5.\tls\t\t\tList the files\n"           "6.\tcaculator\tA simple caculator\t\t\t\n"           "7.\tgame\t\tGaming time\t\t\t\t\n"           "8.\tquit\t\tQuit the menu program\t\t\t\n"           "\t\t\t\t\t\t\t\t\n"           );    return 0;}int cmdCaculator(){    float num1 = 0.0,num2 = 0.0,sum = 0.0;    char ope = '\0';    printf("Please enter the number and operation (like'3+4') \n");    scanf("%f %c %f", &num1,&ope,&num2);    switch (ope) {        case '+': sum = num1+num2; break;        case '-': sum = num1-num2; break;        case '*': sum = num1*num2; break;        case '/': sum =(num2==0)?(0):(num1/num2); break;        default:            break;    }    printf("%g%c%g=%g\n",num1,ope,num2,sum);    return 0;}int cmdHello(){    printf("WINNER WINNER ,CHICKEN DINNER!\n");    return 0;}int cmdgame(){    printf("You should study now!\n");    return 0;}int main(){    char cmd[128];    while (1)    {        printf("Menu:");        scanf("%s", cmd);        if (strcmp(cmd, "help") == 0)        {            cmdHelp();        }        else if (strcmp(cmd, "ls") == 0)        {            system(cmd);        }        else if (strcmp(cmd, "game") == 0)        {            cmdgame();        }        else if (strcmp(cmd, "pwd") == 0)        {            system(cmd);        }        else if (strcmp(cmd, "date") == 0)        {            system(cmd);        }        else if (strcmp(cmd, "caculator") == 0)        {            cmdCaculator();        }        else if (strcmp(cmd, "hello") == 0)        {            cmdHello();        }        else if (strcmp(cmd, "quit") == 0)        {            exit(0);        }        else        {            printf("Error: undefined command, please enter 'help' to get help\n");        }    }}

运行结果

对menu.c进行编译

这里写图片描述

通过menu实现如下8个功能,当输入字符不是menu中的命令时,会提示错误并提示输入help字段。

  1. help:输出help内容,提示进行操作;
  2. hello:输出hello内容;
  3. date: 系统调用功能输出当前时间日期;
  4. pwd: 系统调用功能输出所在文件夹;
  5. ls:系统调用功能输出当前文件夹所有文件;
  6. caculator:简易计算器;
  7. game:输出game内容;
  8. quit:退出menu菜单。

这里写图片描述

以下是menu实现的除help外的其他功能

这里写图片描述

推送本地的更新

这里写图片描述


实验总结

通过本次实验了解代码的规范性,并了解linux和c语言的基本规范和操作,需要不断练习来提高代码质量。

阅读全文
0 0
原创粉丝点击