infix/postfix/prefix程序.想要使用我的printresult()函数打印结果.需要帮助获得输出

Infix/postfix/prefix program. Want to print a result using my printResult() function. Need help getting an output

本文关键字:结果 函数 打印 帮助 输出 printresult 程序 prefix postfix infix 我的      更新时间:2023-10-16

我的问题是我不知道如何从这里输出任何东西。我想使用printresult()到目前为止打印后缀结果。我不确定如何计算我进入Intopost()函数的结果,这应该是转换为后缀的结果。谢谢。

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

}

您的问题太复杂了,无法问"如何将输出发送到屏幕?"您在一堂课中有10个功能...您在main中从未使用过的类。一半的功能是空的...启动较小。从具有私有string变量的类开始,一个构造函数,该构造函数需要string和一个public函数显示该字符串。

它看起来像这样...

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

有工作后,为您的4个选项添加了第二个输入。有很多方法可以做到这一点。然后一一添加剩余的功能并测试它们。

相关文章: