Thursday 22 September 2016

C++ program for simulating job queue.


Group D: (Assignment No:01) (SPPU Syllabus Asignment No:28)

Problem Statement:

Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add job and delete job from queue.



Program Code:

//========================================
// Name        : priosimpq.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Operating System simple job queue and priority job queue in C++, Ansi-style
//================================================

#include <iostream>
using namespace std;
#define size 5

class spq
{
    int f,r,job,djob;            //data members
    int simpq[size],prioq[size];
public:
    spq() //Default constructor
    {
     f=r=-1; //init front and rear to -1.
     job=djob=0;
     prioq[-1]=0;
    }
    //To check Q is full or not
    int isQfull()
    {
        if(r==size-1)
            return 1;
        else
            return 0;
    }
    //To check Q is empty or not
    int isQempty()
    {
        if((f==-1)||(f>r))
            return 1;
        else
            return 0;
    }
    void simpqadd(); //member functions.
    void showsimpleQ();
    void delsimpleQ();
    void prioqadd();
    void delprioQ();
    void showprioQ();
};

//To insert the job inside the simple queue.
void spq::simpqadd()
{
    cout<<"\nEnter the Job: ";
    cin>>job;
    if(isQfull())
        cout<<"\nSorry !! Queue is full....\n";
    else
    {
        if(f==-1)
        {
            f=r=0;
            simpq[r]=job;
        }
        else
        {
            r=r+1;
            simpq[r]=job;
        }
    }

}

//To delete job from the simple queue.
void spq::delsimpleQ()
{
    if(isQempty())
        cout<<"\nSorry Q is empty...\n";
    else
    {
        djob=simpq[f];
        f=f+1;
        cout<<"\nDeleted job is: "<<djob;
    }
}

//To show all the jobs from SimpleQueue.
void spq::showsimpleQ()
{
    cout<<"\nThe simple Queue job are as follows....\n";
    int temp;
    for(temp=f;temp<=r;temp++)
    {
        cout<<"\t"<<simpq[temp];
    }
}

//To delete job from the simple queue.
void spq::delprioQ()
{
    if(isQempty())
        cout<<"\nSorry Q is empty...\n";
    else
    {
        djob=prioq[f];
        f=f+1;
        cout<<"\nDeleted job is: "<<djob;
    }
}

//To show all the jobs from PrioQueue.
void spq::showprioQ()
{
    cout<<"\nThe priority Queue job are as follows....\n";
    int temp;
    for(temp=f;temp<=r;temp++)
    {
        cout<<"\t"<<prioq[temp];
    }
}

//To add the jobs as per the priority.
void spq::prioqadd()
{
    int t=0;
    cout<<"\nEnter the job: ";
    cin>>job;
    if(isQfull())
        cout<<"\nSorry!! Priority Queue is full...\n";
    else
    {
        if(f==-1)
        {
            f=r=0; //initially when q is empty insert first job.
            prioq[r]=job;
        }
        else if(job<prioq[r]) //Check the priority(Ascending) of incoming job if it is high
        {
         t=r;
         while(job<prioq[t])//do until priority is high
         {
            prioq[t+1]=prioq[t]; //then shift all the jobs towards right
            t=t-1; //decrement index to check another job.
         }
         t=t+1; //increment index
         prioq[t]=job; //store job at its appropriate location.
         r=r+1; //increment rear index by one
        }
        else
        {
            r=r+1; // as per the priority store in Q.
            prioq[r]=job;
        }
    }
}

int main()
{
    spq s1,s2; //object creation. s1 for simple Q and s2 for priority Q
    int ch;
    do
    {
     cout<< "\n\t!!!Operating System Job Queue!!!" << endl; // prints the msg.
     cout<<"\n1.SimpleQ Add_Job\n2.SimpleQ Del_Job\n3.Show SimpleQ\n4.PrioQ Add_Job\n5.PrioQ Del_Job\n6.Show PrioQ";
     cout<<"\nEnter Your Choice:";
     cin>>ch;
     switch(ch)
     {
      case 1:s1.simpqadd();break;//calling adding element in simple Q without priority.
      case 2:s1.delsimpleQ();break;
      case 3:s1.showsimpleQ();break;
      case 4:s2.prioqadd();break;//calling adding element in priority Q with priority.
      case 5:s2.delprioQ();break;
      case 6:s2.showprioQ();break;
     }
    }while(ch!=7);
    return 0;
}
/************************** OUTPUT ***********************************


    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 10

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 20

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 15

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 5

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 6

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:1

Enter the Job: 2

Sorry !! Queue is full....

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:3

The simple Queue job are as follows....
    10    20    15    5    6
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 10

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 20

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 15

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 5

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    5    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:4

Enter the job: 6

    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    5    6    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:5

Deleted job is: 5
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    6    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:5

Deleted job is: 6
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:6

The priority Queue job are as follows....
    10    15    20
    !!!Operating System Job Queue!!!

1.SimpleQ Add_Job
2.SimpleQ Del_Job
3.Show SimpleQ
4.PrioQ Add_Job
5.PrioQ Del_Job
6.Show PrioQ
Enter Your Choice:
 **************************************************************************/
