infix to postfix

Hello all I am writing a program that will read in an expression and output the expression in postfix. I am having trouble with the operands. I can't seem to figure out how to tell what the top item on the stack is, so it can compare the priorities of the two operands.

Here is my sruct for stack:

struct stack
{
T ar[20];
int top;
stack() { top = -1; }

void push(T x)
{
ar[++top] = x;
}

void pop(T &x)
{
x = ar[top--];
}
int empty()
{
return top == -1;
}
};

Here is the main program:

#include <iostream>
#include <iostream>
#include <iostream>
#include <fstream>
#include "ts.cpp"
#include <string>
#include <cctype>

int priority(char a)
{
int temp;
if(a =='^')
temp = 3;
if( a == '*' || a == '/')
temp = 2;

if(a == '+' || a == '-')
temp = 1;
return temp;


}

using namespace std;
int main()
{
string aline;

getline(cin,aline);
cout<<aline<<endl;

char x;
stack<char> stackk;





for(int i = 0; i< aline.length(); i++)
{
if(aline[i] != '^' && aline[i] != '*' && aline[i] != '/' && aline[i] != '+' && aline[i] != '-' && aline[i] != ' ')
{
stackk.push(aline[i]);
}

if(aline[i] =='^' || aline[i] == '*' || aline[i] == '/' || aline[i] == '+' || aline[i] == '-')
{
cout<<"is a operand"<<aline[i]<<endl;

while(!stackk.empty() && priority(stackk.top()) >= priority(aline[i]))
{
stackk.pop(i);
}
}


}



while (!stackk.empty())
{
stackk.pop(x);
cout <<"stack"<< x << endl;
}



}


Topic archived. No new replies allowed.