Pagini recente » Cod sursa (job #653742) | Cod sursa (job #1937528) | Cod sursa (job #312047) | Cod sursa (job #2928844) | Cod sursa (job #1517604)
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
#include<stack>
#define DM 100004
using namespace std;
fstream fin("evaluare.in",ios::in),fout("evaluare.out",ios::out);
string in;
stack<char> st;
stack<int> nr;
char p[100];
void numar(int&i)
{
int aux=0;
while(isdigit(in[i]))
{
aux=aux*10+(in[i]-'0');
i++;
}
nr.push(aux);
}
void evaluare(char semn)
{
int a,b;
a=nr.top();
nr.pop();
b=nr.top();
nr.pop();
if(semn=='+')nr.push(b+a);
if(semn=='-')nr.push(b-a);
if(semn=='*')nr.push(b*a);
if(semn=='/')nr.push(b/a);
}
int main()
{
int i,j;
fin>>in;
p[')']=0;
p['(']=0;
p['-']=1;
p['+']=1;
p['*']=2;
p['/']=2;
for(i=0;i<in.size();i++)
{
if(in[i]==')')
{
while(st.top()!='(')
{
evaluare(st.top());
st.pop();
}
st.pop();
}
else
{
if(isdigit(in[i]))
{
numar(i);
i--;
}
else
{
while(!st.empty()&&p[st.top()]>p[in[i]])
{
evaluare(st.top());
st.pop();
}
st.push(in[i]);
}
}
}
while(!st.empty())
{
evaluare(st.top());
st.pop();
}
fout<<nr.top();
return 0;
}