#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
#include<mem.h>
//#include<alloc.h>
#include<string.h>
char token;
char buff[100];
int f=0;
#include<ctype.h>
#include<mem.h>
//#include<alloc.h>
#include<string.h>
char token;
char buff[100];
int f=0;
int c=0;
int n=0;
double exp(char *p,int flag);
double term(char *p);
double factor(char *p);
void match(char expectedToken);
int get();
void error();
tradeMark();
main(){
tradeMark();
while(1){double result;
int flag=1;
char ch;
int count=0;
f=0;
c=0;
printf("printf 'q' to ESC\n");
ch=getchar();
if(ch!='q'){
while(ch!='-'&&!isdigit(ch)&&ch!='('){
f=1;
count++;
if(count==4)
break;
error();
ch=getchar();
}
if(ch=='-'){
flag=-1;
buff[c++]=ch;
count=0;
while(get()){
f=1;
count++;
if(count==4)
break;
error();
}
}
else
token=ch;
buff[c++]=token;
if(f)
ch='q';
}
if(ch=='q'){
printf("EXPRESS ERROR!!!");
break;
}
ch='0';
if(flag==-1){
if(buff[c-1]=='(')
result=exp(&ch,1)*flag;
else
result=exp(&ch,-1);
}
else
result = exp(&ch,1);
if(f){
printf("EXPRESS ERROR!!!");
break;
}
if (token == '\n')
printf("Result is: %g\n", result);
}
}
int get(){
char temp;
temp=getchar();
if(temp=='('||isdigit(temp)){
token=temp;
return 0;
}
return 1;
}
double exp(char *p,int flag){
double temp ;
int count=0;
temp=term(p)*flag;
if(f)
exit(-1);
while (token == '+' || token == '-')
switch(token){
case '+':
while(get()){
count++;
f=1;
if(count==4)
exit(-1);
error();
}
buff[c++]=token;
temp += term(p);
if(f)
exit(-1);
break;
case '-':
while(get()){
count++;
f=1;
if(count==4)
exit(-1);
error();
}
buff[c++]=token;
temp -= term(p);
if(f)
exit(-1);
break;
}
return temp;
}
double term(char *p){
int count=0;
double temp = factor(p);
if(f)
exit(-1);
while (token == '*' || token == '/')
switch(token){
case '*':
while(get()){
count++;
f=1;
if(count==4)
exit(-1);
error();
}
buff[c++]=token;
temp *= factor(p);
if(f)
exit(-1);
break;
case '/':
while(get()){
count++;
f=1;
if(count==4)
exit(-1);
error();
}
buff[c++]=token;
temp/=factor(p);
if(f)
exit(-1);
break;
}
return temp;
}
double factor(char *p){
char ch;
char *sym,symbol;
double temp;
int count=0,flag=1;
symbol='0'; // symbol=(char *)malloc(1);*symbol='0';
sym=&symbol;
if(token == '('){
n++;
symbol='(';
ch=getchar();
while(ch!='-'&&!isdigit(ch)&&ch!='('){
f=1;
if(count==4)
exit(-1);
error();
ch=getchar();
}
if(ch=='-'){
flag=-1;
buff[c++]=ch;
count=0;
while(get()){
f=1;
count++;
if(count==4)
exit(-1);
error();
}
}
else
token=ch;
buff[c++]=token;
if(flag==-1){
if(buff[c-1]=='(')
temp=exp(sym,1)*flag;
else
temp=exp(sym,-1);
}
else
temp = exp(sym,1);
match(')',p);
if(f)
exit(-1);
}
else if (isdigit(token)){
ungetc(token, stdin);
scanf("%lf", &temp);
gcvt(temp,10,&buff[c-1]);
c=strlen(buff);
token=getchar();
if(token==')'&&*p=='('){
buff[c++]=token;
return temp;
}
while(token!='+'&&token!='-'&&token!='*'&&token!='/'&&token!='\n'){
f=1;
count++;
if(count==4)
exit(-1);
error(-1);
token=getchar();
if(token==')'&&*p=='(')
break;
}
buff[c++]=token;
}
return temp;
}
void match(char expectedToken){
int count=0;
if (expectedToken == token){
n--;
token=getchar();
while(token!='+'&&token!='-'&&token!='*'&&token!='/'&&token!='\n'&&token==')'){
if(token==')'&&n){
n--;
break;
}
f=1;
count++;
if(count==4)
exit(-1);
error();
token=getchar();
}
buff[c++]=token;
}
else {
c=c-1;
while(token!=')'){
f=1;
count++;
if(count==4)
exit(-1);
error();
token=getchar();
}
buff[c++]=token;
match(token);
}
}
void error(){
int i;
f=0;
fflush(stdin);
printf("\nEXPRESS ERROR!!!\n");
for(i=0;i<c;i++)
printf("%c",buff[i]);
}
tradeMark(){
printf("***************************************\n");
printf("* *\n");
printf("* 兵主出品 *\n");
printf("* *\n");
printf("****************************************\n");
}