Infix to Prefix
Mar 2, 2017 at 2:08am
if I take this expression -> (1+((2+3)∗(4∗5)))
it converts to this -> +**54+321
Anyone know a solution just based off my function here? I can post my infix to postfix if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
string Expression::inToPre(string myExpression){
// **********MUST BE FULLY PARENTHESIZED**********
myExpression = inToPost(myExpression); //convert infix to postfix, then convert postfix to prefix
int count, length;
length = myExpression.length();
string prefix = "";
for(count = length - 1; count >= 0; count--)
{
prefix += myExpression[count];
}
return prefix;
}
}
| |
Last edited on Mar 2, 2017 at 2:09am
Mar 2, 2017 at 9:13am
Not really fancy but it works at least for the most parts.
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 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
#include <sstream>
int prec(char input);
using namespace std;
int main()
{
std::string final_string;
std::string input_string;
std::stack <char> expression_stack;
// "(1 + ((2 + 3) * (4 * 5)))"
cout << "Please enter an expression (For example, a + b - c)" << endl;
cout << "Enter an expression : "; getline(cin, input_string);
cout << "The prefix expression : ";
{
std::stringstream cout;
int i = 0;
while(i < input_string.size())
{
char input = input_string[i];
if((input >= '0' && input <= '9') || (input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
{
cout << input << ' ';
}
else if(input == '(')
{
expression_stack.push(input);
}
else if(input == ')')
{
while(!expression_stack.empty() && expression_stack.top() != '(')
{
cout << expression_stack.top() << ' ';
expression_stack.pop();
}
if(!expression_stack.empty()) expression_stack.pop();
else
{
cout << endl;
cerr << "Error : No matching '(' token" << endl; cin.get(); return 100;
}
}
else if(input == '*' || input == '/' || input == '+' || input == '-')
{
if(expression_stack.empty() || prec(expression_stack.top()) < prec(input))
{
expression_stack.push(input);
}
else
{
while(!expression_stack.empty())
{
if(prec(expression_stack.top()) < prec(input)) break;
cout << expression_stack.top() << ' ';
expression_stack.pop();
}
expression_stack.push(input);
}
}
else if(input_string[i] != ' ')
{
cout << endl;
cerr << "Error : Invalid character or token : '" << input << "'" << endl; cin.get(); return 100;
}
i++;
}
while(!expression_stack.empty())
{
cout << expression_stack.top() << ' ';
expression_stack.pop();
}
final_string = cout.str();
std::reverse(final_string.begin(), final_string.end());
}
cout << final_string;
cout << endl;
return 0;
}
// Returns a precedence value for a certain operator
// Lowest : (
// Normal : + -
// Highest : * /
int prec(char input)
{
int precedence_input;
if(input == '(') precedence_input = 0;
else
{
if(input == '+' || input == '-') precedence_input = 1;
else
{
if(input == '*' || input == '/') precedence_input = 2;
else
{
cout << endl;
cerr << "Error : Invalid input operator : " << input << endl; cin.get(); exit(100);
}
}
}
return precedence_input;
}
| |
Please enter an expression (For example, a + b - c)
Enter an expression : (1 + ((2 + 3) * (4 * 5)))
The prefix expression : + * * 5 4 + 3 2 1 |
Topic archived. No new replies allowed.