data structures and algorithms Web Developer

Monday 19 September 2016

Pizza parlor accepting maximum M orders.


Group D : (Assignment No: 02) (SPPU Syllabus Assignmnet No: 31)

Problem Statement:
Pizza parlor accepting maximum M orders. Orders are served in first come first served basis. Order once placed cannot be cancelled. Write C++ program to simulate the system using circular queue using array.

Program Code:

//==============================================
// Name        : Pizza_parlor.cpp
// Author      : Nitin Shivale
// Version     : 1.
// Copyright   : Your copyright notice
// Description : ervedPizza parlor accepting maximum M orders.
//==============================================

#include <iostream>
using namespace std;
#define size 5
class pizza
{
    int porder[size];
    int front,rear;
public:
    pizza()
    {
     front=rear=-1;
    }
    int qfull()
    {
     if((front==0)&&(rear==(size-1))||(front==(rear+1)%size))
         return 1;
     else
         return 0;
    }
    int qempty()
    {
        if(front==-1)
            return 1;
        else
            return 0;
    }
    void accept_order(int);
    void make_payment(int);
    void order_in_queue();
};
void pizza::accept_order(int item)
{
    if(qfull())
        cout<<"\nVery Sorry !!!! No more orders....\n";
    else
    {
        if(front==-1)
        {
            front=rear=0;
        }
        else
        {
            rear=(rear+1)%size;
        }
        porder[rear]=item;
    }
}

void pizza::make_payment(int n)
{
    int item;
    char ans;
    if(qempty())
        cout<<"\nSorry !!! order is not there...\n";
    else
    {
      cout<<"\nDeliverd orders as follows...\n";
      for(int i=0;i<n;i++)
      {
          item=porder[front];
          if(front==rear)
          {
               front=rear=-1;
          }
          else
          {
              front=(front+1)%size;
          }
          cout<<"\t"<<item;
      }
      cout<<"\nTotal amount to pay : "<<n*100;
      cout<<"\nThank you visit Again....\n";
    }
}

void pizza::order_in_queue()
{
    int temp;
    if(qempty())
    {
        cout<<"\nSorry !! There is no pending order...\n";
    }
    else
    {
        temp=front;
        cout<<"\nPending Order as follows..\n";
        while(temp!=rear)
        {
            cout<<"\t"<<porder[temp];
            temp=(temp+1)%size;
        }
        cout<<"\t"<<porder[temp];
    }
}
int main()
{
    pizza p1;
    int ch,k,n;
    do
    {
     cout<<"\n\t***** Welcome To Pizza Parlor *******\n";
     cout << "\n1.Accept order\n2.Make_payment\n3.Pending Orders\nEnter u r choice: ";
     cin>>ch;
     switch(ch)
     {
      case 1:cout<<"\nWhich Pizza do u like most....\n";
             cout<<"\n1.Veg Soya Pizza\n2.Veg butter Pizza\n3.Egg_Pizza";
             cout<<"\nPlease enter u r order: ";
             cin>>k;
             p1.accept_order(k);
             break;
      case 2:cout<<"\nHow many Pizza ?";
             cin>>n;
             p1.make_payment(n);
             break;
      case 3:cout<<"\n Following orders are in queue to deliver....as follows..\n";
             p1.order_in_queue();
             break;
     }
    }while(ch!=4);

    return 0;
}
/******************* OUTPUT *********************************************************

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    2    3
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    2    3    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?2

Deliverd orders as follows...
    2    3
Total amount to pay : 200
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    2    3    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?3

Deliverd orders as follows...
    2    3    1
Total amount to pay : 300
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Sorry !! There is no pending order...

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 3

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    1    2    3    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 2

How many Pizza ?2

Deliverd orders as follows...
    1    2
Total amount to pay : 200
Thank you visit Again....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2    1
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 2

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 3

 Following orders are in queue to deliver....as follows..

Pending Order as follows..
    3    1    2    1    2
    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice: 1

Which Pizza do u like most....

1.Veg Soya Pizza
2.Veg butter Pizza
3.Egg_Pizza
Please enter u r order: 1

Very Sorry !!!! No more orders....

    ***** Welcome To Pizza Parlor *******

1.Accept order
2.Make_payment
3.Pending Orders
Enter u r choice:

 *******************************************************/

