当前位置: 代码迷 >> C语言 >> 出一道题目让大家做做
  详细解决方案

出一道题目让大家做做

热度:86   发布时间:2007-12-31 13:11:39.0
无聊得从写了个,写的不好,
修正了函数中的对除数的判断。

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

char str[] = "请输入:数字、运算符(+-*/)和数字的组合,并且在一行内!";

int fun(double a, double b, char ch, double * z);

int main(void)
{
    double a, b, c;
    int ch;
    int k;

    puts(str);
   
    do
    {
        k = 0;
        k += scanf("%lf", &a);
        while( (ch = getchar()) == ' ')
            continue;

        ch = tolower(ch);
        if(ch != 'q')
        {
            if(ch != '\n')
            {
                k += scanf("%lf", &b);
                while(getchar() != '\n')
                    continue;
            }
            (k == 2 && fun(a, b, ch, &c) ) ? printf("   = %g\n\n", c)
                                           : puts("无效输入!\n");
        }
        else
            while(getchar() != '\n')
                continue;

    }while(ch != 'q');
   
    puts("Bye!");
    system("pause");
    return 0;
}

int fun(double a, double b, char ch, double * z)
{
    switch(ch)
    {
        case '+':
            *z = a + b;
            break;
        case '-':
            *z = a - b;
            break;
        case '*':
            *z = a * b;
            break;
        case '/':
            if(b)     // 如果b不等于0
                *z = a / b;
            else
            {
                puts("除数不能为0!");
                return 0;
            }
        default :
            return 0;
    }
    return 1;
}

----------------解决方案--------------------------------------------------------
现在客户的需求变了,要求输入全部是整数就输出整数 ,如果当中有一个是浮点数,那么结果就要输入浮点数.
----------------解决方案--------------------------------------------------------
乍地没人接着做了?
----------------解决方案--------------------------------------------------------
  相关解决方案