convert infix to post fix an calculate with N digits?
<p>I just converted infix to postfix and calculates with ONE digit but i can't convert to postfix and calculate with N digits. Might anyone help me with this! Thanks alot :-)</p>

<p>here is my code with single digit</p>

<p>CharacterStack.h</p>

<pre><code>#define MAX 100
typedef struct
{
    int top;
    char a[MAX];
} CHARACTER_STACK;

void Initialize(CHARACTER_STACK &amp;stack);
bool IsEmpty(CHARACTER_STACK stack);
bool IsFull(CHARACTER_STACK stack);
bool Push(CHARACTER_STACK &amp;stack, char x);
char Pop(CHARACTER_STACK &amp;stack);
char GetTop(CHARACTER_STACK stack);
void Display(CHARACTER_STACK stack);
</code></pre>

<p>IntegerStack.h</p>

<pre><code>#define MAX 100
typedef struct
{
    int top;
    int a[MAX];
} INTEGER_STACK;

void Initialize(INTEGER_STACK &amp;stack);
bool IsEmpty(INTEGER_STACK stack);
bool IsFull(INTEGER_STACK stack);
bool Push(INTEGER_STACK &amp;stack, int x);
int Pop(INTEGER_STACK &amp;stack);
int GetTop(INTEGER_STACK stack);
void Display(INTEGER_STACK stack);
</code></pre>

<p>PolishNotation.h</p>

<pre><code>#include "string"
using namespace std;
string Infix2Postfix(string infix);
int CalcPostfix(string postfix);
</code></pre>

<p>CharacterStack.cpp</p>

<pre><code>#include "CharacterStack.h"
#include "iostream"
using namespace std;


void Initialize(CHARACTER_STACK &amp;stack)
{
    stack.top = 0;
}

bool IsEmpty(CHARACTER_STACK stack)
{
    if (stack.top == 0)
        return true;
    return false;
}

bool IsFull(CHARACTER_STACK stack)
{
    if (stack.top == MAX)
        return true;
    return false;
}

bool Push(CHARACTER_STACK &amp;stack, char x)
{
    if ( !IsFull(stack) )
    {
        stack.a[stack.top] = x;
        stack.top++;
        return true;
    }
    return false;
}

char Pop(CHARACTER_STACK &amp;stack)
{
    if ( !IsEmpty(stack) )
    {
        stack.top--;
        return stack.a[stack.top];
    }
    return NULL;
}

char GetTop(CHARACTER_STACK stack)
{
    if ( !IsEmpty(stack) )
    {
        return stack.a[stack.top-1];
    }
    return NULL;
}

void Display(CHARACTER_STACK stack)
{
    cout &lt;&lt; "Stack characters: ";
    if ( !IsEmpty(stack) )
    {
        for (int i = stack.top - 1; i &gt;= 0; i--)
            cout &lt;&lt; stack.a[i] &lt;&lt; " ";
        cout &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; "is EMPTY !!!" &lt;&lt; endl;
    }
}
</code></pre>

<p>IntegerStack.cpp</p>

<pre><code>#include "IntegerStack.h"
#include "iostream"
using namespace std;


void Initialize(INTEGER_STACK &amp;stack)
{
    stack.top = 0;
}

bool IsEmpty(INTEGER_STACK stack)
{
    if (stack.top == 0)
        return true;
    return false;
}

bool IsFull(INTEGER_STACK stack)
{
    if (stack.top == MAX)
        return true;
    return false;
}

bool Push(INTEGER_STACK &amp;stack, int x)
{
    if ( !IsFull(stack) )
    {
        stack.a[stack.top] = x;
        stack.top++;
        return true;
    }
    return false;
}

int Pop(INTEGER_STACK &amp;stack)
{
    if ( !IsEmpty(stack) )
    {
        stack.top--;
        return stack.a[stack.top];
    }
    return NULL;
}

int GetTop(INTEGER_STACK stack)
{
    if ( !IsEmpty(stack) )
    {
        return stack.a[stack.top-1];
    }
    return NULL;
}

void Display(INTEGER_STACK stack)
{
    cout &lt;&lt; "Stack integers: ";
    if ( !IsEmpty(stack) )
    {
        for (int i = stack.top - 1; i &gt;= 0; i--)
            cout &lt;&lt; stack.a[i] &lt;&lt; " ";
        cout &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; "is EMPTY !!!" &lt;&lt; endl;
    }
}
</code></pre>

<p>PolishNotation.cpp</p>

