#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<process.h>
#define MAX 50
void queu();
void stck();
void expre();
void arry();
void large();
void small();
void secondl();
void seconds();
void search();
void bubble();
void inseration(int []);
void inseration_m();
void merge();
void merge_sort (int a[],int ,int);
void merge_array (int a[],int,int,int);
void insert();
void delt();
void display();
int queue[MAX];
int rear= -1,front= -1,item;
void push();
void PoP();
void disply();
int stack[MAX];
int top,itm;
void infix();
void main()
{ char ch;
clrscr();
cout<<"\t\t << MAIN MENU >> \n";
cout<<"\n\t\t (A) Array program ";
cout<<"\n\t\t\t---------------------------------";
cout<<"\n\t\t (B) Queue using array ";
cout<<"\n\t\t\t---------------------------------";
cout<<"\n\t\t (C) Stack using array ";
cout<<"\n\t\t\t---------------------------------";
cout<<"\n\t\t (D) EXPRESION ";
cout<<"\n\t\t\t--------------------------------- \n ";
cout<<"\n\t\t (X) EXit ";
cout<<"\n\n\t\t\t >>> Enter your Option ";
cin>>ch;
switch(ch)
{
case 'a':
case 'A':
{
arry();
}
break;
case 'b':
case 'B':
{
queu();
}
break;
case 'c':
case 'C':
{
stck();
}
break;
case 'd':
case 'D':
{
expre();
}
break;
case 'x':
case 'X':
{
exit(0);
}
default:
{
cout<<"\n\t\t\t Invalid choice !(please try again)";
getch();
main();
}
}
getch();
}
void arry()
{
clrscr();
int choice,sm,sort;
cout<<"\n\t\t | You Have Selected Array Menu| ";
cout<<"\n\n\t\t (1) Searching ";
cout<<"\n\n\t\t (2) Sorting";
cout<<"\n\n\t\t (3) Back to Main Menu ";
cin>>choice;
switch(choice)
{
case 1:
{
do
{
clrscr();
cout<<"\n\t\t *********** | You are in Searching Menu| *********** ";
cout<<"\n\t\t *1* . Large No";
cout<<"\n\t\t *2* . Small No";
cout<<"\n\t\t *3* . Second largest No";
cout<<"\n\t\t *4* . Second Small No";
cout<<"\n\t\t *5* . Search element in array \n\n";
cout<<"\n\t\t *6* . Back to Array Menu ";
cout<<"\n\t\t *7* . Back to Main Menu ";
cin>>sm;
switch(sm)
{
case 1:
{
large();
}
break;
case 2:
{
small();
}
break;
case 3:
{
secondl();
}
break;
case 4:
{
seconds();
}
break;
case 5:
{
search();
}
break;
case 6:
{
arry();
}
break;
case 7:
{
main();
}
break;
default:
{
cout<<"\n\t\t Invalid choice ! (please try agian)";
getch();
}
}
}
while(1);
}
case 2:
{
do
{
clrscr();
cout<<"\n\t\t | You are in Sorting Menu | ";
cout<<"\n\t\t 1 : Bubble Sorting ";
cout<<"\n\t\t 2 : Inseration Sorting";
cout<<"\n\t\t 3 : Merge Sorting \n\n";
cout<<"\n\t\t 4 : Back to Array Menu ";
cout<<"\n\t\t 5 : Back to Main Menu ";
cin>>sort;
switch(sort)
{
case 1:
{
bubble();
}
break;
case 2:
{
inseration_m();
}
break;
case 3:
{
merge();
}
break;
case 4:
{
arry();
}
break;
case 5:
{
main();
}
break;
default:
{
cout<<"\n\t\t\ Invalid choice !(please try again)";
getch();
}
}
}while(1);
}
case 3:
{
main();
}
break;
default:
{
cout<<"\n\t\t Invalid choice !(please try again)";
getch();
arry();
}
}
}
void queu()
{
clrscr();
int que;
cout<<"\n\t\t | You are in Queue Menu | ";
cout<<"\n\t\t 1 : Insert value in Queue:";
cout<<"\n\t\t 2 :Delete value in Queue:";
cout<<"\n\t\t 3 : Display value in Queue:\n\n ";
cout<<"\n\t\t 4 : Back to Main Menu: ";
cout<<"\n\t\t";
display();
cin>>que;
switch(que)
{
case 1:
{
insert();
queu();
}
break;
case 2:
{
delt();
queu();
}
break;
case 3:
{
display();
queu();
}
break;
case 4:
{
main();
}
break;
default:
{
cout<<"\n\t\t\ Invalid choice !(please try again)";
getch();
}
}
}
void stck()
{
clrscr();
int stm;
cout<<"\n\t\t | You are in Stack Menu | ";
cout<<"\n\t\t 1 : Push value in Stack : ";
cout<<"\n\t\t 2 : POP value in stack : ";
cout<<"\n\t\t 3 : Display value in Stack :";
cout<<"\n\n\n\t\t4 : Back to Main Menu ";
cout<<"\n\t";
disply();
cin>>stm;
switch(stm)
{
case 1:
{
push();
stck();
}
break;
case 2:
{
PoP();
stck();
}
break;
case 3:
{
disply();
stck();
}
break;
case 4:
{
main();
}
break;
default:
{
cout<<"\n\t\t\ Invalid choice !(please try again)";
getch();
stck();
}
}
}
void expre()
{
clrscr();
int ex;
cout<<"\n\t\t |You are in Expression Menu | ";
cout<<"\n\n\t\t 1 : Infix to Postfix Convert ";
cout<<"\n\n\t\t 2 : Back to Main Menu ";
cin>>ex;
switch(ex)
{
case 1:
{
infix();
expre();
}
break;
case 2:
{
main();
}
break;
default:
{
cout<<"\n\t\t Invalid choice !(Please try again)";
getch();
expre();
}
}
}
void large()
{
{
clrscr();
int a[7];
int i,largest;
cout<<"\n\t\t YOU HAVE SELECT TO FIND A LARGEST NO \n\n\t\tEnter the elements of array";
for(i=0;i<6;i++)
cin>>a[i];
largest=a[0];
for(i=0;i<6;i++)
if(a[i] > largest)
largest=a[i];
gotoxy(65,9);cout<<" ";
gotoxy(65,9);cout<<" **^** "; cout<<"\n\t\t<**><**><**> largest no is <**><**><**> ";
gotoxy(65,10);cout<<" "<<largest;
gotoxy(65,12);cout<<" ****";
}
getch();
}
void small()
{
{
clrscr();
int a[7];
int i,smallest;
cout<<"\n\t\t YOU HAVE SELECT TO FIND A Smallest NO \n\n\t\tEnter the elements of array";
for(i=0;i<6;i++)
cin>>a[i];
smallest=a[0];
for(i=0;i<6;i++)
if(a[i] < smallest)
smallest=a[i];
gotoxy(65,9);cout<<" ";
gotoxy(65,9);cout<<" **^** "; cout<<"\n\t\t<**><**><**> Smallest no is <**><**><**> ";
gotoxy(65,10);cout<<" "<<smallest;
gotoxy(65,12);cout<<" ****";
}
getch();
}
void secondl()
{
{
clrscr();
int a[50],size,i,j=0,big,secondbig;
cout<<"\n\t\t YOU HAVE SELECT TO FIND A Second Largest NO \n\n\t\tEnter the size of array";
cin>>size;
cout<<"\n\t\tEnter elements in to the array: "<<size;
for(i=0;i<size;i++)
cin>>a[i];
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
{
big=a[i];
j = i;
}
}
secondbig=a[size-j-1];
for(i=1;i<size;i++)
{
if(secondbig <a[i] && j != i)
secondbig =a[i];
}
gotoxy(65,9);cout<<" ";
gotoxy(65,9);cout<<" **^** "; cout<<"\n\t\t<**><**><**>Second Largest no is <**><**><**> ";
gotoxy(65,10);cout<<" "<<secondbig;
gotoxy(65,12);cout<<" ****";
}
getch();
}
void seconds()
{
{
clrscr();
int a[50],size,i,j=0,sml,secondsml;
cout<<"\n\t\t YOU HAVE SELECT TO FIND A Second Smallest NO \n\n\t\tEnter the size of array";
cin>>size;
cout<<"\n\t\tEnter elements in to the array: "<<size;
for(i=0;i<size;i++)
cin>>a[i];
sml=a[0];
for(i=1;i<size;i++)
{
if(sml<a[i])
{
sml=a[i];
j = i;
}
}
secondsml=a[size-j-1];
for(i=1;i<size;i++)
{
if(secondsml <a[i] && j != i)
secondsml =a[i];
}
gotoxy(65,9);cout<<" ";
gotoxy(65,9);cout<<" **^** "; cout<<"\n\t\t<**><**><**>Second Smallest no is <**><**><**> ";
gotoxy(65,10);cout<<" "<<secondsml;
gotoxy(65,12);cout<<" ****";
}
getch();
}
void search()
{
clrscr();
int a[7];
int item,i;
cout<<"\n\t\t YOU HAVE SELECTED OPTION TO SEARCH NO \n\n\t\t\t Enter the element of array ";
for(i=0;i<6;i++)
cin>>a[i];
cout<<"\n\t\t\tEnter the element to search";
cin>>item;
if(item==a[i])
cout<<"\n\t\t Item is found ";
getch();
}
void bubble()
{
{
clrscr();
int a[50], n, c, d, swap;
cout<<"\n\t\t******YOU HAVE SELECTED OPTION Bubble Sorting ****** \n\n\t\t\t Enter the Size of array ";
cin>>n;
cout<<"\t\t\tEnter the element of array";
for (c=0;c<n; c++)
cin>>a[c];
for (c=0;c<(n - 1); c++)
{
for (d=0;d<n-c-1; d++)
{
if (a[d] > a[d+1]) /* For decreasing order use < */
{
swap = a[d];
a[d] = a[d+1];
a[d+1] = swap;
}
}
}
cout<<"\n\n\t\t <***><***> Sorted list in ascending order:<***><***>\n\n\t\t";
for (c=0;c<n; c++ )
cout<<"\t"<<"["<<a[c]<<"]"<<" ";
}
getch();
}
void inseration_m()
{
clrscr();
int a[7],i;
cout<<"\n\t\tEnter the element of array";
for(i=0;i<6;i++)
cin>>a[i];
inseration(a);
cout<<"\n\n\t\t <***><***> Sorted list in ascending order:<***><***>\n\n\t\t";
for(i=0;i<6;i++)
cout<<" "<<"["<<a[i]<<"]"<<" ";
getch();
}
void inseration(int a[])
{
int j,k,temp;
for(j=1;j<6;j++)
{
temp=a[j];
k=j-1;
while(k>=0 && (a[k]>temp))
{
a[k+1]=a[k];
k--;
}
a[k+1]=temp;
}
getch();
}
void merge()
{
clrscr();
int a[10],i,n;
cout<<"enter the size of array";
cin>>n;
cout<<"enter the element of array";
for(i=0;i<n;i++)
cin>>a[i];
merge_sort(a,0,n-1);
cout<<"ather merge sorting";
for(i=0;i<n;i++)
cout<<" "<<"["<< a[i]<<"]"<<" ";
getch();
}
void merge_sort(int a[],int beg, int end)
{
int mid;
if(beg < end)
{
mid=(beg+end)/2;
merge_sort(a,beg,mid);
merge_sort(a,mid+1,end);
merge_array(a,beg,mid,end);
}
}
void merge_array(int a[],int beg,int mid,int end)
{
int i,num,temp,j,k,b[10];
for(i=beg;i<=end;i++)
b[i]=a[i];
i=beg;
j=mid+1;
k=beg;
while((i<=mid) && (j<=end))
{
if(b[i]<=b[j])
{
a[k]=b[i];
i++;
k++;
}
else
{
a[k]=b[j];
j++;
k++;
}
}
if(i<=mid)
{
while(i<=mid)
{
a[k]=b[i];
i++;
k++;
}
}
else
{
while(j<=end)
{
a[k]=b[j];
j++;
k++;
}
}
}
void insert()
{
if(rear==MAX-1)
cout<<"\n\n queue is full";
else
{
cout<<"\n\n enter item";
cin>>item;
if(rear==-1 && front==-1)
{
rear=0;
front=0;
}
else
rear++;
queue[rear]=item;
cout<<"\n\n item inserted:"<<" "<<"["<<item<<"]"<<" ";
}
getch();
}
void delt()
{
if(front== -1)
cout<<"\n\n queue is empty";
else
{
item=queue[front];
if(front== rear)
{
front= -1;
rear= -1;
}
else
front++;
cout<<"\n\n item is deleted:"<<" "<<"["<<item <<"]"<<" ";
}
getch();
}
void display()
{
int i;
if(front== -1)
cout<<"\n\n queue is empty";
else
{
cout<<"\n\n";
for(i=front;i<=rear;i++)
cout<<" "<<"["<<queue[i]<<"]"<<" ";
}
getch();
}
void push()
{
int i;
if(top==MAX)
cout<<"\n\n\t\t stack is full";
else
{
cout<<"\n\n\t\t enter item";
cin>>itm;
top++;
stack[top]=itm;
cout<<"\n\n\t\t item inserted:"<<"["<<itm<<"]";
}
getch();
}
void PoP()
{
if(top== 0)
cout<<"\n\n\t\t stack is empty";
else
{
itm=stack[top];
top=top-1;
cout<<"\n\n\t\t item is deleted:"<<"["<<itm<<"]";
}
getch();
}
void disply()
{
int i;
if(top== 0)
cout<<"\n\n\t\t queue is empty";
else
{
cout<<"\n\n\t\t";
for(i=top;i>0;i--)
cout<<" "<<"["<<stack[i]<<"]"<<" ";
}
getch();
}
void infix()
{
clrscr();
cout<<"\n";
cout<<"\n\t\tInfix to Postfix Conversion";
cout<<"\n";
char infix[30],postfix[30],stack[30],op;
int i=0,pc=0,sc=0,flag=0;
cout<<"\n\t\tEnter the expression in infix:\n\n";
gets(infix);
for(i=0;infix[i]!='\0';i++)
{
if(infix[i]>=97 && infix[i]<=122)
{
postfix[pc]=infix[i];
pc++;
}
else
{
op=infix[i];
if(sc==0)
{
stack[sc]=op;
sc++;
}
else
{
switch(op)
{
case '+': flag=0;
while(flag==0 && sc!=0)
{
if(stack[sc-1]=='+'|| stack[sc-1]=='*' || stack[sc-1]=='/')
{
postfix[pc]=stack[sc-1];
pc++;
sc--;
stack[sc]='+';
}
else
{
stack[sc]='+';
flag=1;
}
}
sc++;
break;
case '-':
flag=0;
while(flag==0 && sc!=0)
{
if(stack[sc-1]=='+'||stack[sc-1]=='-'||stack[sc-1]=='*'||stack[sc-1]=='/')
{
postfix[pc]=stack[sc-1];
pc++;
sc--;
stack[sc]='-';
}
else
{
stack[sc]='-';
flag=1;
}
}
sc++;
break;
case '*': flag=0;
while(flag==0 && sc!=0)
{
if(stack[sc-1]=='/'||stack[sc-1]=='*')
{
postfix[pc]=stack[sc-1];
pc++;
sc--;
stack[sc]='*';
}
else
{
stack[sc]='*';
flag=1;
}
}
sc++;
break;
case '/':
flag=0;
while(flag==0 && sc!=0)
{
if(stack[sc-1]=='/')
{
postfix[pc]=stack[sc-1];
pc++;
sc--;
stack[sc]='/';
}
else
{
stack[sc]='/';
flag=1;
}
sc++;
break;
}
}
}
}
}
for(i=sc-1;i>=0;i--)
{
postfix[pc]=stack[i];
pc++;
}
postfix[pc]='\0';
cout<<"postfix expression:\t";
for(i=0;postfix[i]!='\0';i++)
{
cout<<postfix[i];
}
getch();
}
No comments:
Post a Comment