Pagini recente » Cod sursa (job #1717620) | Cod sursa (job #2330441) | Cod sursa (job #127465) | Cod sursa (job #1317870) | Cod sursa (job #2791032)
#include <stdio.h>
#include <stdlib.h>
#define LMAX 100000
char ch[LMAX];
int indexx;
int adunscad();
int factor() {
int nr;
if( ch[indexx] == '(' ) {
indexx++;//trec peste paranteza deschisa
nr = adunscad();//incep o noua expresie
indexx++;//trec peste paranteza inchisa
}
else {
nr = 0;
while( ch[indexx] >= '0' && ch[indexx] <= '9' ) {
nr = nr * 10 + ch[indexx] - '0';
indexx++;
}
}
return nr;
}
int inmimp() {
int rez;
rez = factor();
while( ch[indexx] == '*' || ch[indexx] == '/' ) {
indexx++;
if( ch[indexx-1] == '*' )
rez *= factor();
else
rez /= factor();
}
return rez;
}
int adunscad() {
int rez;
rez = inmimp();
while( ch[indexx] == '+' || ch[indexx] == '-' ) {
indexx++;
if( ch[indexx-1] == '+' )
rez += inmimp();
else
rez -= inmimp();
}
return rez;
}
int main() {
FILE *fin, *fout;
fin = fopen( "evaluare.in", "r" );
fout = fopen( "evaluare.out", "w" );
indexx = 0;
ch[indexx] = fgetc( fin );
while( ch[indexx] != '\n' && ch[indexx] != EOF )
ch[++indexx] = fgetc( fin );
indexx = 0;
fprintf( fout, "%d", adunscad() );
fclose( fin );
fclose( fout );
return 0;
}