2017-04-25 4 views
-2

Ich baue einen "einfachen" Rechner, der Polymorphismus und seine rekursive verwendet. Ich stehe ein wenig fest, wenn ich versuche, einen nicht zu vereinfachenden Ausdruck zurückzugeben.Wie man einen nicht vereinfachten Ausdruck zurückgibt

wenn ich als Eingabe 3/2 bekomme, wie soll ich es zurückgeben? Ich würde gerne als String zurückgeben, aber es wird wahrscheinlich alle anderen Methoden und Funktionen durcheinander bringen.

Mein Programm, wie ich schon sagte, lässt uns virtuelle Funktionen verwenden. Im anhängen auch meine "Haupt" -Funktion, die die Ausdrucktypen erstellt und schließlich löst sie

Vielen Dank im Voraus!

#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; 

} 

Hier ist meine PostUtilies (die ein bisschen wie mein Haupt ist)

// 
// 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; 


}; 

Antwort

-1

Verwendung Pass von Referenz

siehe: 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

, wenn Sie mich downvote Gonna Sag mir wenigstens warum. –

Verwandte Themen