<pre><code> #include "string"
    #include "limits"
    using namespace std;
    #include "CharacterStack.h"
    #include "IntegerStack.h"
    #include "TemplateStack.cpp"
    #include "stack"

    int Precedence(char operatorr)
    {
        if (operatorr == '+' || operatorr == '-') return 1;
        if (operatorr == '/' || operatorr == '*' || operatorr == '%') return 2;
        return 0;
    }

    bool IsOperand(char operand)
    {
        if (operand &gt;= '0' &amp;&amp; operand &lt;= '9')
            return true;
        return false;
    }

    string Infix2Postfix(string infix)
    {
        string postfix = "";

        //CHARACTER_STACK stack;
        STACK&lt;char&gt; stack; // using template
        Initialize(stack);
        string temp;
        //char ch;
        // step 1
        for (int i = 0 ; i &lt; infix.length(); i++)
        {
            char item = infix[i];
            //
            if (IsOperand(item)) // step 1.1
                postfix += item;
            else if (item == '(') // step 1.2
                Push(stack, item);
            else if (item == ')') // step 1.3
            {
                char x;
                while ( (x = Pop(stack)) != '(' )
                    postfix += x;
            }
            else // step 1.4
            {
                while ( !IsEmpty(stack) &amp;&amp; Precedence(GetTop(stack)) &gt;= Precedence(item) )
                {
                    postfix += Pop(stack);
                }
                Push(stack, item);
            }
            //Display(stack); // for DEBUG
        }
        // step 2
        char x;
        while ( (x = Pop(stack)) != NULL )
            postfix += x;

        return postfix;
    }

    int CalcPostfix(string postfix)
    {
        int result = 0;

        //INTEGER_STACK stack;
        STACK&lt;int&gt; stack; // using template
        Initialize(stack);

        // step 1
        for (int i = 0 ; i &lt; postfix.length(); i++)
        {
            char item = postfix[i];

            if (IsOperand(item)) // step 1.1
            {
                int x = item - '0';
                Push(stack, x);
            }
            else // step 1.2
            {
                int x = Pop(stack);
                int y = Pop(stack);

                int z = numeric_limits&lt;int&gt;::min(); // minimum value
                if (item == '+') z = y + x;
                else if (item == '-') z = y - x;
                else if (item == '*') z = y * x;
                else if (item == '/') z = y / x;
                else if (item == '%') z = y % x;
                Push(stack, z);
            }
            //Display(stack); // for DEBUG
        }
        // step 2
        result = GetTop(stack);

        return result;
    }
</code></pre>

<p>TemplateStack.cpp</p>

<pre><code>#include "iostream"
using namespace std;


#define MAX 100

template&lt;class T&gt;
struct STACK
{
    int top;
    T a[MAX];
};

template&lt;class T&gt;
void Initialize(STACK&lt;T&gt; &amp;stack)
{
    stack.top = 0;
}

template&lt;class T&gt;
bool IsEmpty(STACK&lt;T&gt; stack)
{
    if (stack.top == 0)
        return true;
    return false;
}

template&lt;class T&gt;
bool IsFull(STACK&lt;T&gt; stack)
{
    if (stack.top == MAX)
        return true;
    return false;
}

template&lt;class T&gt;
bool Push(STACK&lt;T&gt; &amp;stack, T x)
{
    if ( !IsFull(stack) )
    {
        stack.a[stack.top] = x;
        stack.top++;
        return true;
    }
    return false;
}

template&lt;class T&gt;
T Pop(STACK&lt;T&gt; &amp;stack)
{
    if ( !IsEmpty(stack) )
    {
        stack.top--;
        return stack.a[stack.top];
    }
    return NULL;
}

template&lt;class T&gt;
T GetTop(STACK&lt;T&gt; stack)
{
    if ( !IsEmpty(stack) )
    {
        return stack.a[stack.top-1];
    }
    return NULL;
}

template&lt;class T&gt;
void Display(STACK&lt;T&gt; stack)
{
    cout &lt;&lt; "Stack members: ";
    if ( !IsEmpty(stack) )
    {
        for (int i = stack.top - 1; i &gt;= 0; i--)
            cout &lt;&lt; stack.a[i] &lt;&lt; " ";
        cout &lt;&lt; endl;
    }
    else
    {
        cout &lt;&lt; "is EMPTY !!!" &lt;&lt; endl;
    }
}
</code></pre>

<p>main.cpp</p>

<pre><code>#include "PolishNotation.h"
#include "string"
#include "iostream"
using namespace std;
  void main()
    {
        string infix ;
        cout&lt;&lt;"insert infix ";
        cin&gt;&gt;infix;
        cout &lt;&lt; "infix = " &lt;&lt; infix &lt;&lt; endl;

        string postfix = Infix2Postfix(infix);
        cout &lt;&lt; "postfix = " &lt;&lt; postfix &lt;&lt; endl;

        int result = CalcPostfix(postfix);
        cout &lt;&lt; "result = " &lt;&lt; result &lt;&lt; endl;
    }
</code></pre>
