**/// Improved Version '$' replaced as '^'
Infix to Postfix conversion is one of the most important applications of stack. A humble request Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker on our website. Infix / Postfix converter This tool gives you a way to change between infix (seen normally in most writing) and post fix also known as reverse polish notation or Polish postfix notation which is used in some HP calculators such as the 9100A and HP-35.
using namespace std;
bool IsOperand(char ch)
{
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
return true;
}
return false;
}
{
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
return true;
}
return false;
}
bool IsOperator(char C)
{
if (C '+' || C '-' || C '*' || C '/' || C '^') {
return true;
}
return false;
}
bool IsLeftParenthesis(char ch)
{
if (ch '(') {
return true;
}
return false;
}
{
if (C '+' || C '-' || C '*' || C '/' || C '^') {
return true;
}
return false;
}
bool IsLeftParenthesis(char ch)
{
if (ch '(') {
return true;
}
return false;
}
bool IsRightParenthesis(char ch)
{
if (ch ')') {
return true;
}
return false;
}
{
if (ch ')') {
return true;
}
return false;
}
bool Flag(char ch)
{
if (!IsOperand(ch) || !IsOperator(ch) || !IsLeftParenthesis(ch) || !IsRightParenthesis(ch)) {
return false;
}
return true;
}
{
if (!IsOperand(ch) || !IsOperator(ch) || !IsLeftParenthesis(ch) || !IsRightParenthesis(ch)) {
return false;
}
return true;
}
int IsRightAssociative(char op)
{
if (op '^') {
return true;
}
return false;
}
{
if (op '^') {
return true;
}
return false;
}
int GetOperatorWeight(char op)
{
int weight = -1;
switch (op) {
case '+':
case '-':
weight = 1;
break;
case '*':
case '/':
weight = 2;
break;
case '^':
weight = 3;
break;
}
return weight;
}
{
int weight = -1;
switch (op) {
case '+':
case '-':
weight = 1;
break;
case '*':
case '/':
weight = 2;
break;
case '^':
weight = 3;
break;
}
return weight;
}
bool HasHigherPrecedence(char op1, char op2)
{
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
{
int op1Weight = GetOperatorWeight(op1);
int op2Weight = GetOperatorWeight(op2);
}
string InfixToPostfix(string expression)
{
stack S;
string postfix = ';
for (auto& elem : expression) {
if (Flag(elem)) {
continue;
}
// If character is operator, pop two elements from stack, perform operation and push the result back.
else if (IsOperator(elem)) {
while (!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(), elem)) {
postfix += S.top();
S.pop();
}
S.push(elem);
}
else if (IsOperand(elem)) {
postfix += elem;
}
else if (elem '(') {
S.push(elem);
}
else if (elem ')') {
while (!S.empty() && S.top() != '(') {
postfix += S.top();
S.pop();
}
S.pop();
}
}
{
stack S;
string postfix = ';
for (auto& elem : expression) {
if (Flag(elem)) {
continue;
}
// If character is operator, pop two elements from stack, perform operation and push the result back.
else if (IsOperator(elem)) {
while (!S.empty() && S.top() != '(' && HasHigherPrecedence(S.top(), elem)) {
postfix += S.top();
S.pop();
}
S.push(elem);
}
else if (IsOperand(elem)) {
postfix += elem;
}
else if (elem '(') {
S.push(elem);
}
else if (elem ')') {
while (!S.empty() && S.top() != '(') {
postfix += S.top();
S.pop();
}
S.pop();
}
}
}
int main()
{
// std::string expression = '5_4/(5^2)+(6^2^3)';
std::string expression = 'A+(B_C-(D/E^F)_G)_H';
std::string postfix = InfixToPostfix(expression);
std::cout << 'Output = ' << postfix << 'n';
}
**
{
// std::string expression = '5_4/(5^2)+(6^2^3)';
std::string expression = 'A+(B_C-(D/E^F)_G)_H';
std::string postfix = InfixToPostfix(expression);
std::cout << 'Output = ' << postfix << 'n';
}
**
I'm new to stackoverflow, and have final project for my Data Structures class in college. For my project, I am creating a calculator that takes a mathematical expression from input, such as (11-2)/3*(15+2/1)-6, and does two things:
1) Converts the expression from infix notation to postfix notation2) Uses the postfix notation to evaluate the value of the expression
I have gotten fairly close to a final product, but my issue is that the program can only take numbers 0-9, as the way I currently have the code is that the conversion function evaluates the input as a string, and thus numbers greater than 9 (i.e., double/triple digit numbers) will trip up the evaluation function. How can I detect these numbers as ints (or floats/doubles) and put them in a format (with the operators) that the evaluation function can deal with?
Additionally, I need to create exception class (incorrect parenthesis, division by zero, letters/non-operands/non-operators, etc). I have not learned much about what exception classes even are, so I'm not sure how I write these. I know it has to do with throw/catch functions, but any input is appreciated (or direction towards where I can find a simple explanation). I have pasted my code below:
GrahamGraham
1 Answer
Thanks everyone for the replies. After looking into the solutions you all posted and gathering ideas from them, I found a very simple solution to my specific case.
In my getPostFix function, I added spaces after every character inserted into the postFixStr, and then using these spaces as delimiters, I was able to add this to the first 'if' statement in my evaluate function:
Note, the * 10 is to move the previous value in as the 10's place (or 100's+ place), and the
-'0'
is a slightly clearer version of the -49
that I had before to change the ascii value to the arithmetic value.I hope this helps someone in the future!
user458130122.2k55 gold badges2121 silver badges3636 bronze badges
GrahamGraham
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.