美文网首页
逆波兰计算器

逆波兰计算器

作者: 编码的哲哲 | 来源:发表于2016-12-18 22:54 被阅读65次

    include<iostream>

    include<vector>

    include<stack>

    using namespace std;

    int main(){

    //转换后的数据 
    vector<char> transData;
    //临时栈
    stack<char> tempData; 
    
    //将输入的数据依次读入然后处理放到一个数组里面
    
    char temp;
    cin>>temp;
    
    while( temp != '#'){
        
        while( temp>='0' && temp<='9'){
            transData.push_back(temp);
            cin>>temp;          
            if(temp<'0' || temp>'9'){
                transData.push_back(' ');
                break;
            }
        }
        
        if(temp == ')'){
            
            while(tempData.top() != '('){
                
                transData.push_back(tempData.top());
                tempData.pop();
            }
            
            tempData.pop();
        }
        
        else if(temp == '+' || temp == '-'){
            
            while(tempData.size()&&tempData.top() != '('){
                transData.push_back(tempData.top());
                tempData.pop();
            }
            
            tempData.push(temp);
            
        }
        
        else if(temp == '*' || temp == '/' || temp == '('){
            
            tempData.push(temp);
        }
        
        else if(temp == '#'){
            
            break;
        }
        
        else {
            
            cout<<"输入有误!!!"<<endl; 
        }
        
        cin>>temp;
                
    } 
    
    while(tempData.size()){
        
        transData.push_back(tempData.top());
        tempData.pop();
    }
    
    //cout<<"输出转换后的数据:"<<endl;
    

    //接下来计算
    stack<int> result;
    int temp1 = 0,temp2 = 0;;
    int flag = 1;
    int j = 0;

    for(int i=0; i<transData.size();){

        //cout<<transData[i];
        
        if(transData[i]>='0' && transData[i]<='9'){
            
            while(transData[i+j]>='0'&& transData[i]<='9'){
                
                temp1 += (transData[i+j] - '0')*flag;
                flag *= 10;
                j++;                
            }
            
            result.push(temp1);
            i = i+j;
            j=0;
            flag = 1;
            temp1 = 0;
            
            continue;
            
            
       }else if(transData[i]!=' '){
            
            switch(transData[i]){
                
                case'+' : temp1 = result.top();
                          result.pop();
                          temp2 = result.top();
                          result.pop();
                          result.push(temp1+temp2);
                          temp1=0;
                          temp2=0; 
                          break;
                case'-' : temp1 = result.top();
                          result.pop();
                          temp2 = result.top();
                          result.pop();
                          result.push(temp2-temp1);
                          temp1=0;
                          temp2=0;                            
                          break;
                case'*' : temp1 = result.top();
                          result.pop();
                          temp2 = result.top();
                          result.pop();
                          result.push(temp1*temp2);
                          temp1=0;
                          temp2=0; 
                          break;
                case'/' : temp1 = result.top();
                          result.pop();
                          temp2 = result.top();
                          result.pop();
                          result.push(temp2/temp1);
                          temp1=0;
                          temp2=0; 
                          break;
            }
            
        }   
        
        i++;
    

    }

    cout<<"计算结果是:"<<endl;
    cout<<result.top(); 
    
    return 0;
    

    }

    相关文章

      网友评论

          本文标题:逆波兰计算器

          本文链接:https://www.haomeiwen.com/subject/yymzmttx.html