Pagini recente » Cod sursa (job #2583348) | Cod sursa (job #496027) | Cod sursa (job #2875697) | Cod sursa (job #319686) | Cod sursa (job #3241817)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
const int szmax = 100000;
char s[szmax + 5];
int pr[300];
vector <int> st_nr;
vector <char> st_op;
int get_number(int& pos)
{
int nr = 0;
while(isdigit(s[pos]))
nr=nr*10 + s[pos++]-'0';
return nr;
}
void resolve_operation()
{
int b=st_nr.back();
st_nr.pop_back();
int a = st_nr.back();
st_nr.pop_back();
if(st_op.back()=='+')
st_nr.push_back(a+b);
else if(st_op.back()=='-')
st_nr.push_back(a-b);
else if(st_op.back()=='*')
st_nr.push_back(a*b);
else if(st_op.back()=='/')
st_nr.push_back(a/b);
st_op.pop_back();
}
int main()
{
pr['+']=1;
pr['-']=1;
pr['*']=2;
pr['/']=2;
fin>>s;
int ind = 0;
while(s[ind])
{
if(s[ind]=='(')
{
st_op.push_back('(');
++ind;
continue;
}
if(s[ind]==')')
{
while(st_op.back()!='(')
resolve_operation();
st_op.pop_back();
++ind;
continue;
}
if(isdigit(s[ind]))
{
st_nr.push_back(get_number(ind));
continue;
}
else
{
while(st_op.empty()==false && pr[st_op.back()]>=pr[s[ind]])
resolve_operation();
st_op.push_back(s[ind]);
++ind;
continue;
}
}
while(!st_op.empty())
resolve_operation();
fout<<st_nr.front();
}