函数未输出到主

时间:2018-09-24 22:39:54

标签: c++ function stack postfix-notation

这是用于学校

我被分配将表达式从infix转换为postfix并评估该表达式。我知道这不是解决此问题的最有效方法,但是我有两个单独的函数(一个用于转换符号,另一个用于解决表达式)。我的转换函数有效,因此我认为没有理由发布该函数,但是我的Solve函数不会向我的main输出任何内容。我将在下面发布必要的功能,但请告知我是否应该发布整个程序。


#include <stdlib.h>
#include <iostream>
#include <stack>

using namespace std;

/*
Function precedence: This function will check which operator comes first. I
control which operator takes precedence by returning values.
*/
int precedence(char checkOperator){

  if(checkOperator == '^')
    return 4;
  else if(checkOperator == '*' || checkOperator == '/')
    return 3;
  else if(checkOperator == '+' || checkOperator == '-')
    return 2;
  else
    return 1;
}

/*
Function applyOperation: Just applies the operation
*/
int applyOperation(int valueOne, int valueTwo, char operation){

  switch(operation){
    case '+': return valueOne + valueTwo;
    case '-': return valueOne - valueTwo;
    case '*': return valueOne * valueTwo;
    case '/': return valueOne / valueTwo;
    }
}
/*
Function solveExpression: This function will solve the expression using a similar
approach used in function convertPostfix
*/
void solveExpression(string input){

  stack<int> operands;
  stack<char> operators;
  int stringLen = input.length();

  for(int i = 0; i < stringLen; i++){ //Traverse

    if(input[i] == ' '){ //Skip whitespace
      continue;
    }

    else if(input[i] == '('){ //Check if opening paren
            operators.push(input[i]);
    }

    else if(isdigit(input[i])){ //Check if theres a number then pushes it onto the operand stack
            int value = 0;
            value = value + (input[i]-'0'); //Not sure why -0 is needed, but I was getting crazy values without it
            operands.push(value);
        }

    //solve everything before the ')' is detected
    else if(input[i] == ')'){
            while(!operators.empty() && operators.top() != '('){
                int value2 = operands.top();
                operators.pop();

                int value1 = operands.top();
                operators.pop();

                char operatorBuffer = operators.top();
                operators.pop();

                operators.push(applyOperation(value1, value2, operatorBuffer));
            }
            operators.pop(); //pop '('
    }

    //Check and take care of operators
    else{
      while(!operators.empty() && precedence(operators.top()) >= precedence(input[i])){
        int value2 = operands.top();
                operands.pop();

                int value1 = operands.top();
                operands.pop();

                char operatorBuffer = operators.top();
                operators.pop();

                operands.push(applyOperation(value1, value2, operatorBuffer));
        }
        operators.push(input[i]); //push current into operator stack
    }
  }

  //Take care of everything that wasn't taken care of prior
  while(!operators.empty()){
    int value2 = operands.top();
    operands.pop();

    int value1 = operands.top();
    operands.pop();

    char operatorBuffer = operators.top();
    operators.pop();

    operands.push(applyOperation(value1, value2, operatorBuffer));
  }
  cout << operands.top();
   //The soultion of the expression should be stored at the top of the stack
}




int main(){

  string test = "(1 +2)*3";
    convertPostfix(test);
  solveExpression(test);
    return 0;

}

程序的问题(到目前为止)是,当我调用表达式求解器函数时,它没有输出任何东西,但是我的convertPostfix函数输出了预期的输出。任何帮助表示赞赏!好的,经过一些额外的测试,我发现括号中的函数读取有问题。

0 个答案:

没有答案