Pagini recente » Cod sursa (job #1125365) | Cod sursa (job #1939059) | Cod sursa (job #2723896) | Cod sursa (job #248195) | Cod sursa (job #1903119)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int prioritate(char c)
{
if(c=='('||c==')')return 0;
else
if(c=='+'||c=='-')return 1;
else
if(c=='*'||c=='/')return 2;
}
int main()
{
char E[100003],FP[100003],S[100000], aux[4],sirnr[20],*p;
int i,vf,nr,semn,S1[1],R;
FILE *f,*g;
f=fopen("evaluare.in","r");
g=fopen("evaluare.out","w");
fgets(E,100000,f);
puts(E);
E[strlen(E)-1]=NULL;
strcpy(FP,"(");
strcat(FP,E);
strcat(FP,")");
strcpy(E,FP);
puts(E);
strcpy(FP,"");
vf=0;
for(i=0;E[i]!=NULL;i++)
{
if(E[i]>='0' && E[i]<='9')
{
nr=0;
if (E[i-1]=='-' && E[i-2]=='('&&i-2>=0) semn=-1;
else semn=1;
while(E[i]>='0' && E[i]<='9')
{
nr=nr*10+E[i]-48;
i++;
}
nr=nr*semn;
itoa(nr,sirnr,10);
strcat(FP,sirnr);
strcat(FP," ");
i--;
puts(FP);
}
else
if(E[i]=='+'||(E[i]=='-' &&E[i-1]!='(')||E[i]=='*'||E[i]=='/')
{
while(prioritate(S[vf])>=prioritate(E[i]))
{
aux[0]=S[vf];
aux[1]=' ';
aux[2]=NULL;
strcat(FP,aux);
vf--;
}
vf++;
S[vf]=E[i];
}
else
if(E[i]=='(')
{
vf++;
S[vf]=E[i];
}
else
if(E[i]==')')
{
while(S[vf]!='(')
{
aux[0]=S[vf];
aux[1]=' ';
aux[2]=NULL;
strcat(FP,aux);
vf--;
}
vf--;
}
for(int j=1;j<=vf;j++)
printf("%c ",S[j]);
printf("\n");
printf("%d ",i);
}
puts(FP);
p=strtok(FP," ");
vf=0;
while(p!=NULL)
{
if(*p=='+' || (*p=='-' && *(p+1)==NULL) ||*p=='*' || *p=='/')
{
if(*p=='+') R=S1[vf]+S1[vf-1];
if(*p=='-') R=S1[vf-1]-S1[vf];
if(*p=='*') R=S1[vf]*S1[vf-1];
if(*p=='/') R=S1[vf-1]/S1[vf];
vf--;
S1[vf]=R;
}
else
{
vf++;
S1[vf]=atoi(p);
}
p=strtok(NULL," ");
}
fprintf(g,"%d",S1[1]);
fclose(f);
fclose(g);
return 0;
}