2017-04-25 94 views
-2

我正在構建一個使用多態性及其遞歸的「簡單」計算器。當試圖返回一個不可簡化的表達式時,我有點卡住了。如何返回非簡化表達式

如果我得到一個輸入3/2,我應該如何返回它?我想返回的是一個字符串,但它可能會弄亂所有其他的方法和功能。

我的程序,正如我所說的,使我們使用虛擬功能。我也附上我的「主」功能,創建表達類型,並最終解決它們

在此先感謝!

#include "Expression.h" 
#include "nThRoot.h" 
using namespace std; 

#include "tgmath.h" 

nThRoot::nThRoot(Expression *l, Expression* r) { 
    leftSide = l; 
    rightSide = r; 



} 

bool nThRoot::is_nth_power(int a, int n) { 
    if(n <= 0) 
     return false; 
    if(a < 0 && n % 2 == 0) 
     return false; 
    a = abs(a); 

    int b = pow(a, 1./n); 
    return pow((double) b, n) == a || pow((double) (b+1), n) == a; 

    } 
int nThRoot::getValue() { 


    try { 
     if (leftSide->getValue() == 0) { 

      throw overflow_error("Cannot take the 0th Root, Please enter a valid Expression"); 
     } 
    } 
    catch (overflow_error e) { 
     //cout << "Error Happened, divided by 0"<< endl; 
     cerr << e.what(); 
    } 
    cout << "Bool is nth power is " << is_nth_power(rightSide->getValue(), leftSide->getValue()) <<endl; 

    if (is_nth_power(rightSide->getValue(), leftSide->getValue())) { 

     int returnPow = (int)(pow(rightSide->getValue(), (1.0/leftSide->getValue()))); 
     return returnPow; 

    } 
    else { 
     simplify(); 
    } 




} 
Expression* nThRoot::getLeftSide() { 
    return leftSide; 
} 
Expression* nThRoot::getRightSide() { 
    return rightSide; 
} 
vector<Expression*> nThRoot::getNumeratorFactors() { 

} 
vector<Expression*> nThRoot::getDenominatorFactors() { 

} 
vector<Expression*> nThRoot::getAdditiveTerms(){ 

} 
Expression* nThRoot::simplify() { 

     cout << "not solvable" <<endl; 
    //return the input 
     Expression* expr = new nThRoot(leftSide,rightSide); 
     return expr; 

} 

這裏是我的PostUtilies(這有點像我的主)

// 
// Created by Eyal on 4/17/17. 
// 
#include "Addition.h" 
#include "Subtraction.h" 


#include "PostFixUtilities.h" 
//#include "ProjectCOP.h" 
//#include "Expression.h" 
#include <ctype.h> 

//#include <stack> 



#include "Multiplication.h" 
#include "Division.h" 

#include "Exponential.h" 
#include "nThRoot.h" 
#include "Logarithmic.h" 

#include "Integer.h" 
#include "NumericalMenu.h" 
#include <cmath> 

#include <iostream> 

using namespace std; 
ProjectCOP algorithm; 
NumericalMenu expression; 




bool isParam(string line) 
{ 
    char* p; 
    strtol(line.c_str(), &p, 10); 
    return *p == 0; 
} 