data structures and algorithms Web Developer

Sunday 18 September 2016

Infix to Postfix conversion code in c++


Group C: (Assignment No: 02) (SPPU Syllabus Assignment No: 25)
Problem Statement:
Implement C++ program for expression conversion as infix to postfix and its evaluation using stack based on given conditions
i. Operands and operator, both must be single character.
ii. Input Postfix expression must be in a desired format.
iii. Only '+', '-', '*' and '/ ' operators are expected.

Program Code:

//================================================================================================================================
// Name        : infix_postfix.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Conversion of infix expression to postfix expression and evaluation of postfix expression using stack, Ansi-style
//================================================================================================================================

#include <iostream>
#include<ctype.h>
using namespace std;
//class inpo containing data member and member functions
class inpo
{
    char in[20],po[20],stk[10];//stack for expression conversion.
    int i,j,is,ic,top,top1; //Two top one for stack and one for stack 1.
    int stk1[10]; //Stack 1 for expression evaluation.
public:
    inpo()
    {
     i=j=is=ic=0; //initialization of data members
     top=top1=-1;
    }
    bool IsOperand(char C)
    {
        if(C >= '0' && C <= '9') return true;
        if(C >= 'a' && C <= 'z') return true;
        if(C >= 'A' && C <= 'Z') return true;
        return false;
    }
    void getinfix() //Accept the infix expression
    {
        cout<<"\nEnter valid infix Expression: ";
        cin>>in; //Stored infix exp in in[]char array.
    }
    void showinfix() //Show the infix expression
    {
        cout<<"\t"<<in;
    }
    int isempty() //Stack basic functions.
    {
        if(top==-1)
            return 1;
        else
            return 0;
    }
    int isfull()
    {
        if(top==9)
            return 1;
        else
            return 0;
    }
    void push1(int x1)//for integer stack
    {
        top=top+1;
        stk1[top]=x1;
    }
    int pop1() //for integer stack
    {
       int s1;
       s1=stk1[top];
       top=top-1;
       return s1;
    }
    void push(char x)//for character stack
    {
        top=top+1;
        stk[top]=x;
    }
    char pop()//for character stack
    {
        char s;
        s=stk[top];
        top=top-1;
        return s;
    }
    void showpostfix()
    {
        cout<<"\t"<<po;
    }
    void convert();
    int instackprio();
    int incomingprio(char); //member functions
    void postfixExpEval();
};
void inpo::postfixExpEval() //To evaluate the postfix expression
{
    i=0;
    char ch;
    int op1,op2,res,tot;
        while(po[i]!='\0')//read postfix expression one by one
        {
            ch=po[i];
            if((ch=='+')||(ch=='-')||(ch=='*')||(ch=='/')||(ch=='^')) //isdigit(ch) built in function to check digit.
            {
                switch(ch)//if operator pop and perform operation and push back into the stack.
                {
                case '+':op2=pop1();
                         op1=pop1();
                         res=op1+op2;
                         push1(res);
                         break;
                case '-':op2=pop1();
                         op1=pop1();
                         res=op1-op2;
                         push1(res);
                         break;
                case '*':op2=pop1();
                         op1=pop1();
                         res=op1*op2;
                         push1(res);
                         break;
                case '/':op2=pop1();
                         op1=pop1();
                         res=op1/op2;
                         push1(res);
                         break;
                case '^':op2=pop1();
                         op1=pop1();
                         res=op1;
                         while(op2>1)
                         {
                          res=res*op1;
                          op2--;
                         }
                         push1(res);
                         break;
                }//end of switch

            } //end of if
            else if(IsOperand(ch))//To check operand we use IsOperand function.
            {
                push1(ch-'0'); //if operand push it inside stack
            } //end of else
            i=i+1;
        }//end of while

    tot=pop1(); //final evaluated result at the top of stack.
    cout<<"\nResult is:"<<tot;
}//end of fun

