Pagini recente » Cod sursa (job #553498) | Cod sursa (job #1056490) | Cod sursa (job #1083202) | Cod sursa (job #885296) | Cod sursa (job #677434)
Cod sursa(job #677434)
//marco
#include <string.h>
#include <stack>
#include <queue>
#include <string>
#include <stdio.h>
using namespace std;
int a[27];
queue<char>polo;
stack<char>rofl;
bool inline isnum(char x){
return x>='0' && x<='9';
}
int inline op(char x){
switch (x){
case '-':return 1;break;
case '+':return 1;break;
case '/':return 2;break;
case '*':return 2;break;
case '(':return 3;break;
}
}
bool inline canpush(char x){
if (rofl.empty())
return true;
if (rofl.top()=='(')
return true;
if (op(rofl.top())<op(x))
return true;
return false;
}
int inline operatie(int a,int b,char op){
switch (op){
case '-':return a-b;break;
case '+':return a+b;break;
case '/':return a/b;break;
case '*':return a*b;break;
}
}
int main(){
freopen("polo.in","r",stdin);
freopen("polo.out","w",stdout);
char x[100001]={0};
gets(x);
char *p=&x[0];
while (*p!=0){
if (isnum(p[0]))
polo.push(p[0]);
else{
if (p[0]!='(')
polo.push('|');
if (canpush(p[0])){
rofl.push(p[0]);
} else{
if (p[0]==')'){
while (rofl.top()!='('){
polo.push(rofl.top());
rofl.pop();
}
rofl.pop();
} else{
polo.push(rofl.top());
rofl.pop();
rofl.push(p[0]);
polo.push('|');
}
}
}
p++;
}
while (!rofl.empty()){
if (rofl.top()!='('){
polo.push('|');
polo.push(rofl.top());
}
rofl.pop();
}
stack<int> elemente;
int nr;
long solutie=0;
nr=0;
while (!polo.empty()){
char x=polo.front();
if (x=='|'){
if (nr!=0){
elemente.push(nr);
nr=0;
}
}
if (x!='|' && !isnum(x)){
int a1,a2;
a1=elemente.top();elemente.pop();
a2=elemente.top();elemente.pop();
elemente.push(operatie(a2,a1,x));
}
if (x!='|' && isnum(x)){
nr=nr*10+x-'0';
}
polo.pop();
}
printf("%d",elemente.top());
return 0;
}