将infix转换为Rpn(调车场)

编程入门 行业动态 更新时间:2024-10-13 20:18:38
本文介绍了将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(调车场)

本文发布于:2023-11-29 12:13:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646365.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:调车场   转换为   infix   Rpn

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!