本文介绍了将infix转换为Rpn(调车场)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,用于使用调车场将infix转换为ron.我知道算法如何运作良好,对此我没有任何问题. 但是当我运行时,什么也没发生. 当我调试它时,在堆栈初始化行上出现未知错误
here is my code to convert infix to ron using shunting yard . I know how the algorithm works well and I have no problem with that . but when I run this just nothing happen . when I debug it I get unknown error on stack initialization line
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <stack> using namespace std ; void convert (char *) ; double eval(char *) ; int precedence(char); int main() { cout << "\n Enter an expression :\n" ; cout << " >> " ; char input[51] ; cin.get(input,50); convert (input) ; system("PAUSE"); } int precedence(char op) { switch (op) { case '-' : case '+' : return 1 ; case '*' : case '/' : return 2 ; case '^' : return 3 ; default : return 0 ; } } void convert(char* exp) { stack <char> stc; string outputQueue ; // string temp ; while (*exp) { while (isspace(*exp)){ *exp++ ; } if (*exp == '(') { stc.push(*exp++) ; } else if (*exp == ')'){ while (stc.top()!='(') { outputQueue += stc.top() ; stc.pop(); *exp++; } stc.pop() ; /** Havaset bashe */ } else if (isdigit(*exp)){ while (isdigit(*exp)) { outputQueue += *exp++ ; } outputQueue += " " ; } else if (strchr("+-*/^",*exp)) { if (precedence(*exp) > precedence(stc.top())) { stc.push(*exp++) ; } else { if (precedence (*exp) == precedence (stc.top()) && precedence(*exp) == 3) { stc.push(*exp++) ; } else { outputQueue += stc.top() ; outputQueue += ' ' ; stc.pop(); } } } } while (!stc.empty()) { outputQueue =+ stc.top() ; stc.pop(); } cout << outputQueue ; }推荐答案
我不知道您的意思,我修复了代码. 在2行中,您尝试获取空堆栈的顶部元素.这会产生断言.
I don´t know what you mean, i fix the code. In 2 lines you try to get the top element of an empty stack. This produce a assert break.
#include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <stack> using namespace std ; void convert (char *) ; double eval(char *) ; int precedence(char); int main() { cout << "\n Enter an expression :\n" ; cout << " >> " ; char input[51] ; cin.get(input,50); convert (input) ; system("PAUSE"); } int precedence(char op) { switch (op) { case '-' : case '+' : return 1 ; case '*' : case '/' : return 2 ; case '^' : return 3 ; default : return 0 ; } } void convert(char* exp) { stack <char> stc; string outputQueue ; while (*exp) { while (isspace(*exp)) { *exp++ ; } if (*exp == '(') { stc.push(*exp++) ; } else if (*exp == ')') { while (stc.top()!='(') { outputQueue += stc.top() ; stc.pop(); *exp++; } stc.pop() ; /** Havaset bashe */ } else if (isdigit(*exp)) { while (isdigit(*exp)) { outputQueue += *exp++ ; } outputQueue += " " ; } else if (strchr("+-*/^",*exp)) { if (precedence(*exp) > precedence(stc.top())) { stc.push(*exp++) ; } else { if (precedence (*exp) == precedence (stc.top()) && precedence(*exp) == 3) { stc.push(*exp++) ; } else { outputQueue += stc.top() ; outputQueue += ' ' ; stc.pop(); } } } } while (!stc.empty()) { outputQueue =+ stc.top() ; stc.pop(); } cout << outputQueue ; }更多推荐
将infix转换为Rpn(调车场)
发布评论