Shunting Yard Algorithm, App Crash Help?
So Im writing code that is supposed to convert infix to postfix, and I have it working when I am not including parentheses, the working code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
bool isNumber(std::string p_str) {
for (std::size_t i = 0; i < p_str.length(); i++) {
if (p_str[i] != '0'
&& p_str[i] != '1'
&& p_str[i] != '2'
&& p_str[i] != '3'
&& p_str[i] != '4'
&& p_str[i] != '5'
&& p_str[i] != '6'
&& p_str[i] != '7'
&& p_str[i] != '8'
&& p_str[i] != '9'
&& p_str[i] != '.') {
return false;
}
}
return true;
}
int precedenceOf(std::string p_operator) {
int precedence;
if (p_operator == "+" || p_operator == "-")
precedence = 2;
else if (p_operator == "*" || p_operator == "/")
precedence = 3;
else if (p_operator == "^")
precedence = 4;
return precedence;
}
bool isOperator(std::string p_str) {
if (p_str == "+" || p_str == "-"
|| p_str == "*" || p_str == "/"
|| p_str == "%" || p_str == "^")
return true;
else
return false;
}
std::vector<std::string> inputToInfix(std::string input) {
std::vector<std::string> infix;
std::string tempNum;
std::string token;
for (std::size_t i = 0; i < input.length(); i++) {
token = std::string(1, input[i]);
if (isNumber(token))
tempNum += token;
if (isOperator(token) || token == "(" || token == ")") {
if (!tempNum.empty()) {
infix.push_back(tempNum);
tempNum.clear();
}
infix.push_back(token);
}
}
if (!tempNum.empty())
infix.push_back(tempNum);
return infix;
}
std::vector<std::string> infixToPostfix(std::vector<std::string> infix) {
std::vector<std::string> stack, output;
std::string token;
for (std::size_t i = 0; i < infix.size(); i++) {
token = infix[i];
if (isNumber(token)) {
output.push_back(token);
}
else if (isOperator(token)) {
while (!stack.empty() &&
((token != "^" && precedenceOf(token) <= precedenceOf(stack.back())) ||
(precedenceOf(token) < precedenceOf(stack.back())))) {
output.push_back(stack.back());
stack.pop_back();
}
stack.push_back(token);
}
}
while (!stack.empty()) {
output.push_back(stack.back());
stack.pop_back();
}
return output;
}
int main() {
std::string str = "2*3-4/5";
std::vector<std::string> infix = inputToInfix(str);
std::vector<std::string> postfix = infixToPostfix(infix);
for (std::size_t i = 0; i < postfix.size(); i++)
std::cout << postfix[i];
}
|
But when I try to add support for parentheses shown in the code below, I get an app crash error whenever I try to run it:
1 2 3 4 5 6 7 8 9 10 11
|
else if (token == "(") {
stack.push_back(token);
}
else if (token == ")") {
while (stack.back() != "(") {
output.push_back(stack.back());
stack.pop_back();
}
if (stack.back() == "(")
stack.pop_back();
}
|
I can't see why im having this issue, my full unworking code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
bool isNumber(std::string p_str) {
for (std::size_t i = 0; i < p_str.length(); i++) {
if (p_str[i] != '0'
&& p_str[i] != '1'
&& p_str[i] != '2'
&& p_str[i] != '3'
&& p_str[i] != '4'
&& p_str[i] != '5'
&& p_str[i] != '6'
&& p_str[i] != '7'
&& p_str[i] != '8'
&& p_str[i] != '9'
&& p_str[i] != '.') {
return false;
}
}
return true;
}
int precedenceOf(std::string p_operator) {
int precedence;
if (p_operator == "+" || p_operator == "-")
precedence = 2;
else if (p_operator == "*" || p_operator == "/")
precedence = 3;
else if (p_operator == "^")
precedence = 4;
return precedence;
}
bool isOperator(std::string p_str) {
if (p_str == "+" || p_str == "-"
|| p_str == "*" || p_str == "/"
|| p_str == "%" || p_str == "^")
return true;
else
return false;
}
std::vector<std::string> inputToInfix(std::string input) {
std::vector<std::string> infix;
std::string tempNum;
std::string token;
for (std::size_t i = 0; i < input.length(); i++) {
token = std::string(1, input[i]);
if (isNumber(token))
tempNum += token;
if (isOperator(token) || token == "(" || token == ")") {
if (!tempNum.empty()) {
infix.push_back(tempNum);
tempNum.clear();
}
infix.push_back(token);
}
}
if (!tempNum.empty())
infix.push_back(tempNum);
return infix;
}
std::vector<std::string> infixToPostfix(std::vector<std::string> infix) {
std::vector<std::string> stack, output;
std::string token;
for (std::size_t i = 0; i < infix.size(); i++) {
token = infix[i];
if (isNumber(token)) {
output.push_back(token);
}
else if (isOperator(token)) {
while (!stack.empty() &&
((token != "^" && precedenceOf(token) <= precedenceOf(stack.back())) ||
(precedenceOf(token) < precedenceOf(stack.back())))) {
output.push_back(stack.back());
stack.pop_back();
}
stack.push_back(token);
}
else if (token == "(") {
stack.push_back(token);
}
else if (token == ")") { //
while (stack.back() != "(") {
output.push_back(stack.back());
stack.pop_back();
}
if (stack.back() == "(")
stack.pop_back();
} //
}
while (!stack.empty()) {
output.push_back(stack.back());
stack.pop_back();
}
return output;
}
int main() {
std::string str = "2*(3-4)/5";
std::vector<std::string> infix = inputToInfix(str);
std::vector<std::string> postfix = infixToPostfix(infix);
for (std::size_t i = 0; i < postfix.size(); i++)
std::cout << postfix[i];
}
|
Topic archived. No new replies allowed.