Thursday, 3 December 2015

Infix to Postfix Conversion

#include <iostream>
using namespace std;


class expression
{
private:
    char st[100], stack[10];
    char ch;
    int top;
public:
    expression()
    {
        top = -1;
    }
    void input()
    {
        cout << "Please Enter Expression having operator + *  / - ( )" << endl;
        cin >> st;
    }
    void scan(void);
    void push(void);
    void pop(void);

};
int main()
{
    expression obj;
    obj.input();
    obj.scan();
    cout << endl;
}
void expression::scan(void)
{
    for (int i = 0; st[i] != '\0';i++)
    {
        ch = st[i];
        if (ch == '(' || ch == ')' || (ch == '*') || (ch == '+') || (ch == '-') || (ch == '/'))
        {
            if (ch == '(')
                push();

            else if (top == -1)
                push();
            else if (stack[top] == '(')
                push();
            else if (((ch == '*') || (ch == '/')) && ((stack[top] == '+') || (stack[top] == '-')))
                push();
            else if (ch == ')')
                pop();
            else
            {
                pop();
                push();
            }

        }
        else
        {
            cout << ch;
        }
    }

    pop();
}
void expression::push(void)
{
    top++;
    stack[top] = ch;
}
void expression::pop(void)
{
    while (top != -1)
    {
        if (ch == ')')
        {
            cout << stack[top];
            top--;


            if (stack[top] == '(')
            {

                top--;
                break;
            }

        }


        else
        {
            cout << stack[top];
            top--;
            if (stack[top] = '(')
            {

                break;
            }
        }
    }
}

2 comments:

Tree Insertion

#include <iostream> using namespace std; struct node {     int data;     node*left;     node* right; }; class tree { priva...