逆波兰计算器

include

include

include

using namespace std;

int main(){

//转换后的数据 
vector transData;
//临时栈
stack 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<<"输入有误!!!"<>temp;
            
} 

while(tempData.size()){
    
    transData.push_back(tempData.top());
    tempData.pop();
}

//cout<<"输出转换后的数据:"<

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

for(int i=0; i

    //cout<='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<<"计算结果是:"<

}

你可能感兴趣的:(逆波兰计算器)