Pagini recente » Cod sursa (job #699207) | Cod sursa (job #860763) | Cod sursa (job #708021) | Cod sursa (job #1573510) | Cod sursa (job #1989641)
#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 a = 0, b = 0, c = 0, d = 0;
i=0;
while(mystack.size()!=1 && i>=k) {
while(polo[i] != 47 && polo[i] != 42 && polo[i] != 43 && polo[i] != 45 && i <= k) {
i++;
mystack.push(polo[i]);
}
a = mystack.top();
mystack.pop();
b = mystack.top();
mystack.pop();
c = mystack.top();
mystack.pop();
d = Operation(c,b,a);
mystack.push(d);
}*/
}
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;
}
}