#include "stdafx.h"
#include <cstdlib>
#include <cctype>
#include <iostream>
using namespace std;
int addsubt();
int multdiv();
int number();
void error();
static char expr[8];
static int pos;
int _tmain(int argc, _TCHAR* argv[])
{
int ans;
do
{
pos = 0;
cout<<"Enter Expression(0 quit):"<<endl;
cin>>expr;
ans = addsubt();
if (expr[pos] != '\0')
{
error();
}
if (ans != 0)
{
cout<<ans<<endl;
}
} while (ans !=0);
return 0;
}
int addsubt()
{
int rtn = multdiv();
while (expr[pos] == '+' ||expr[pos] == '-')
{
int po1 =expr[pos++];
int po2 = multdiv();
if(po1=='+')
rtn += po2;
else
rtn -= po2;
}
return rtn;
}
int multdiv()
{
int rtn = number();
while (expr[pos] == '*' ||expr[pos] == '/')
{
int po1 =expr[pos++];
int po2 = number();
if(po1=='*')
rtn *= po2;
else
rtn /= po2;
}
return rtn;
}
int number()
{
int rtn;
if (expr[pos] == '(')
{
pos++;
rtn = addsubt();
if (expr[pos++] != ')')
error();
return rtn;
}
if( !isdigit(expr[pos]))
error();
rtn = atoi(expr + pos);
while( isdigit(expr[pos]))
pos++;
return rtn;
}
void error()
{
cout<<'\r';
while(pos--)
cout<<' ';
cout<< "syntem error" <<endl<< '\a';
exit(-1);
}