《C编码实践篇》实验二——命令行菜单小程序的实现

来源:互联网 发布:ubuntu谷歌拼音输入法 编辑:程序博客网 时间:2024/06/07 07:36

一、实验内容

  • 实现一个命令行的菜单小程序,执行某个命令时调用一个特定的函数作为执行动作,实现的命令个数不少于8个;

  • 类似ftp的help目录或者bash的help目录;

  • 程序循环、接收用户的命令,如help、others等命令;

  • 可以广泛通用的命令行菜单子系统组件,可方便地定制而嵌入到其他系统;

二、详细代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void help();
void smaller();
void sum();
void bigger();
void quit();
void jian();
void cheng();
void chu();
int main ()
{
    char command[256];
    while (1)
    {
        scanf ("%s",command);
        if (strcmp(command,"help") == 0)
            {
            help();
            }
        else if (strcmp(command,"quit") == 0)
            {
            quit();
            }
                else if (strcmp(command,"sum") == 0)
                    {
                        sum();
                    }
                else if (strcmp(command,"bigger") == 0)
                    {
                        bigger();
                    }
                else if (strcmp(command,"jian") == 0)
                    {
                        jian();
                    }
                else if (strcmp(command,"cheng") == 0)
                    {
                        cheng();
                    }
                else if (strcmp(command,"smaller") == 0)
                    {
                        smaller();
                    }
                else if (strcmp(command,"chu") == 0)
                    {
                        chu();
                    }
                else
                    {
            printf("Error:This a wrong command!\n");
                        help();
                    }

    }
    return     0;
}
void help()
{
    printf("----------------------------\n");
    printf("command------What can do----\n");
    printf("quit-----quit this program.-\n");
    printf("bigger---output the bigger--\n");
    printf("smaller--output the smaller-\n");
    printf("jia-----------A+B-----------\n");
    printf("jian----------A-B-----------\n");
    printf("cheng---------AxB-----------\n");
    printf("chu-----------A/B-----------\n");
}    
void quit()
{
    exit(0);
}
void bigger()
{
    double a,b,c;
    printf("input two numbers:\n");
    scanf("%lf,%lf",&a,&b);
    c=a>b?a:b;
    printf("The bigger one is:");
    printf("%lf\n",c);
}
void smaller()
{
        double a,b,c;
        printf("input two numbers:\n");
        scanf("%lf,%lf",&a,&b);
        c=a<b?a:b;
        printf("The smaller one is:");
        printf("%lf\n",c);
}
void sum()
{
        double a,b,c;
        printf("input two numbers:\n");
        scanf("%lf,%lf",&a,&b);
        c=a+b;
        printf("The sum is:");
        printf("%lf\n",c);
}
void jian()
{
        double a,b,c;
        printf("input two numbers:\n");
        scanf("%lf,%lf",&a,&b);
        c=a-b;
        printf("The sub is:");
        printf("%lf\n",c);
}
void cheng()
{
        double a,b,c;
        printf("input two numbers:\n");
        scanf("%lf,%lf",&a,&b);
        c=a*b;
        printf("The mul is:");
        printf("%lf\n",c);
}
void chu()
{
        double a,b,c;
        printf("input two numbers:\n");
        scanf("%lf,%lf",&a,&b);
        c=a/b;
        printf("The div is:");
        printf("%lf\n",c);
}

三、实验思路及具体过程

        思路:构造八个函数通过不同的输入来确定具体调用什么函数,用一个while循环来执行。

        过程:1.创建文件并用vim完成编辑(set设置更方便的编写代码,提高可读性)。

                    2.编译并运行该程序。


                    3。提交到github

四、关键代码截图及实验运行结果截图

关键代码截图:


实验运行结果截图:

五、实验总结

复习了以往的C语言编码,在编写过程中经常出现各种小的语法错误,导致浪费了很多时间,在经常训练的过程中这种问题出现的频率肯定会越来越低。更大的收获在于懂得了代码简明、易读、无二义性的重要性,在编写过程中时刻缩进、命名、注释等代码编排的风格规范,为今后的学习工作生活打下了很好的基础。

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