Pagini recente » Cod sursa (job #415196) | Cod sursa (job #2300825) | serj | Cod sursa (job #1809232) | Cod sursa (job #1972020)
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("fp.in");
ofstream fout("fp.out");
int vfop;
struct nod{
int nr=0;
char oper;
bool o=0;
nod * urm;
};
char op[100002],v[100002];
nod *prim,*curent,*nou;
bool prioritate(char s)
{
switch (s)
{
case '+' : return 0;
case '-' : return 0;
case '*' : return 1;
case '/' : return 1;
}
}
int main()
{
fin >> v;
int l=strlen(v);
bool p=1;
curent = new nod;
prim=curent;
for (int i=0;i<l;i++)
if (strchr("1234567890",v[i]))
{
p=0;
curent->nr=curent->nr*10+v[i]-'0';
}
else
{
if (p==0)
{
curent->o=1;
p=1;
nou=new nod;
curent->urm=nou;
curent=nou;
}
if (v[i]==')')
{
while (op[vfop]!='(' && vfop>0)
{
curent->oper=op[vfop--];
nou=new nod;
curent->urm=nou;
curent=nou;
}
vfop--;
}
else
if (prioritate(op[vfop]) >= prioritate(v[i]) && vfop > 0 && op[vfop] != '(')
{
while (prioritate(op[vfop]) >= prioritate(v[i]) && vfop > 0 && op[vfop]!= '(')
{
curent->oper=op[vfop--];
nou=new nod;
curent->urm=nou;
curent=nou;
}
op[++vfop]=v[i];
}
else op[++vfop]=v[i];
}
if (!p)
{
nou=new nod;
curent->o=1;
curent->urm=nou;
curent=curent->urm;
}
while (vfop)
{
curent->oper=op[vfop--];
nou=new nod;
curent->urm=nou;
curent=nou;
}
curent->urm=NULL;
while(prim->urm->urm)
{
curent=prim;
while (curent->urm)
{
while (curent->urm->o && !curent->urm->urm->o)
{
int numar=curent->urm->nr;
switch(curent->urm->urm->oper)
{
case '+' : {curent->nr+=numar;break;}
case '-' : {curent->nr-=numar;break;}
case '/' : {curent->nr/=numar;break;}
case '*' : {curent->nr*=numar;break;}
}
delete curent->urm;
delete curent->urm->urm;
curent->urm=curent->urm->urm->urm;
}
curent=curent->urm;
}
}
curent=prim;
while (curent->urm)
{
if (curent->o)fout << curent->nr << " ";
else fout << curent->oper << " ";
curent=curent->urm;
}
return 0;
}