// Hang Doi Mua Ve.cpp : Defines the entry point for the console application.
// So nguoi mua ve duoc bieu dien ban ky tu A B C D ...
// Nguoi dau tien la A, sau moi 1s thi ban ve duoc 1 nguoi
#include "stdafx.h"
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include <windows.h>
// Ham gotoxy (lenh:gotoxy(x,y), thu vien:windows.h)
void gotoxy(short x, short y)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x-1;
pos.Y=y-1;
SetConsoleCursorPosition(hCon, pos);
}
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
//KHOI TAO DSLK QUEUE
struct tagnode{
char Data;
struct tagnode *next;
};
typedef tagnode *node;
typedef struct taglist{
node head;
node tail;
}linkedlist;
typedef linkedlist Queue;
void initQueue(Queue &q){
q.head=q.tail=NULL;
}
int isempty(Queue q) //kiem tra q rong hay khong
{
return(q.head==NULL);
}
void enQueue(Queue &q,char x) //them x vao cuoi q
{ node p= new tagnode;
if(p==NULL) return;
p->Data=x;
p->next=NULL;
if(isempty(q)){
q.head=q.tail=p;
}
else
{ q.tail->next=p;
q.tail=p;
}
}
void deQueue(Queue &q) //xoa phan tu dau cua q
{ node p=q.head;
if(isempty(q))
return;
q.head=p->next;
if(q.head==NULL)
q.tail=NULL;
p->next=NULL;
delete p;
}
void insertQueue(Queue &q,int n) //nhap phan tu cua q(nhap nguoi mua ve)
{ char j=65;
initQueue(q);
for(int i=0;i<n;i++)
enQueue(q,j++);
}
void drawQueue(int x,int y) //ve phong cho mua ve
{ int i,j;
for(i=0;i<4;i++)
if(i==0 || i==3)
for(j=0;j<16;j++)
{ gotoxy(x+j,y+i);
putchar(196);
}
else
for(j=0;j<17;j+=3)
{ gotoxy(x+j,y+i);
putchar(221);
}
}
void putoutQueue(Queue q,int x,int y) //in hang doi mua ve xem phim ra man hinh
{ node p=q.head;
int i=1;
gotoxy(20,1);
printf(" \n MO PHONG HANG DOI MUA VE XEM PHIM \n");
drawQueue(x,y);
if(isempty(q))
return;
else
while(p)
{ gotoxy(x+i,y+2);
putchar(p->Data);
p=p->next;
i+=3;
}
}
void out_Queue(Queue q,int n) //quan ly so nguoi cho mua ve
{ int x=29,y=4;
insertQueue(q,n);
putoutQueue(q,x,y);
gotoxy(25,10);
sleep(4000);
for(int i=0;i<n;i++)
{ deQueue(q);
putoutQueue(q,x,y);
if(i!=(n-1))
{ gotoxy(25,10);
printf("\n Con %d nguoi cho mua ve",((n-1)-i));
}
else
{ gotoxy(33,10);
}
sleep(2000);
}
}
//HAM MAIN
void main()
{ Queue q;int n, m ,k;
printf("\n Nhap so nguoi muon mua ve: ");
scanf("%d",&m);
printf("\n Nhap so ve: ");
scanf("%d",&k);
if(k<=m)
{
n=k;
printf("\n Ban het ve");
out_Queue(q,n);
printf("\n Con %d nguoi chua co ve", m-k);
}
else
{
n=m;
printf("\n Tat ca deu co ve !");
out_Queue(q,n);
printf("\n Con thua %d ve :",k-m );
}
getch();
}
0 nhận xét:
Đăng nhận xét