求高手大大指点迷津!!!!if语句的问题,里面嵌switch语句
#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=='/'))
这两个条件不是一样的么?
求高手大大指点迷津!!!!