If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Sunday, October 21, 2018

CONVERTING INFIX EXPRESSION INTO POSTFIX EXPRESSION.



///PROGRAM TO CONVERT INFIX EXPRESSION INTO POSTFIX EXPRESSION.//

#include <iostream>
#include <string.h>
#include <cctype>
#include <stdlib.h>
#include <conio.h>

using namespace std;

template <class Type>

class Infix2Postfix
{
private:
    Type *Stack;
    int Size, Top;

public:
    Infix2Postfix(int Siz)
    {
        Size = Siz;

        Stack = new Type[Size];
        strcpy(Stack, "\0");

        Top = -1;
    }

    void Push(Type Ch)
    {
        if( IsFull() )
        {
            cout << "\nSTACK OVERFLOWED!!!!!!!!!\n" << endl;
            exit(1);
        }

        Stack[++Top] = Ch;
    }

    Type Pop()
    {
        if( IsEmpty() )
        {
            cout << "\nSTACK UNDERFLOWED!!!!!!\n" << endl;
            exit(1);
        }

        return Stack[Top--];
    }

    Type TOP()
    {
        return Stack[Top];
    }

    bool IsOperator(char Ch)
    {
        return ( !isalpha(Ch) && !isdigit(Ch) );
    }

    int Priority(char Ch)
    {
   if (Ch == '^')
return 3;

else if( Ch == '*' || Ch == '/' )
            return 2;

        else if(Ch == '+' || Ch == '-')
            return  1;

            return 0;
    }

    string Convert(string Infix)
    {
        char Ch;
        string Postfix;

        for(int I =0; I <Infix.length(); I++)
        {
            Ch = Infix[I];

            if( isalpha(Ch) || isdigit(Ch) )
                Postfix += Ch;

            else if( Ch == '(' )
                    Push(Ch);

            else if( Ch == ')' )
            {
                while(TOP() !='(')
                {
                    Postfix += Pop();
                }

                Pop();
            }

            else
            {
                if(IsOperator( TOP() ) )
                   {
                       while( Priority(Ch) <= Priority( TOP() ) )
                                 Postfix += Pop();

                             Push(Ch);
                    }
            }
}

while(!IsEmpty())
            Postfix += Pop();

        return Postfix;
    }

    inline bool IsFull()
    {
        return (Top == Size - 1);
    }

    inline bool IsEmpty()
    {
        return (Top == -1);
    }

    ~Infix2Postfix()
    {
        cout << "\n\n\n****************PROGRAM ENDED*******************\n\n\n" << endl;
    }
};

int main()
{
    Infix2Postfix<char> Obj(100);
    string Expression;

    cout << "Enter any INFIX Expression: ";
    cin >> Expression;

    cout << "\nExpression After Converting To POSTFIX is: " << Obj.Convert(Expression) << endl;

    getch();
    return 0;
}

SORTING ARRAY USING SELECTION SORT.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "\nEnter Size Of Array: ";
cin >> No;
Num = No;

cout << "Enter Elements Of Array: " << endl;
for (int I = 0; I <Num; I++)
{
cin >> Arr[I];
}
}

void Select_Sort()
{
for (int I = 0; I <Num - 1; I++)
{
int Min = I;

for (int J = I + 1; J <Num; J++)
{
if (Arr[J] <Arr[Min])
{
Min = J;

int Temp = Arr[I];
Arr[I] = Arr[Min];
Arr[Min] = Temp;
}
}
}
}

void Print()
{
cout << "\nElements Of Array is: " << endl;
for (int I = 0; I <Num; I++)
{
cout << Arr[I] << endl;
}
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(10);

Obj.Input();
Obj.B_Sort();
Obj.Print();

getch();
return 0;
}

MERGING TWO ARRAYS IN ONE ARRAY.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;
Num = No;

cout << "Enter Elements Of Array: " << endl;
for (int I = 0; I <Num; I++)
{
cin >> Arr[I];
}
}

void Merge(Array Obj1, Array Obj2)
{
int I = 0, J = 0;
for (; I <Obj1.Num;)
{
Arr[Num++] = Obj1.Arr[I++];
}

for (; J <Obj2.Num;)
{
Arr[Num++] = Obj2.Arr[J++];
}
}

