Calculator(console application)

作者在 2006-07-30 09:16:00 发布以下内容

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

programming | 阅读 1732 次
文章评论,共1条
kai(作者)
2006-08-31 20:13
1
一不小心把老白的回复给删掉了, 实在有欠妥当, 在此赔礼了.
游客请输入验证码

kai
浏览94818次