0

My problem is I don't know how to get anything to output from here. I want to print the postfix result so far using printResult().. how can I do this? I am not sure how to cout the result I am getting inside my inToPost() function which should be the result of converting to postfix. Thanks.

#include <iostream>
#include <fstream>
#include <stack>
#include <string>

using namespace std;

class Expression{
    public:
        string inToPost();
        string convertThis; // Expression that we want converted

        Expression(string input, int direction);   //constructor
        bool isOperator(char character);
        bool isOperand(char character);
        int isHigherWeight(char character);
        bool isHigherPrecedence(char op1, char op2);
        string printResult();

    private:
        string infix;
        string postfix;
        string prefix;


};

                                      //Constructor function
Expression::Expression(string input, int direction){
    switch (direction){
        case 1: infix = input;
        case 2: postfix = input;
        case 3: prefix = input;

    }
}

                                    //Operator Function checks to see if character is a legal symbol
bool Expression::isOperator(char character){
    if((character == '*')||(character == '+')||(character == '-')||(character == '/'))
                            return true;
                            else
                            return false;
    }


                                    //Operand Function checks to see if character is a legal character
bool Expression::isOperand(char character){
    if(character >= 'a' && character <= 'z')
    return true;
    if(character >= 'A' && character <= 'Z')
    return true;
    if(character >= '0' && character <= '9')
    return true;
    else
    return false;
}

                                //Function determines the weight of Operator.
int Expression::isHigherWeight(char character){
    int weight = 0;   // or -1?
    switch(character){
    case '+':
    case '-':
        weight = 1;
    case '*':
    case '/':
        weight = 2;
    }
    return weight;
}

                               //Function that compares weights of two different Operators.
bool Expression::isHigherPrecedence(char oper1, char oper2){
    int op1Weight = isHigherWeight(oper1);
    int op2Weight = isHigherWeight(oper2);

    // If operators have equal precedence, return true
    // return false
    return op1Weight > op2Weight ?  true: false;{

    }
}

string Expression::inToPost(){

    stack<char> Stack;
    string postfix = "";  // Initialize postfix as empty string.

        for(int i = 0;i< convertThis.length();i++){   //go through array of string convertThis

            if (convertThis[i] == '('){            //1-Read the left parenthesis and push it onto the stack
                Stack.push(convertThis[i]);
        }


            else if(isOperand(convertThis[i])){    //2-else if( the next input is a number or letter)
                 cout << convertThis[i];           //3-Read the operand and write it to the output
        }



            else if(isOperator(convertThis[i])){   //4-else if the next input is operator
                   cout << Stack.top();
                   Stack.pop();                    //5-Print the top operation and pop it
            }
                                    //6-
            while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),convertThis[i])){
                Stack.push(convertThis[i]);     //7- Read the next input symbol, and push this symbol onto the stack
            }
                          // 8- Read and discard the next input symbol(which should be a right parenthesis).
                if (convertThis[i] == ')'){
                    i+1;

                                           //  9- Print the top operation and pop it; Keep printing and popping until
                while (!Stack.top() == '('){
                   cout << Stack.top();
                   Stack.pop();
                }
    }
                Stack.pop();                 //10- Finally, pop the left parenthesis.

            while(!Stack.empty()){
                 cout << Stack.top();

            }


    return postfix;

   }

  }




string Expression::printResult(){





  return postfix;

   }


int main(){

string convertThis;


int choice;


cout << "|-----Here is my conversion menu-----|" << endl;
cout << "|----What are you converting to?-----|" << endl << endl;
cout << "1- Infix to postfix" << endl;
cout << "2- Infix to prefix" << endl;
cout << "3- postfix to infix?" << endl;
cout << "4- prefix to infix?" << endl;
//cin >> choice;
//cin.ignore();

cout << "Now enter the expression you want to convert ";
getline(cin,convertThis);

//Expression printResult;

//cout << printResult.printResult();


}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • You'll get better help by reducing your code to a simple, minimal example of what's not working and what you expect it to do. – Matt Feb 24 '17 at 04:12
  • 1
    What's not working? That wasn't even the question. I just don't know how to continue. – mikeboss21 Feb 24 '17 at 04:19
  • 1
    "Assuming everything I've done up to here is correct" Don't assume. Write the program in small pieces. Write a piece, test that piece. Proceed when it works. You may find writing all of the test cases first helps you write the code. To test something you have to know how data gets in and out (thus defining all of your functions and the interface in general) and what comes out for a given input. If you don't know what's supposed to happen, you don't understand the problem enough to write code and can save yourself writing a lot of bad code. – user4581301 Feb 24 '17 at 04:57
  • 2
    You say you want to print the result. By "result" do you mean "the evaluated value"? Because you have an `evaluate` method that returns a `double`. So is the question "How do I print a `double`?" You need to ask a specific question. – Raymond Chen Feb 24 '17 at 06:25
  • 1
    @mikeboss21 What's not working is you're not getting output. Try something, and explain why what you tried is not working. You're question is bad because it's just a wall of code asking where to go next. – Matt Feb 24 '17 at 14:40
  • I want to print the result of postfix. I havent even started to get near evaluate yet. Any help would be appreciated. I need to print to test, but I don't know how to print my postfix with classes. – mikeboss21 Feb 24 '17 at 15:54

1 Answers1

0

Your question is far too complicated to be asking "How do I send output to the screen?" You have 10 functions in a class...a class that you never used in main. Half the functions are empty...Start smaller. Start with a class that has a private string variable, a constructor that takes a string and a single public function to display that string.

It will look something like this...

#include <string>
#include <iostream>

class MyClass{
public:
    MyClass(std::string str) : m_string(str){}
    void PrintString() { std::cout << m_string << std::endl; }

private:
    std::string m_string;
};

int main(int argc, char * argv[]){

    std::string inputString("This is my test string.");

    MyClass myClass(inputString); // create an instance of your class
    myClass.PrintString(); // Print the variable within your class

    return 0;
}

Once you have that working add a second input to the constructor for your 4 options. There are many ways to do this. Then one by one add the remaining functions and test them.

Matt
  • 2,554
  • 2
  • 24
  • 45
  • Hey this was more insightful. I am not sure what line: MyClass(std::string str) : m_string(str){} is doing though. Our teacher wants us to use our class and constructor how I wrote it. I did add many functions for later though as I was thinking about how I will do it. I am only working on infix to postfix in this program so maybe I will delete everything else and maybe someone could help me better like you said. I am trying to read about printing results in classes but I just can't get it to work in my program. – mikeboss21 Feb 25 '17 at 17:44
  • Anyways I edited out what I dont need so far. Thanks for the help though. – mikeboss21 Feb 25 '17 at 17:50