/*******************************************************************
 * Function Name: To convert infix expression to postfix expression.
 * return type: Void
 *******************************************************************/

void inpo::convert()
{
    i=j=0;
    char p,k;
    while(in[i]!='\0')//do until null character not found
    {
     p=in[i];//to read one by one from infix expression
     if((p=='(')||(p=='+')||(p=='-')||(p=='*')||(p=='/')||(p=='^')||(p==')'))
     {
         if(isempty()) //here we are dealing with operator only as per their priority.
         {
             push(p); //if initially stack is empty push
         }
         else if(p==')') //when we encountered with ')' bracket pop from stack until we r not encountered with '(' bracket.
         {
             k=pop();
             while(k!='(') //check the pop element with '(' bracket. if equal stop poping.
             {
                 po[j]=k; //pop and store it inside the postfix expression array po[].
                 j++;     //increment po[] array index by 1.
                 k=pop(); //pop next element
             }
         }
         else
         {
             is=instackprio(); //when we are pushing the operator inside the stack.
             ic=incomingprio(p);// we are always checking their incoming and instack priority.
             if(is>ic)//if instack priority is gretter than incoming priority
             {
              k=pop(); //pop the stack top operator whose priority is bigger than incoming operator from stack.
              po[j]=k;//store it in postfix expression array po[j]
              j++;  //increment j by one.
              push(p);//then push the incoming operator in stack.
             }
             else
             {
                 push(p); //if incoming operator priority is gretter than instack operator priority then
             }            // directly push the incoming operator inside the stack.
         }
     }
     else// if opearnd is their directly store it inside the postfix expression array po[j].
     {
        po[j]=p;
        j++; //increment j by one.
     }
     i=i+1; //read the next character of infix expression for conversion.
    }//end of while loop
    if(in[i]=='\0')//if we encountered with NULL Character of infix expression then
    {
        while(!isempty())//pop the stack containts until stack is not empty and
        {
         k=pop(); //pop from stack
         po[j]=k; //store it inside the postfix expression array po[j].
         j++;  //increment j by one.
        }
    }
 po[j]='\0'; // at the end of the postfix expression store null character to indicate end of the expression.
}

/* * Function to check the instack priority of operator * */
int inpo::instackprio()
{
    char b;
    b=stk[top];
    switch(b)
    {
     case '(':return 0; break;
     case '+':return 2; break;
     case '-':return 2; break;
     case '*':return 4; break;
     case '/':return 4; break;
     case '^':return 5; break;
    }
}
/* * Function to check the priority of incoming operator * */
int inpo::incomingprio(char ch)
{
    switch(ch)
    {
     case '(':return 9; break;
     case '+':return 1; break;
     case '-':return 1; break;
     case '*':return 3; break;
     case '/':return 3; break;
     case '^':return 6; break;
    }
}

int main()
{
    inpo p1;
    p1.getinfix();
    p1.showinfix();
    cout<<"\nAfter conversion from infix to postfix...\n";
    p1.convert();
    p1.showpostfix();
    cout << "\n\n!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!" << endl;
    p1.postfixExpEval();
    return 0;
}

/************** OUTPUT***********************************************************
 *
 Enter valid infix Expression: 2^4
    2^4
After conversion from infix to postfix...
    24^

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:16

Enter valid infix Expression: (9-1)/(4-2)^2
    (9-1)/(4-2)^2
After conversion from infix to postfix...
    91-42-2^/

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:2

[student@localhost SE_Comp_A]$ g++ conv_in_po_eval.cpp
[student@localhost SE_Comp_A]$ ./a.out

Enter valid infix Expression: (8*6)/(4-2)^3
    (8*6)/(4-2)^3
After conversion from infix to postfix...
    86*42-3^/

!!!POSTFIX EXPRESSION EVALUATION ARE AS FOLLOWS..!!!

Result is:6

 ***********************************************************************************/
data structures and algorithms Web Developer

Linked list program to store negative and positive number


