Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to illustrate the implementation of arrays as a Stack in graphics

Posted By: Easy Tutor     Category: C++ Programming     Views: 2882

A C++ Program to illustrate the implementation of arrays as a Stack ( in graphics ).

Code for Program to illustrate the implementation of arrays as a Stack in graphics in C++ Programming

/**************************************************************************  **************************************************************************     A C++ Program to illustrate the implementation of arrays as     a Stack ( in graphics ). ************************************************************************** **************************************************************************/

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

 const max_length=10;

 /***********************************************************************///------------------------------  stack  ------------------------------///***********************************************************************/class STACK
    {
       private:
        long stack[max_length];
        int flag[max_length];
        int top;

       public:
        STACK( );
        void pop( );
        void push( );
        void waiting( );
        long get_value( );
        void show_main_screen( );
        void show_working( );
    };


 /*************************************************************************///--------------------------  STACK( )  ---------------------------------///*************************************************************************/void STACK::STACK( )
    {
       top=-1;

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

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

       char String[10]={NULL};

       setfillstyle(1,0);
     bar(35,255,320,275);

       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(1,0);
             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(1,0);
     bar(35,255,320,275);

       long number=atol(String);

       return number;
    }

 /*************************************************************************///---------------------------  push( )  ---------------------------------///*************************************************************************/void STACK::push( )
    {
       long item=get_value( );

       if(top==max_length-1)
      {
         setcolor(11);
         settextstyle(0,0,3);
           outtextxy(30,250,"Stack is Full");

         delay(1000);

         setcolor(0);
         settextstyle(0,0,3);
         outtextxy(30,250,"Stack is Full");
      }

       else
      {
         top++;
         stack[top]=item;
         flag[top]=1;
      }
    }

 /*************************************************************************///---------------------------  pop( )  ----------------------------------///*************************************************************************/void STACK::pop( )
    {
       if(top==-1)
      {
         setcolor(11);
         settextstyle(0,0,3);
           outtextxy(30,250,"Stack is Empty");

         delay(1000);

         setcolor(0);
         settextstyle(0,0,3);
           outtextxy(30,250,"Stack is Empty");
      }

       else
      {
         stack[top]=0;
         flag[top]=0;
         top--;
       }
    }

 /*************************************************************************///----------------------  show_main_screen( )  --------------------------///*************************************************************************/void STACK::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);
      }

       int x;
       int y;
       int color;

       for(int count_2=0;count_2<3000;count_2++)
      {
         x=random(640);
         y=random(480);
         color=random(15);

         putpixel(x,y,color);
      }

       setfillstyle(1,7);
       bar(80,55,540,58);

       setcolor(9);
       settextstyle(1,0,5);
     outtextxy(80,10,"Stack Implementation");
     outtextxy(81,10,"Stack Implementation");

       setcolor(10);
       settextstyle(1,0,5);
     outtextxy(445,380,"Stack");
     outtextxy(446,380,"Stack");

       setcolor(12);
       settextstyle(8,0,4);
     outtextxy(20,80,"Press :");
     outtextxy(21,80,"Press :");

       setcolor(14);
       settextstyle(7,0,2);
     outtextxy(50,120,"< P > to Push an element,");
     outtextxy(50,150,"< O > to Pop an element,");
     outtextxy(50,180,"< Esc > to Exit.");

       setcolor(7);
       settextstyle(7,0,2);
     outtextxy(51,120,"  P  ");
     outtextxy(51,150,"  O  ");
     outtextxy(51,180,"  Esc ");

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

       setcolor(15);
     rectangle(449,79,551,379);

       setfillstyle(1,0);
     bar(450,80,550,378);

       char String[10][10]={"Stack[9]","Stack[8]","Stack[7]","Stack[6]",
                "Stack[5]","Stack[4]","Stack[3]","Stack[2]",
                "Stack[1]","Stack[0]"};

       for(int count_3=0;count_3<max_length;count_3++)
      {
         setcolor(15);
           line(449,79+(count_3*30),550,79+(count_3*30));

         setcolor(15);
         settextstyle(0,0,1);
           outtextxy(380,92+(count_3*30),String[count_3]);
      }
    }

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

       int size=0;

       char *Buffer=NULL;

       size=imagesize(555,80,635,400);
       Buffer=newchar[size];

       getimage(555,80,635,400,Buffer);

       int key_code=0;

       do
      {
         waiting( );

         char key=NULL;

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

         if(key_code==112 || key_code==80)
        push( );

         elseif(key_code==111 || key_code==79)
        pop( );

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

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

           putimage(555,80,Buffer,0);

           if(count_1==top)
              {
             setfillstyle(1,11);
               bar(565,360-(count_1*30),595,370-(count_1*30));

             int y1=365-(count_1*30);
             int y2=355-(count_1*30);

             setcolor(11);

             for(int count_2=0;count_2<=20;count_2++)
                {
                   line(558,y1,568,y2);

                   y2++;
                }

             setcolor(15);
             settextstyle(2,0,6);
               outtextxy(600,y1-10,"Top");
               outtextxy(601,y1-10,"Top");

             flag=1;
        }
        if(flag)
          break;
         }

      delay(500);

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

        else
           setfillstyle(1,0);

        bar(450,378-(count_3*30),550,350-(count_3*30));
         }

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

        charvalue[6]={NULL};

        itoa(stack[count_4],value,10);

        if(flag[count_4]!=0)
           outtextxy(480,360-(count_4*30),value);
         }

      }
       while(key_code!=27);
    }

 /*************************************************************************///----------------------------  waiting( )  -----------------------------///*************************************************************************/void STACK::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(1,0);
        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(1,0);
      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");

       STACK obj;

       obj.show_working( );

       closegraph( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to illustrate the implementation of arrays as a Stack in graphics is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!