Hello,
I've searched through the Infix to Postfix threads and I wasn't able to find my mistake. Hoping I can get some perspective from someone. Thanks in advance! I commented with "//***ERROR:" were the compiler is pointing to.
This is the algorithm:
Write a program in C++ with the following specications.1
// An algorithm for infix to postfix expression conversion.
// For example, a + b - c translates to a b + c -
// a + b * c translates to a b c * +
// (a + 2) / (5 + d) goes to a 2 + 5 d + /
// Valid operands are single digits and characters: 0-9 a-z A-Z
// Valid operators are: + - * / ( )
// Highest precedence: * /
// Lowest precedence: + -
// ( has lowest precedence on the stack and highest precedence outside of stack.
// ) never goes on stack.
// Bottom of the stack has the lowest precedence than any operator.
// Use a prec() function to compare the precedence of the operators based on the above rules.
// Note there is little error checking in the algorithm!
while there is more input
if input is an operand
print input
else
if input is '(' // '(' has lowest precedence in the stack, highest outside
push input on stack
else if input is ')'
while stack is not empty and top of stack is not '('
print top of stack
pop stack
if stack is not empty
pop stack
else error // no matching '('
else if stack is empty or prec(top of stack) < prec(input) // bottom of stack has lower
// precedence than everything
push input on stack
else if prec(top of stack) >= prec(input)
while stack is not empty and prec(top of stack) >= prec(input)
print top of stack
pop stack
push input on stack
else check for errors
while stack is not empty
print top of stack
pop stack
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 120 121 122 123 124 125 126 127 128 129 130
|
#include <iostream>
#include <string>
using namespace std;
template <class Container>
class queue
{
public:
typedef /*TYPENAME*/ Container::value_type value_type; //***ERROR: MISSING 'TYPENAME' PRIOR TO DEPENDENT TYPE NAME
//opertations
bool empty() {return c.empty();}
int size() {return c.size();}
value_type & front() {return c.front();} //INSERTED OPEN BRACE INFRONT OF 'return'
value_type & back() {return c.back();}
void push(value_type & X) {push_front(X);}
void pop() {return c.pop_back();}
protected:
Container c;
};
int main()
{
//operators listed in precedence order
enum operators {leftparen, plus, minus, multiply, divide};
// return a textual representation of an operator
string opString(operators theOp)
{
switch(theOp) //***ERROR: ASKS FOR 'EXPECTED EXPRESSION'
{
case plus: return " + ";
case minus: return " - ";
case multiply: return " * ";
case divide: return " / ";
}
void processOp(operators theOp, stack<list<operators>>& opStack, string & result)
{
// pop stack while operatord have a higher precednece
while(! opStack.empty()) && (theOp < opStack.top()))
{
//{
result += opString(opStack.top());
opStack.pop();
//}
// then push current operator
opStack.push(theOp);
}
string infixToPrefix(string infixStr)
{
stack < list<operators> > opStack;
string result ("");
int i = 0;
while(infixStr[i] != '\0')
{
if(isdigit (infixStr[i]))
{
// process other characters
while (isdigit(infixStr[[i]))
{ //INSERTED {} FOR WHILE
result +=infixStr[i++];
result += " "; //add seperator
}
}
else
Switch(infixStr[i++])
{
//process other characters
case '(': opStack.push(leftparen);
break;
case ')':
while (opStack.top() != leftparen)
{
result += opString(opStack.top());
opstack.pop();
)
opStack.pop();
break;
case '+': proccessOp(plus, opStack, result);
break;
case '-': process(minus, opStack result);
break;
case'*':processOp(multiply, opStack, result);
break;
case'/': processOp(divide, opStack, result);
break;
}
}
while(! opStack.empty())
{
//empty the stack on end of input
Result +=opString(opStack.top());
opStack.pop();
}
return result; // return result string
}
/* MOVED THIS CLASS OUTSIDE OF MAIN ^
template <class Container>
class queue
{
public:
typedef Contaner::value_type value_type;
//opertations
bool empty() {return c.empty();}
int size() {return c.size();}
value_type & front() {return c.front();} //INSERTED OPEN BRACE INFRONT OF 'return'
value_type & back() {return c.back();}
void push(value_type & X) {push_front(X);}
void pop() {return c.pop_back();}
protected:
Container c;
};
*/
} //<-- ***ERROR: ASKS FOR A " ';" AT END OF DECLARATION
|