注册 登录
编程论坛 C++教室

求高手大大指点迷津!!!!if语句的问题,里面嵌switch语句

csbeng001 发布于 2011-09-17 11:16, 635 次点击
#include "iostream.h"
#include "stdlib.h"
int main()
{
    float a,b,c,temp;
    char oper1,oper2;
    cout <<"请输入一个表达式(eg.1+2*3):" <<endl;
    cin >>a >>oper1 >>b >>oper2 >>c;
    if ((oper1!='*' &&oper1!='/') && (oper2=='*'||oper2=='/'))
    {
        switch (oper2)
        {
        case '*':
            {
                temp=b*c;
                break;
            }
        case '/':
            {
                if (c!=0) temp=b/c;
                else
                {
                    cout <<"出错啦!"<<endl;
                    exit(1);
                }
                break;
            }
        default:
            cout <<"出错啦!"<<endl;
            exit(1);
        }
        switch (oper1)
            {
            case '+':
                {
                    temp=a+temp;
                    break;
                }
            case '-':
                {
                    temp=a-temp;
                    break;
                }
            default:
                cout <<"出错啦!"<<endl;
                exit(1);
            }
    }
    else
    {
            switch (oper1)
            {
            case '+':
                {
                    temp=a+b;
                    break;
                }
            case '-':
                {
                    temp=a-b;
                    break;
                }
            case '*':
                {
                    temp=a*b;
                    break;
                }
            case '/':
                {
                    if (b!=0) temp=a/b;
                    else
                    {
                        cout <<"出错啦!"<<endl;
                        exit(1);
                    }
                    break;
                }
            default:
                cout <<"出错啦!"<<endl;
                exit(1);
            }
            switch (oper2)
            {
            case '+':
                {
                    temp=temp+c;
                    break;
                }
            case '-':
                {
                    temp=temp-c;
                    break;
                }
            case '*':
                {
                    temp=temp*c;
                    break;
                }
            case '/':
                {
                    if (c!=0) temp=temp/c;
                    else
                    {
                        cout <<"出错啦!"<<endl;
                        exit(1);
                    }
                    break;
                }
            default:
                cout <<"出错啦!"<<endl;
                exit(1);
            }
    }
    cout <<a <<oper1 <<b <<oper2 <<c <<'=' <<temp <<endl;
return 0;
}

注意红色部分,为什么换成if ((oper2=='*' || oper2=='/') && (oper1!='*' && oper1!='/'))后,
运算a*b*c,或a/b/c时,结果是"出错了!"呢?
if ((oper2=='*' || oper2=='/') && (oper1!='*' && oper1!='/'))
 if ((oper1!='*' &&oper1!='/') && (oper2=='*'||oper2=='/'))
这两个条件不是一样的么?
求高手大大指点迷津!!!!
6 回复
#2
xg56992011-09-17 12:17
回复 楼主 csbeng001
我怎么显示是正常的!并没有出错!
只有本站会员才能查看附件,请 登录


只有本站会员才能查看附件,请 登录

不信自己看图

[ 本帖最后由 xg5699 于 2011-9-17 12:24 编辑 ]
#3
csbeng0012011-09-17 13:59
我现在贴的是正确的,但换成下面的就不行了
#4
csbeng0012011-09-17 15:44
回复 2楼 xg5699
我写在上面的是正确的, 改成下面的就出错了
#5
xg56992011-09-17 15:54
回复 4楼 csbeng001
那就奇怪了,你看见我2张图没?不管是if ((oper2=='*' || oper2=='/') && (oper1!='*' && oper1!='/'))
还是  if ((oper1!='*' &&oper1!='/') && (oper2=='*'||oper2=='/'))
都不会出错
#6
csbeng0012011-09-22 09:45
回复 5楼 xg5699
我估计可能是软件的原因,我用的是VC6.0
#7
S23927358182011-09-22 09:59
1