Pagini recente » Cod sursa (job #268619) | Cod sursa (job #1442144) | Cod sursa (job #210342) | Cod sursa (job #1752078) | Cod sursa (job #1751071)
#include <iostream>
#include<stack>
#include<string.h>
using namespace std;
stack<char> operators;
stack<int> factors;
void afiseaza(stack<int> v){
while(!v.empty()){
int a=v.top();
v.pop();
cout<<a<<" ";
}
cout<<endl;
}
void solve_op_prcedence(){
while(operators.top()=='*'||operators.top()=='/'){
char op=operators.top();
operators.pop();
int f1=factors.top();
factors.pop();
int f2=factors.top();
factors.pop();
switch(op){
case '*':factors.push(f1*f2);break;
case '/':factors.push(f1/f2);
}
}
}
void solve_op_bracket(){
while(operators.top()!='('){
char op=operators.top();
operators.pop();
int f1=factors.top();
factors.pop();
int f2=factors.top();
factors.pop();
switch(op){
case '*':factors.push(f1*f2);break;
case '/':factors.push(f1/f2);break;
case '+':factors.push(f1+f2);break;
case '-':factors.push(f1-f2);
}
}
operators.pop();
}
void solve_rest(){
while(!operators.empty()){
char op=operators.top();
operators.pop();
int f1=factors.top();
factors.pop();
int f2=factors.top();
factors.pop();
switch(op){
case '*':factors.push(f1*f2);break;
case '/':factors.push(f1/f2);break;
case '+':factors.push(f1+f2);break;
case '-':factors.push(f1-f2);
}
}
}
int main() {
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
char* w=(char*)malloc(20000);
cin>>w;
for(int i=0;i<strlen(w);i++){
switch(w[i]){
case '*':operators.push(w[i]);break;
case '/':operators.push(w[i]);break;
case '+':
if(operators.top()=='*'||operators.top()=='/')solve_op_prcedence();
operators.push(w[i]);break;
case '-':
if(operators.top()=='*'||operators.top()=='/')solve_op_prcedence();
operators.push(w[i]);break;
case '(':operators.push(w[i]);break;
case ')':
solve_op_bracket();break;
default:factors.push(w[i]-'0');
}
}
solve_rest();
afiseaza(factors);
}