Tuesday, 9 June 2015

Program to illustrate the implementation of arrays as a Linear Queue ( in graphics )



Code for Program to illustrate the implementation of arrays as a Linear Queue ( in graphics ) in C++ Programming



 



 



 
 #include<iostream.h>
 #include<graphics.h>
 #include<stdlib.h>
 #include<conio.h>
 #include<math.h>
 #include<dos.h>

 const max_length=10;

 class Queue
    {
       private:
        long queue[max_length];
        int flag[max_length];
        int rear;
        int front;

       public:
        Queue( );
        void Delete( );
        void Insert( );
        void waiting( );
        long get_value( );
        void show_main_screen( );
        void show_working( );
    };



 /*************************************************************************///--------------------------  Queue( )  ---------------------------------///*************************************************************************/void Queue::Queue( )
    {
       rear=-1;
       front=-1;

       for(int count=0;count<max_length;count++)
      {
         queue[count]=0;
         flag[count]=0;
      }
    }

 /*************************************************************************///----------------------  get_value( )  ---------------------------------///*************************************************************************/long Queue::get_value( )
    {
       int count=0;

       char String[10]={NULL};

       setcolor(11);
       settextstyle(2,0,7);
     outtextxy(40,250,"Enter the element : ");
     outtextxy(41,250,"Enter the element : ");

       do
      {
         int key_code=0;

         char key=NULL;

         if(kbhit( ))
        {
           key=getch( );
           key_code=int(key);
        }

         if( (count>0 && count<5) && key_code==13)
        break;

         elseif( (key_code>=48 && key_code<=57 || key_code==46) &&
                                     count<4)
        {
           String[count]=key;

           count++;
        }

         elseif(key_code==8 && count>0)
        {
           setfillstyle(10,6);
             bar(260,255,320,275);

           count--;
           String[count]=NULL;
        }

         setcolor(11);
         settextstyle(2,0,6);

         moveto(265,255);
           outtext(String);

         moveto(265,255);
           outtext(String);

         int x=getx( );
         int y=gety( )+9;

         while(!kbhit( ))
        {
           settextstyle(0,0,1);
             setcolor(15);
               moveto(x+2,y);
             outtext("_");

           delay(250);

             setcolor(0);
               moveto(x+2,y);
             outtext("_");

           delay(200);
           }
      }
       while(count<6);

       delay(500);

       setfillstyle(10,6);
     bar(35,255,320,275);

       long number=atol(String);

       return number;
    }

 /*************************************************************************///-------------------------------  Insert( )  ---------------------------///*************************************************************************/void Queue::Insert( )
    {
       long item=get_value( );

       if(rear==max_length-1)
      {
         setcolor(11);
         settextstyle(1,0,4);
           outtextxy(30,250,"Queue is Full");

         delay(1000);

         setfillstyle(10,6);
           bar(30,260,300,290);
      }

       elseif(rear==-1 && front==-1)
      {
         rear++;
         queue[rear]=item;
         front=rear;
         flag[rear]=1;
      }

       else
      {
         rear++;
         queue[rear]=item;
         flag[rear]=1;
      }
    }

 /*************************************************************************///---------------------------  Delete( )  -------------------------------///*************************************************************************/void Queue::Delete( )
    {
       if(rear==-1 && front==-1)
      {
         setcolor(11);
         settextstyle(1,0,4);
           outtextxy(30,250,"Queue is Empty");

         delay(1000);

         setfillstyle(10,6);
           bar(30,260,300,290);
      }

       else
      {
         queue[front]=0;
         flag[front]=0;

         if(front==rear)
        front=rear=-1;

         else
        front++;
      }
    }

 /*************************************************************************///-----------------------  show_main_screen( )  -------------------------///*************************************************************************/void Queue::show_main_screen( )
    {
       for(int count_1=0;count_1<5;count_1++)
      {
         setcolor(7);
           rectangle(count_1,count_1,getmaxx( )-count_1,
                             getmaxy( )-count_1);
      }

       setfillstyle(10,6);
     bar(5,5,getmaxx( )-5,getmaxy( )-5);

       setfillstyle(1,7);
     bar(80,60,550,63);

       settextstyle(7,0,3);
     setcolor(8);
       outtextxy(108,12,"Linear");
       outtextxy(109,12,"Linear");

     setcolor(10);
       outtextxy(110,10,"Linear");
       outtextxy(111,10,"Linear");

       settextstyle(1,0,5);
     setcolor(8);
       outtextxy(78,17,"Queue Implementation");
       outtextxy(79,17,"Queue Implementation");

     setcolor(12);
       outtextxy(80,15,"Queue Implementation");
       outtextxy(81,15,"Queue Implementation");

       settextstyle(1,0,5);
     setcolor(8);
       outtextxy(418,382,"Queue");
       outtextxy(419,382,"Queue");

     setcolor(15);
       outtextxy(420,380,"Queue");
       outtextxy(421,380,"Queue");

       settextstyle(7,0,3);
     setcolor(8);
       outtextxy(448,377,"Linear");
       outtextxy(449,377,"Linear");

     setcolor(14);
       outtextxy(450,375,"Linear");
       outtextxy(451,375,"Linear");

       settextstyle(8,0,4);
     setcolor(8);
       outtextxy(18,82,"Press :");
       outtextxy(19,82,"Press :");

     setcolor(3);
       outtextxy(20,80,"Press :");
       outtextxy(21,80,"Press :");

       setfillstyle(2,9);
     bar(16,118,135,121);

       setcolor(14);
       settextstyle(2,0,6);
     outtextxy(50,130,"<I> to Insert an element");
     outtextxy(51,130,"<I> to Insert an element");

     outtextxy(50,150,"<D> to Delete an element");
     outtextxy(51,150,"<D> to Delete an element");

     outtextxy(50,170,"<E> to Exit");
     outtextxy(51,170,"<E> to Exit");

       setcolor(11);
     outtextxy(60,130,"I");
     outtextxy(61,130,"I");

     outtextxy(60,150,"D");
     outtextxy(61,150,"D");

     outtextxy(60,170,"E");
     outtextxy(61,170,"E");

       setcolor(7);
       settextstyle(0,0,1);
     outtextxy(430,452,"* CopyRights (C) 2000-02");
     outtextxy(430,465,"* Muhammad Tahir Shahzad");

       setcolor(15);
     rectangle(429,79,531,379);

       setfillstyle(1,0);
     bar(430,80,530,378);

       charstring[10][10]={"Queue[9]","Queue[8]","Queue[7]","Queue[6]",
                "Queue[5]","Queue[4]","Queue[3]","Queue[2]",
                "Queue[1]","Queue[0]"};

       for(int count_2=0;count_2<max_length;count_2++)
      {
         setcolor(15);
           line(429,79+(count_2*30),530,79+(count_2*30));

         setcolor(15);
         settextstyle(0,0,1);
           outtextxy(355,92+(count_2*30),string[count_2]);
      }
    }

 /*************************************************************************///-------------------------  show_working( )  ---------------------------///*************************************************************************/void Queue::show_working( )
    {
       show_main_screen( );

       int key_code=0;

       do
      {
         waiting( );

         char key='\0';

         key=getch( );
         key_code=int(key);

         if(key_code==105 || key_code==73)
        Insert( );

         elseif(key_code==100 || key_code==68)
        Delete( );

         elseif(key_code==27 || key_code==101)
        exit(0);

         setfillstyle(10,6);
           bar(540,80,615,400);

         for(int count_1=0;count_1<max_length;count_1++)
        {
           int flag=0;

           if(count_1==rear && flag==0)
              {
             setfillstyle(1,9);
               bar(550,358-(count_1*30),570,363-(count_1*30));

             setcolor(9);

             for(int count_2=0;count_2<=10;count_2++)
                   line(540,363-(count_1*30),550,
                        363-(count_1*30)-count_2);

             setcolor(7);
             settextstyle(0,0,1);
               outtextxy(577,357-(count_1*30),"Rear");

             flag=1;
              }

           flag=0;

           if(count_1==front && flag==0)
              {
             setfillstyle(1,11);
               bar(550,368-(count_1*30),570,373-(count_1*30));

             setcolor(11);

             for(int count_3=0;count_3<=10;count_3++)
                line(540,368-(count_1*30),550,
                          368-(count_1*30)+count_3);

             setcolor(7);
             settextstyle(0,0,1);
               outtextxy(577,369-(count_1*30),"Front");

             flag=1;
              }
        }

         delay(500);

         for(int count_4=0;count_4<max_length;count_4++)
        {
           if(flag[count_4]!=0)
              setfillstyle(1,count_4+1);

           else
              setfillstyle(1,0);

            bar(430,378-(count_4*30),530,350-(count_4*30));
        }

         for(int count_5=0;count_5<max_length;count_5++)
        {
           setcolor(15);
           settextstyle(0,0,1);

           charvalue[6]={'\0'};

           itoa(queue[count_5],value,10);

           if(flag[count_5]!=0)
              outtextxy(460,360-(count_5*30),value);
        }
      }
       while(key_code!=27);
    }

 /*************************************************************************///----------------------------  waiting( )  -----------------------------///*************************************************************************/void Queue::waiting( )
    {
    do
       {
          setfillstyle(1,4);
        bar(51,440,99,445);

          setfillstyle(1,10);
        bar(101,440,149,445);

          setfillstyle(1,9);
        bar(151,440,199,445);

          setfillstyle(1,14);
        bar(201,440,249,445);

          delay(300);

          setfillstyle(10,6);
        bar(51,440,99,445);

          delay(150);

        bar(101,440,149,445);

          delay(150);

        bar(151,440,199,445);

          delay(150);

        bar(201,440,249,445);

          delay(150);

          setfillstyle(1,4);
        bar(51,440,99,445);

          delay(150);

          setfillstyle(1,10);
        bar(101,440,149,445);

          delay(150);

          setfillstyle(1,9);
        bar(151,440,199,445);

          delay(150);

          setfillstyle(1,14);
        bar(201,440,249,445);
       }
    while(!kbhit());

    setfillstyle(10,6);
      bar(51,440,99,445);
      bar(101,440,149,445);
      bar(151,440,199,445);
      bar(201,440,249,445);
   }

 main( )
    {
       int driver=VGA;
       int mode=VGAHI;

       initgraph(&driver,&mode,"..\\Bgi");

       Queue obj;

       obj.show_working( );

       closegraph( );
       return 0;
    }

No comments:

Post a Comment