Pagini recente » Cod sursa (job #1549029) | Cod sursa (job #203318) | Cod sursa (job #122357) | Cod sursa (job #715714) | Cod sursa (job #1704928)
#include<iostream>
#include<fstream>
#include<stack>
#include<vector>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char x;
int cnt=65,nr,k,i,p,y,nst[100011],v[1000];
bool ok;
stack<char>st;
vector<char>fp;
int prioritate(char c)
{
if(c=='(' || c==')')
return 0;
if(c=='-' || c=='+')
return 1;
else
return 2;
}
bool cifra(char x)
{
if(x-48>=0 && x-48<=9) return 1;
else return 0;
}
int main()
{
st.push('(');
f>>x;
while(!f.eof())
{
nr=0;
ok=0;
if(cifra(x))
{
ok=1;
while(cifra(x) && !f.eof())
{
nr=nr*10+(x-48);
f>>x;
}
fp.push_back(char(cnt));
v[cnt-'A']=nr;
cnt++;
}
else
{
if(x=='(')
st.push(x);
else if(x==')')
{
while(st.top()!='(' && !st.empty())
{
fp.push_back(st.top());
st.pop();
}
st.pop();
}
else
{
while(prioritate(x)<prioritate(st.top()) &&!st.empty())
{
fp.push_back(st.top());
st.pop();
}
st.push(x);
}
}
if(!ok)
f>>x;
}
while(!st.empty())
{
if(st.top()!=')' && st.top()!='(')
fp.push_back(st.top());
st.pop();
}
//for(int i=0; i<fp.size(); i++)
// g<<fp[i];
for(i=0; i<fp.size(); i++)
{
if(fp[i]>=65)
{
p++;
nst[p]=v[fp[i]-'A'];
}
else
switch (fp[i])
{
case '-':
nst[p-1]=nst[p-1]-nst[p];
p--;
break;
case '+':
nst[p-1]=nst[p-1]+nst[p];
p--;
break;
case '*':
nst[p-1]=nst[p-1]*nst[p];
p--;
break;
case '/':
nst[p-1]=nst[p-1]/nst[p];
p--;
break;
}
}
g<<nst[1];
}