加减乘除+菜单实现

#include<stdio.h>
#include<ctype.h>


double Arithmetic(char ch, double x, double y);
char Menu(void);
double input(void);

int main(void)
{
    char ch;
    double x,y;
    while(ch=Menu())
    {
        printf("Enter first number:");
        x=input();
        printf("Enter second number:");
        y=input();
        printf("
%.2f %c %.2f = %.2f
",x, ch, y, Arithmetic(ch, x,y));
    }
    printf("done
");
    return 0;
}
double Arithmetic(char ch, double x, double y)
{
        switch(ch)
    {
             case '+' :
                 return x+y;
             case '-' :
                 return x-y;
             case '*' :
                 return x*y;
             case '/' :
                 return x/y;
             default :
                return 0;
    }
}
char Menu(void)
{
    char menu;
   for(;;)
       {
        for(int i=0;i<80;i++)
            putchar('*');
        printf("
Enter the number corresponding to the desired pay rate or action:
");
        printf("a) add                         s) subtract
");
        printf("m) multiply                    d) divide
");
        printf("q) quit
");
        for(int i=0;i<80;i++)
            putchar('*');
        putchar('
');
        menu=getchar();
        switch(menu)
        {
            case 'a':
               return '+';
            case 'm':
               return '*';
            case 's':
               return '-';
            case 'd':
               return '/';
            case 'q':
               return 0;
            default:
                {
                    printf("Enter Again!
");
                    while(getchar()!='
')continue;
                }
       }
       }
}

double input(void)
{
    char ch;
    double x;
    while(scanf("%lf", &x)==0)
        {
            while((ch=getchar)!='
')
            {
                putchar(ch);
            }
            printf(" is not an number.
");
            printf("Please enter a number:");
        }
    return x;
}

原文地址:https://www.cnblogs.com/WALLACE-S-BOOK/p/9732324.html