Group B: (Assignment No:04)(SPPU Syllabus assignment No :18)
Problem Statement: Write C++ program to store set of negative and positive numbers using linked list. Write functions to 
a) Insert numbers
b) Delete nodes with negative numbers
c) Create two more linked lists using this list, one containing all positive numbers and other containing negative numbers
d) For two lists that are sorted; Merge these two lists into third resultant list that is sorted.

Program Code:
//Author: Prof.Anjali Almale
#include<iostream>
using namespace std;
struct node   //node defined to allocate memory
{
    int num; //To store data
    node *next; //To store address of next node.
};

class numbers  //class name is number
{
 public:

    node *head,*head1,*head2; //data memebers

    numbers() //default constructor
    {
        head=NULL;
        head1=NULL;
        head2=NULL;
    }
    void create();       //member function declaration.
    void display(node *);
    void insert();
    void sort(node *);
    void display2();
    void remove();
    void seperate();
    void merge();
}; //end of class

void numbers::create()
{

  node *current,*new_node;
    char c;
 cout<<"\n------------CREATION OF NUMBERS LIST-------------\n\n ";
    do
    {

    new_node=new node; //dynamic memory allocation
    cout<<"\n Enter the no: \n ";
    cin>>new_node->num; //To store data

    new_node->next=NULL; //make node next null(init)
    if(head==NULL) //check header if null
    {
        head=new_node;
        current=new_node;
    }
    else
    {
        current->next=new_node;
        current=new_node;

    }
    cout<<"\nDo you want to add new member";
    cin>>c;
       }while(c!='n');
}
void numbers::seperate()//To separate the positive numbers and negative numbers.
{
    node *temp,*current1,*current2,*n1,*n2;
    temp=head;

    while(temp!=NULL)
    {
        if((temp->num)<0)
        {
            n1=new node;
            n1->num=temp->num;
            n1->next=NULL;

            if(head1==NULL)
            {
                current1=n1;
                head1=n1;
            }
            else
            {
            current1->next=n1;
            current1=n1;
            }

        }
        else if((temp->num)>=0)
        {
            n2=new node;
            n2->num=temp->num;
            n2->next=NULL;
            if(head2==NULL)

            {
                current2=n2;
                head2=n2;
            }
            else
            {
              current2->next=n2;
              n2->next=NULL;
              current2=n2;
            }
        }
          temp=temp->next;
     }
}

void numbers::display(node *head)//To display the list
{
    node *p;
    p=head;
    if(p==NULL)
    {
        cout<<"\nThe list is Empty";
    }
    else
    {
        while(p!=NULL)
        {
            cout<<p->num<<"\t";
             p=p->next;
        }
    }
}

void numbers::sort(node *head_d) //Before merge u hv to sort them
{
    node *temp1,*temp2,*temp3;
    temp1 = head_d;

 for( ;temp1->next!=NULL;temp1=temp1->next)
 {
     for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
     {
         if(temp1->num>temp2->num)
         {
             int temp= temp1->num;
             temp1->num = temp2->num;
             temp2->num = temp;
         }
     }
 }
 temp3 = head_d;

while (temp3!=NULL)
{
cout<<"\t"<< temp3->num;
temp3 =temp3->next;
}
}

void numbers::merge()//To merge both the list
{
    node *temp;
    temp=head1;

    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    temp->next=head2;
}
void numbers::insert()
{
    node *p,*temp;
        p=new node;
        cout<<"\n\n Enter New Number to be insert  :  ";
        cin>>p->num;
        p->next=NULL;
        temp=head;
        while(temp->next!=NULL)
        {
    temp=temp->next;
        }
    temp->next=p;
}

int main()
{
    numbers n;
    n.create();
    cout<<"\n \nTHE LIST OF +VE AND -VE NUMBERS IS: \n";
    n.display(n.head);
    n.insert();
    cout<<"\n \nTHE LIST AFTER INSERTION IS: \n";
    n.display(n.head);
    n.seperate();
    cout<<"\n\n THE LIST OF ONLY -VE NUMBERS IS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF ONLY +VE NUMBERS IS: \n";
    n.display(n.head2);
    cout<<"\n\n THE LIST OF ONLY SORTED -VE NUMBERS IS : \n";
    n.sort(n.head1);
    cout<<"\n\n THE LIST OF ONLY SORTED +VE NUMBERS IS : \n";
    n.sort(n.head2);
    n.merge();
    cout<<"\n AFTER MERGE .....THE LIST IS AS FOLLOWS: \n";
    n.display(n.head1);
    cout<<"\n\n THE LIST OF SORTED +VE AND -VE NUMBERS IS : \n";
    n.display(n.head1);
    return 0;
}

