#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <assert.h>
#include <conio.h>
int tok;
double tokval;
FILE *stream;
char s[15]="\0";
int next() {
for (;;) {
int c = fgetc(stream);
if (c == EOF || strchr("+-()\n", c) != NULL) return tok = c;
if (isspace(c)) continue;
if (isdigit(c) || c == '.') {
ungetc(c, stream);
// fgetc(tokval,stream);
fscanf(stream," %lf", &tokval);
return tok = 'n';
}
fprintf(stderr, "Bad character: %c\n", c); abort();
}
}
void skip(int t) { assert(tok == t); next(); }
double expr();
// numpar ::= number | '(' expr ')'
double numpar() {
if (tok == 'n') { double x = tokval; skip('n'); return x; }
skip('('); double x = expr(); skip(')'); return x;
}
// factor ::= numpar | numpar '^' factor
double factor() {
double x = numpar();
return x;
}
// term ::= factor | term '*' factor | term '/' factor
double term() {
double x = factor();
for (;;) {
return x;
}
}
// expr ::= term | expr '+' term | expr '-' term
double expr() {
double x = term();
for (;;) {
if (tok == '+') { skip('+'); x += term(); }
else if (tok == '-') { skip('-'); x -= term(); }
else return x;
}
}
int main() {
clrscr();
// stream=NULL;
if (( stream=fopen("input.in", "r"))==NULL)
{
printf ("There are an error\n");
}
else
{
next();
printf("RESULT=");
while (tok != EOF) {
if (tok == '\n') { skip('\n'); continue; }
printf("%.9g\n", expr());
} }
getch();
// printf("s=%s",s);
return 0;
}