Pagini recente » Cod sursa (job #1546240) | Cod sursa (job #237724) | Cod sursa (job #1557627) | Cod sursa (job #2944967) | Cod sursa (job #156449)
Cod sursa(job #156449)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
FILE*f=fopen("evaluare.in","r");
FILE*g=fopen("evaluare.out","w");
char s[100000][11];
char st[100000];
char rez[100000][11];
int n; //numarul de linii ale lui s[][]
void read()
{
char x,y;
int p;
y='!';
p=0;
while(!feof(f))
{
fscanf(f,"%c",&x);
if(x!='\n') {++n;
if(isdigit(y) && isdigit(x)) { n--;++p;}
else p=0;
s[n][p]=x;
y=x; }
}
}
void poloneza()
{
int i,k,vf=0;
st[0]='!';
k=0;
for(i=1;i<=n;++i)
{
if(s[i][0]=='+' || s[i][0]=='-')
{
while(st[vf]=='*' || st[vf]=='/' || st[vf]=='-' || st[vf]=='+')
{
rez[k++][0]=st[vf];
--vf;
}
st[++vf]=s[i][0];
}
else if(s[i][0]=='(') st[++vf]='(';
else if (s[i][0]==')')
{
//copiez in rez toti operanzii pana la ).
while(st[vf]!='(')
{
rez[k++][0]=st[vf];
--vf;
}
--vf; //sterg '(';
}
else
if (s[i][0]=='*' || s[i][0]=='/')
{
while(st[vf]=='*' || st[vf]=='/')
{
rez[k++][0]=st[vf];
--vf;
}
st[++vf]=s[i][0];
}
else
{
strcpy(rez[k++],s[i]);
}
}
while(st[vf]!='!')
{
rez[k++][0]=st[vf];
vf--;
}
n=k;
}
void eval()
{
int x,y,i,vf=-1;
char st[100000][11];
for(i=0;i<n;++i)
{
if(rez[i][0]=='+' || rez[i][0]=='-' || rez[i][0]=='*' || rez[i][0]=='/')
{
x=atoi(st[vf]);
--vf;
y=atoi(st[vf]);
switch(rez[i][0])
{
case '+': itoa(x+y,st[vf],10); break;
case '-': itoa(y-x,st[vf],10); break;
case '*': itoa(x*y,st[vf],10); break;
case '/': itoa(y/x,st[vf],10); break;
}
}
else
strcpy(st[++vf],rez[i]);
}
fprintf(g,"%s\n",st[0]);
}
int main()
{
read();
poloneza();
eval();
return 0;
}