Pagini recente » Cod sursa (job #1444223) | Cod sursa (job #320890) | Cod sursa (job #266687) | Statisticile problemei Carti | Cod sursa (job #561668)
Cod sursa(job #561668)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nod
{
int data;
struct nod * link;
} nod;
void init (nod **st)
{
(*st)=NULL;
}
void push (nod **st,int s)
{
nod *p=(nod*) malloc (sizeof (nod));
p->data=s;
p->link=*st;
*st=p;
}
void pop (nod **st,int *rez)
{
if (*st==0) *rez=2000000006;
else
{
nod *p=*st;
*rez=p->data;
(*st)=(*st)->link;
}
}
int ord (int oper)
{
if (oper==2000000000||oper==2000000001) return 1;
if (oper==2000000002||oper==2000000003) return 2;
return 0;
}
int extr (char **s,int *rez)
{
if ((*s)[0]=='(')
{
*rez=2000000004;
(*s)++;
return 1;
}
if ((*s)[0]==')')
{
*rez=2000000005;
(*s)++;
return 2;
}
if ((*s)[0]=='+'||(*s)[0]=='-'||(*s)[0]=='*'||(*s)[0]=='/')
{
if ((*s)[0]=='+') *rez=2000000000;
if ((*s)[0]=='-') *rez=2000000001;
if ((*s)[0]=='*') *rez=2000000002;
if ((*s)[0]=='/') *rez=2000000003;
(*s)++;
return 3;
}
if ((*s)[0]>='0'&&(*s)[0]<='9')
{
int k=0;
*rez=0;
while ((*s)[k]>='0'&&(*s)[k]<='9')
{
*rez=10*(*rez)+(*s)[k]-'0';
k++;
}
(*s)+=k;
return 4;
}
*rez=0;
return 0;
}
int calcul (int op1,int op2,int op)
{
if (op==2000000000) return (op1+op2);
if (op==2000000001) return (op1-op2);
if (op==2000000002) return (op1*op2);
if (op==2000000003) return (op1/op2);
}
int main ()
{
FILE *in,*out;
char *s=(char*) malloc (100010);
int rez,rez2,rez3,i;
nod *st1,*st2;
init (&st1); init (&st2);
in=fopen ("evaluare.in","r");
out=fopen ("evaluare.out","w");
fgets (s,100010,in);
do
{
i=extr (&s,&rez);
switch (i)
{
case 1:
push (&st1,rez);
break;
case 2:
while (1)
{
pop (&st1,&rez);
if (rez!=2000000004) push (&st2,rez);
else break;
}
break;
case 3:
while (1)
{
pop (&st1,&rez2);
if (ord (rez2)>=ord (rez)) push (&st2,rez2);
else {
if (rez2!=2000000006) push (&st1,rez2);
break;
}
}
push (&st1,rez);
break;
case 4:
push (&st2,rez);
break;
}
}
while (i);
while (st1)
{
pop (&st1,&rez);
push (&st2,rez);
}
init (&st1);
while (st2)
{
pop (&st2,&rez);
push (&st1,rez);
}
init (&st2);
while (st1)
{
pop (&st1,&rez);
if (rez<2000000000) push (&st2,rez);
else
{
pop (&st2,&rez2);
pop (&st2,&rez3);
push (&st2,calcul (rez3,rez2,rez));
}
}
pop (&st2,&rez);
fprintf (out,"%d\n",rez);
fclose (in); fclose (out);
return 0;
}