| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 635 人关注过本帖
标题:求高手大大指点迷津!!!!if语句的问题,里面嵌switch语句
取消只看楼主 加入收藏
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
求高手大大指点迷津!!!!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=='/'))
这两个条件不是一样的么?
求高手大大指点迷津!!!!
搜索更多相关主题的帖子: include 表达式 
2011-09-17 11:16
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
收藏
得分:0 
我现在贴的是正确的,但换成下面的就不行了
2011-09-17 13:59
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
收藏
得分:0 
回复 2楼 xg5699
我写在上面的是正确的, 改成下面的就出错了
2011-09-17 15:44
csbeng001
Rank: 1
来 自:湖南长沙市
等 级:新手上路
帖 子:15
专家分:0
注 册:2011-9-13
收藏
得分:0 
回复 5楼 xg5699
我估计可能是软件的原因,我用的是VC6.0
2011-09-22 09:45
快速回复:求高手大大指点迷津!!!!if语句的问题,里面嵌switch语句
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028546 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved