Thursday, 12 October 2017

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.
---------------------------------------------------------------------------------

#include<iostream>
//Author : Vivek S. Sharma
//Copyright : Your copyright notice
using namespace std;
class stack
{                        
 public:
  char stack_array[50];
  int top;
  stack()
  {
    top=-1;
  }
  void push(char symbol)
  {
    if(full())
    {
      cout<<"\nStack overflow:\n";
    }
    else
    {
      top=top+1;
      stack_array[top]=symbol;
     }
   }
   char pop()
   {
     if(empty())
       return('#');
     else
       return(stack_array[top--]);
   }
   int empty()
   {
     if(top==-1)
       return(1);
     else
       return(0);
   }
   int full()
   {
     if(top==49)
       return(1);
     else
       return(0);
   }
};
class Expression
{
  char infix[50];
  char postfix[50];
  public:
    void read()
    {
      cout<<"\nEnter an infix expression:";
      cin>>infix;
    }
    int white_space(char symbol)
    {
      if(symbol==' ' || symbol=='\t' || symbol=='\0')
     return 1;
      else
     return 0;
    }
    void ConvertToPostfix()
    {
      stack s;
      int l,precedence,p;
      char entry1,entry2;
      p=0;
      for(int i=0;infix[i]!='\0';i++)

      {
    entry1=infix[i];
    if(!white_space(entry1))
    {
      switch(entry1)
      {
        case '(':
          s.push(entry1);
          break;
        case ')':
          while((entry2=s.pop())!='(')
          postfix[p++]=entry2;
          break;
        case '+':
        case '-':
        case '*':
        case '/':
          if(!s.empty())
          {
        precedence=prec(entry1);
        entry2=s.pop();
        while(precedence<=prec(entry2))
        {
           postfix[p++]=entry2;
           if(!s.empty())
              entry2=s.pop();
           else
              break;
        }
        if(precedence>prec(entry2))
           s.push(entry2);
          }
          s.push(entry1);
          break;
        default:
          postfix[p++]=entry1;
          break;
      }
      }
    }
    while(!s.empty())
       postfix[p++]=s.pop();

    postfix[p]='\0';
    cout<<"\nThe postfix expression is: "<<postfix<<endl;
  }
  int prec(char symbol)
  {
    switch(symbol)
    {
      case '/': return(4);
      case '*': return(3);
      case '+': return(2);
      case '-': return(1);
      case '(': return(0);
      default: return(-1);
    }
  }
};
int main()
{

 Expression expr;

   expr.read();
   expr.ConvertToPostfix();



}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma
Write C++ program to store first year percentage of students in array. Sort array of floating point numbers in ascending order using quick sort and display top five scores
---------------------------------------------------------------------------------

#include <iostream>
//Author : Vivek S. Sharma

//Copyright : Your copyright notice
using namespace std;
void quick_sort(int[],int,int);
int partition(int[],int,int);
void top_five(int[],int);

int main()
{
    int a[50],n,i;
    cout<<"Enter number of students : ";
    cin>>n;
    cout<<"\nEnter first year percentage :";

    for(i=0;i<n;i++)
        cin>>a[i];

    int ch;
    do{
      cout<<"\n\nEnter choice : \n1.Quick Sort \n2.Top five scores \n3.Exit";
      cin>>ch;
      switch(ch){
            case 1:quick_sort(a,0,n-1);
          cout<<"\nPercentage after sorting:";

          for(i=0;i<n;i++)
              cout<<a[i]<<" ";
          break;

            case 2: top_five(a,n);break;
      };
    }while(ch!=3);


    return 0;
}

void quick_sort(int a[],int l,int u)
{
    int j;
    if(l<u)
    {
        j=partition(a,l,u);
        quick_sort(a,l,j-1);
        quick_sort(a,j+1,u);
    }
}

int partition(int a[],int l,int u)
{
    int v,i,j,temp;
    v=a[l];
    i=l;
    j=u+1;

    do
    {
        do
            i++;

        while(a[i]<v&&i<=u);

        do
            j--;
        while(v<a[j]);

        if(i<j)
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }while(i<j);

    a[l]=a[j];
    a[j]=v;

    return(j);
}
void top_five(int a[],int n){
for(int i=n-1 ; i>=n-5 ; i--){
      cout<<"\n "<<a[i];
}
}
--------------------------------------------------------------------------------------------------------------------------



**If some mistakes, plz let me know!-----Vivek S. Sharma