/*Ticket window - Program Using Queue
compiler- Turbo c++ author- Mangilal Sharma
--------------------------------------------------------*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class ticket
{
private:
int q[5];
int front, rear, n; //n..to store number of elements
public:
void enqueue(int);
void ticketchecker();
void print();
int isempty();
void queuebuilder();
ticket();
};
void ticket::enqueue(int a)
{
q[rear] = a;
rear++;
n++;
if (rear == 5)
rear = 0;
cout<<""<<a<<" Is Enqeueued....."<<endl;
}
void ticket::ticketchecker()
{
clrscr();
int not,rt;
if ( !isempty() )
{
not = q[front];
if (not > 2)
{
rt = not - 2;
if ( rt > 0 )
{
enqueue(rt);
front++;
cout<<""<<not<<"# Tickets were demanded. \n2 Tickets aregiven.\nEnqueued again for next go to get remaining #"<<rt<<"Tickets"<<endl;
}
else if ( rt <= 0 )
front++;
n--;
}
else if ( not <= 2 )
{
front++;
n--;
cout<<""<<not<<"# tickets are given...."<<endl;
}
}
else
cout<<"!!! NO REQUEST IN THE QUEUE " <<endl;
getch();
clrscr();
return;
}
void ticket::print()
{
clrscr();
int i = front;
if ( n > 0 )
{
cout<<"REQUESTS ENQUEUED ARE :" ;
do
{
cout<<q[i]<<" ";
i++;
if ( i == 5 )
i = 0;
}
while ( i != rear );
}
else
cout<<"NO REQUEST IN QUEUE...QUEUE IS EMPTY ";
getch();
clrscr();
return;
}
int ticket :: isempty()
{
if ( n == 0 )
return 1;
else
return 0;
}
//This Function takes input from the user and shows Queue graphically
void ticket::queuebuilder()
{
int tickets, z = 0, i = 0, x = 20, y = 10, h = 17;
char c;
cout<<"Enter number of tickets to purchase... "<<endl;
while (!(c == 'E' || c == 'e'))
{
if ( z > 4)
{
gotoxy(5,15);
cout<<" ERROR!!! Queue Size Violation...Exiting Now "<<endl;
getch();
clrscr();
return;
}
else
{
cout<<"Enter : ";
cin>>tickets;
enqueue(tickets);
z++;
gotoxy(17,10);
cout<<"->";
gotoxy(20,9);
cout<<"----------------------------------";
gotoxy(x,y);
cout<<q[i];
x+=4;
gotoxy(20,11);
cout<<"----------------------------------";
gotoxy(5,15);
cout<<"PRESS C/c to CONTINUE E/e to END: ";
cin>>c;
h += 6;
i++;
}
gotoxy(1,2);
}
return;
}
ticket::ticket()
{
n = rear = front = 0;
int b;
do
{
clrscr();
gotoxy(15,5);
cout<<"Press 1................ENTER NO OF TICKETS";
gotoxy(15,8);
cout<<"Press 2................TICKET CHECKER";
gotoxy(15,11);
cout<<"Press 3................ALL TICKET REQUESTS";
gotoxy(15,14);
cout<<"Press 4................MAIN PAGE";
gotoxy(15,17);
cout<<"Press 5................EXIT FROM PROGRAM";
gotoxy(15,20);
cout<<"NOW ENTER : ";
cin>>b;
switch(b)
{
case 1:
clrscr();queuebuilder();break;
case 2:
ticketchecker();break;
case 3:
print();break;
case 4:
return;
case 5:
exit(1);
default:
cout<<"\n\t\tWrong Choice, Enter Again\n";
}
}
while(1);
}
/*=========================19Nov.,2010=============================*/
nice article for beginners.thank you.
ReplyDeletec++ tutorial
java tutorial