PostFixUtilities::PostFixUtilities(vector<string> s) { 

    if (numberofRuns > 0) { 
     cout << "numberfRunsComplete" <<endl; 
     cout << "first top of ansstack is " << expression.ansStack.top()->getValue() <<endl; 

    } 


    Expression* result; 

     //string first = s.top().c_str(); 
     //s.pop(); 
     //bool firstInt = (isdigit(stoi(first))); 
    //cout << "Starting Queue" <<endl; 
    //cout <<"Size of quee is " << s.size() <<endl; 

    for (int i = 0;i<s.size();i++) { 



    if (isParam(s[i]) || s[i] == "e") { 
     exprStack.push(new Integer(stoi(s[i]))); 
     //new Integer(stoi(first)); 
     //s.pop(); 
     //PostFixUtilities(s); 
     cout << "Adding Int to stack " << s[i]<< endl; 
    } 
     else if (s[i] == "+") { 

      Expression* que1 = exprStack.top(); 
      exprStack.pop(); 
      Expression* que2 = exprStack.top(); 
      exprStack.pop(); 
      Expression * tempAdd = new Addition(que1,que2); 
      exprStack.push(tempAdd); 
      cout << "Adding + to stack " <<endl; 


     } 
     else if (s[i] == "-") { 
     Expression* que1 = exprStack.top(); 
     exprStack.pop(); 
     Expression* que2 = exprStack.top(); 
     exprStack.pop(); 
     Expression * tempAdd = new Subtraction(que2,que1); 
     exprStack.push(tempAdd); 
      cout << "Adding - to stack" <<endl; 

     } 
     else if (s[i] == "*") { 
     Expression* que1 = exprStack.top(); 
     exprStack.pop(); 
     Expression* que2 = exprStack.top(); 
     exprStack.pop(); 
     Expression * tempAdd = new Multiplication(que2,que1); 
     exprStack.push(tempAdd); 
      cout << "Adding * to stack" <<endl; 

     } 
     else if (s[i] == "/") { 

     Expression* que1 = exprStack.top(); 
     exprStack.pop(); 
     Expression* que2 = exprStack.top(); 
     //cout << "que2 on division is (right side)" << que2->getValue() <<endl; 
     exprStack.pop(); 
     cout << "middle division" <<endl; 
     Expression * tempAdd = new Division(que1,que2); 
     exprStack.push(tempAdd); 
      cout << "Adding/to stack" <<endl; 



     } 
     else if (s[i] == "^") { 
     Expression* que1 = exprStack.top(); 
     exprStack.pop(); 
     Expression* que2 = exprStack.top(); 
     exprStack.pop(); 
     Expression * tempAdd = new Exponential(que2,que1); 
     exprStack.push(tempAdd); 
      cout << "Adding^to stack" <<endl; 

     } 
     else if (s[i] == "rt") { 
     Expression* que1 = exprStack.top(); 
     exprStack.pop(); 
     Expression* que2 = exprStack.top(); 
     exprStack.pop(); 

     Expression * tempAdd = new nThRoot(que2,que1); 

     } 
     else if (s[i] == "ans") { 
     if (numberofRuns >= 0) { 
      cout << "numberodRuns is more than 1" <<endl; 
      cout << "Answer Found" <<endl; 

      cout << "not crashed before push temp" <<endl; 
      if (expression.ansStack.empty()) { 
       cout <<"Anstack is null" <<endl; 
      } 
      else 
       cout << "Anstack is not full" <<endl; 
      Expression* temporary = expression.ansStack.top(); 
      cout << "not crashed before ansstack value" <<endl; 
      //cout << expression.ansStack.pop(); 
      exprStack.push(temporary); 
      cout << "Pushing to exprstack" << exprStack.top()->getValue() <<endl; 
      expression.ansStack.pop(); 


     } 
     else { 
      cout << "Less than 1" <<endl; 
     } 


     } 

     else 
     { 
      return; 
     } 


    } 
    numberofRuns++; 
    Expression* temp = exprStack.top(); 
    expression.ansStack.push(temp); 
    if (expression.ansStack.empty()) { 
     cout <<"Anstack is empty(end of progra)" <<endl; 
    } 
    else 
     cout << "Anstack is filled with something " << expression.ansStack.top()<<endl; 

    expression.ansStack.top()->getValue() <<endl; 
    cout << exprStack.top()->getValue() << endl; 


}; 

回答

-1

使用通過引用傳遞

見:http://www.cplusplus.com/reference/string/string/operator+=/

string calculatorStringOutput; 

void returnEquation(string &calculatorStringOutput){ 
//This is how you append to string 
    calculatorStringOutput += valueOne; 
    calculatorStringOutput += operationChar; 
    calculatorStringOutput += valueTwo; 
} 
+0

如果你要downvote我,至少告訴我爲什麼。 –