lundi 24 avril 2017

How to return an unsimplified Expression

I am building a "simple" calculator that uses polymorphism and its recursive. I am a little bit stuck when trying to return an unsimplifiable expression.

if I get as an input 3 / 2, how should i return it? I would like to return is as a string, but it will probably mess up all the other methods and functions.

My program, as i said, makes us use virtual functions. Im also attaching my "main" function that creates the expression types and eventually solves them

Thanks in Advance!

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

}

Here is my PostUtilies (which is kinda like my main)

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


};

Aucun commentaire:

Enregistrer un commentaire