/*   OUTPUT *************************************************************


------------CREATION OF NUMBERS LIST-------------


 Enter the no:
 1

Do you want to add new membery

 Enter the no:
 -5

Do you want to add new membery

 Enter the no:
 6

Do you want to add new membery

 Enter the no:
 -7

Do you want to add new membery

 Enter the no:
 -3

Do you want to add new membery

 Enter the no:
 -4

Do you want to add new membery

 Enter the no:
 7

Do you want to add new membern


THE LIST OF +VE AND -VE NUMBERS IS:
1    -5    6    -7    -3    -4    7

 Enter New Number to be insert  :  10


THE LIST AFTER INSERTION IS:
1    -5    6    -7    -3    -4    7    10

 THE LIST OF ONLY -VE NUMBERS IS:
-5    -7    -3    -4

 THE LIST OF ONLY +VE NUMBERS IS:
1    6    7    10

 THE LIST OF ONLY SORTED -VE NUMBERS IS :
    -7    -5    -4    -3

 THE LIST OF ONLY SORTED +VE NUMBERS IS :
    1    6    7    10
 AFTER MERGE .....THE LIST IS AS FOLLOWS:
-7    -5    -4    -3    1    6    7    10

 THE LIST OF SORTED +VE AND -VE NUMBERS IS :
-7    -5    -4    -3    1    6    7    10

********************************************************************************************/
data structures and algorithms Web Developer

Tuesday 6 September 2016

C++ program using stack to check whether given expression is well parenthesized or not.


Group C: Assignment No: 01 (SPPU Syllabus Assignment No:24)

Problem Statement:
In any language program mostly syntax error occurs due to unbalancing delimiter such as (),{},[]. Write C++ program using stack to check whether given expression is well parenthesized or not.


Program Code:

//============================================================================
// Name        : wellformness.cpp
// Author      : Nitin Shivale
// Version     : 1
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#define size 10

class stackexp
{
    int top;
    char stk[size];
public:
    stackexp()
    {
     top=-1;
    }
    void push(char);
    char pop();
    int isfull();
    int isempty();
};

void stackexp::push(char x)
{
    top=top+1;
    stk[top]=x;
}

char stackexp::pop()
{
    char s;
    s=stk[top];
    top=top-1;
    return s;
}

int stackexp::isfull()
{
    if(top==size)
        return 1;
    else
        return 0;
}

int stackexp::isempty()
{
    if(top==-1)
        return 1;
    else
        return 0;
}

int main()
{
    stackexp s1;
    char exp[20],ch;
    int i=0;
    cout << "\n\t!!Well Formness of Parenthesis..!!!!" << endl; // prints !!!Hello World!!!
    cout<<"\nEnter the expression to check whether it is in well form or not :  ";
    cin>>exp;
    if((exp[0]==')')||(exp[0]==']')||(exp[0]=='}'))
    {
        cout<<"\n Invalid Expression.....\n";
        return 0;
    }
    else
    {
        while(exp[i]!='\0')
        {
            ch=exp[i];
            switch(ch)
            {
            case '(':s1.push(ch);break;
            case '[':s1.push(ch);break;
            case '{':s1.push(ch);break;
            case ')':s1.pop();break;
            case ']':s1.pop();break;
            case '}':s1.pop();break;
            }
            i=i+1;
        }
    }
    if(s1.isempty())
    {
        cout<<"\nExpression is well parenthesis...\n";
    }
    else
    {
        cout<<"\nSorry !!! Invalid Expression or not in well parenthesized....\n";
    }
    return 0;
}

/******************* output   *****************************************************

  !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m<(n[8]+0))

Expression is well parenthesis...


!!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  )(m+n)*(a-b)

 Invalid Expression.....



     !!Well Formness of Parenthesis..!!!!

Enter the expression to check whether it is in well form or not :  (m+b(n[8)/2+3)

Sorry !!! Invalid Expression or not in well parenthesized....


*********************************************************************************/


data structures and algorithms Web Developer