void Print()
{
cout << "\nElements Of Array is: " << endl;
for (int I = 0; I <Num; I++)
{
cout << Arr[I] << endl;
}
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj1(10), Obj2(10), Obj3(20);

Obj1.Input();
Obj2.Input();

Obj3.Merge(Obj1, Obj2);   ///elements of Obj1 and Obj2 will be Merged in Obj3;
Obj3.Print();

getch();
return 0;
}

PROGRAM FOR LINEAR SEARCH IN ARRAY.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I < Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
cout << "Enter Size Of Array: ";
cin >> Num;

cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I < Num; I++)
{
cin >> Arr[I];
}
}

void L_Search()
{
int Value, Index, Times = 0;
bool Find = false;

cout << "\nEnter Value U want To Search: ";
cin >> Value;

for (int I = 0; I < Num; I++)
{
if (Arr[I] == Value)
{
Index = I;
Find = true;
Times++;          ///Times work as counter and will count occurrences;
}
}

if (!Find)
cout << "\nValue Not Found...." << endl;

else
cout << "Value Find " << Times << " times in an Array." << endl;
}
};

int main()
{
Array Obj1(10);

Obj1.Input();
Obj1.L_Search();

getch();
return 0;
}

INSERT AND DELETE ELEMENTS FROM ARRAY's GIVEN INDEX.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;
Num = No;

cout << "Enter Elements Of Array: " << endl;
for (int I = 0; I <Num; I++)
{
cin >> Arr[I];
}
}

void Insert(int Index, int Value)
{
for (int I = Num + 1; I >= Index; I--)
{
Arr[I] = Arr[I - 1];
}

Arr[Index] = Value;
Num++;
}

void Delet(int Index)
{
for (int I = Index; I <Num; I++)
{
Arr[I] = Arr[I + 1];
}

Num--;
}

void Print()
{
cout << "\nElements Of Array is: " << endl;
for (int I = 0; I <Num; I++)
{
cout << Arr[I] << endl;
}
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(10);

Obj.Input();
Obj.Insert(3, 334);       ///334 inserted at index 4th and size increases by 1;
Obj.Delet(5);                 ///value at 6th index deleted and size decreases by 1;
Obj.Print();

getch();
return 0;
}

SORTING ARRAY USING BUBBLE SORT.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "\nEnter Size Of Array: ";
cin >> No;
Num = No;

cout << "Enter Elements Of Array: " << endl;
for (int I = 0; I <Num; I++)
{
cin >> Arr[I];
}
}

void B_Sort()
{
int temp = 0;
for (int I = 0; I <Num - 1; I++)
{
for (int J = 0; J <Num - I - 1; J++)
{
if (Arr[J] >Arr[J + 1])
{
temp = Arr[J];
Arr[J] = Arr[J + 1];
Arr[J + 1] = temp;
}
}
}
}

void Print()
{
cout << "\nElements Of Array is: " << endl;
for (int I = 0; I <Num; I++)
{
cout << Arr[I] << endl;
}
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(10);

Obj.Input();
Obj.B_Sort();
Obj.Print();

getch();
return 0;
}

PROGRAM FOR BINARY SEARCH IN ARRAY.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Num;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I < Siz; I++)
{
Arr[I] = 0;
}

Num = 0;
Size = Siz;
}

void Input()
{
cout << "Enter Size Of Array: ";
cin >> Num;

cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I < Num; I++)
{
cin >> Arr[I];
}
}

void B_Search()
{
bool Find;
int  Value, Beg = 0, Mid = 0, Count = 0, End = Num - 1;

cout << "\nEnter any Value you want to Search: ";
cin >> Value;

while (Beg <= End)
{
Mid = (Beg + End) / 2;

if (Arr[Mid] == Value)
{
Find = true;
///Count++;                    ///Count will count number of occurrences of searched value;
}

else if (Value  < Arr[Mid])
End = Mid - 1;

else
Beg = Mid + 1;
}

if (!Find)
cout << "\nValue Not Find in Array...." << endl;

else
cout << "\nValue Found " << Count << " times in Array. " << endl;
}
};

int main()
{
Array Obj1(10);

Obj1.Input();
Obj1.B_Search();

getch();
return 0;
}