Pagini recente » Cod sursa (job #1540859) | Cod sursa (job #2156080) | Cod sursa (job #3166425) | Cod sursa (job #2885043) | Cod sursa (job #1989642)
#include <fstream>
#include <string>
using namespace std;
int polo[1000], op[20], pr[300];
string str;
int k=0, o=0;
void Solve();
int Operation(int a, int b, int c);
int main()
{
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
pr['+']=pr['-']=1;
pr['*']=pr['/']=2;
pr['(']=3;
cin>>str;
Solve();
for(int i=1;i<=k;i++)
cout<<polo[i]<<" ";
return 0;
}
void Solve() {
int i = 0, x = 0;
while(i <= str.size()) {
char ch = str [i];
if('0' <= ch && ch <= '9') {
int b = 0;
while(i<str.size() && '0' <= str[i] && str[i] <= '9') {
b = b*10 + str[i]-'0';
i++;
}
i--;
polo[++k] = b;
}
else if(ch == ')') {
while(op[o] != '(') {
polo[++k] = op[o--];
x = Operation(polo[k-2],polo[k-1],polo[k]);
k -= 2;
polo[k] = x;
}
o--;
}
else {
while(o > 0 && op[o] != '(' && pr[op[o]] >= pr[ch]) {
polo[++k] = op[o--];
x = Operation(polo[k-2],polo[k-1],polo[k]);
k -= 2;
polo[k] = x;
}
op[++o] = ch;
}
i++;
}
}
int Operation(int a, int b, int c) {
switch(c) {
case '+': return a+b;
case '-': return a-b;
case '/': return a/b;
case '*': return a*b;
}
}