UploadFiles/2006-7/731720792.rar
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Expression
{
private:
string exp;
string left;
string right;
char op;
string rest;
public:
Expression(){}
Expression(string expStr)
{
ereaseSpace(expStr);
exp = expStr;
if(beginWithLeftParentheses(exp))
{
int leftParenthesesCounter = 0;
int rightParenthesesCounter = 0;
int i = 0;
for(; i<exp.size(); i++)
{
char c = exp.at(i);
if(c == '(')
leftParenthesesCounter++;
else if(c == ')')
rightParenthesesCounter++;
if(rightParenthesesCounter == leftParenthesesCounter)
break;
}
left = string(exp, 1, i-1);
expStr.erase(0, i+1);
if(expStr == "")
{
*this = Expression(left);
}
else
{
string temp;
doubleToString(temp, Expression(left).doCalculation());
*this = Expression(temp + expStr);
}
}
else
{
int min = setOp(exp);
if(op == '+')
{
left = string(exp, 0, min);
string tempRest = string(exp, min+1, exp.size());
int opPos;
char opLocal;
computeOp(tempRest, opLocal, opPos);
if(opPos == -1)
{
right = tempRest;
rest = "";
}
else if(opLocal == '+' || opLocal == '-')
{
right = string(tempRest, 0, opPos);
rest = string(tempRest, opPos, tempRest.size());
}
else
{
right = "";
rest = tempRest;
}
}
else if(op == '-' && min == 0)
{
string tempRest = string(exp, min+1, exp.size());
if(beginWithLeftParentheses(tempRest))
{
int leftParenthesesCounter = 0;
int rightParenthesesCounter = 0;
int i = 0;
for(; i<tempRest.size(); i++)
{
char c = tempRest.at(i);
if(c == '(')
leftParenthesesCounter++;
else if(c == ')')
rightParenthesesCounter++;
if(rightParenthesesCounter == leftParenthesesCounter)
break;
}
string temp = string(tempRest, 1, i-1);
double value = 0 - Expression(temp).doCalculation();
&nbs