PDA

View Full Version : ĐA CTDL trong C,cần giúp đỡ!!



langtutcs
27-09-2011, 08:13 AM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<dos.h>
#include<ctype.h>
#include<graphics.h>
#include<stdlib.h>
#include<alloc.h>
//====================================

//CHUONG TRINH TAO VA SO SANH HAI CAY NHI PHAN

struct Tree2
{
int So;
Tree2 *Left,*Right;
};
typedef Tree2 *PTree2;
PTree2 Root1,Root2,p;
int y;
int nd,nd2,cv,k;
//==================================
//ve cay do hoa
void ve(PTree2 root,int cot,int dong,int i,int j)
{
setcolor(14);
outtextxy(20,20," DAI HOC BACH KHOA DA NANG ");
setcolor(10);
outtextxy(165,130,"Cay I");
outtextxy(430,130,"Cay II");
{
i=2*i/3;
moveto(cot,dong);
setcolor(j);
setfillstyle(SOLID_FILL,j);
circle(cot,dong,6);
floodfill(cot,dong+5,j);// i=i/2;
}

if((*root).Left!=NULL)
{
line(cot,dong,cot-i,dong+i) ;
ve((*root).Left,cot-i,dong+i,i,j);
}
if((*root).Right!=NULL)
{
line(cot,dong,cot+i,dong+i) ;
ve((*root).Right,cot+i,dong+i,i,j);
}
}
//==================================
// dich cay
void dich_cay()
{
void *buf;
int x=60,i,z;
unsigned size;
size=imagesize(45,144,300,400);
buf=malloc(size);
getimage(45,144,300,400,buf);
putimage(45,144,buf,COPY_PUT);delay(200);
cleardevice();
for(i=0;i<124;i++)
{
x=x+2;
z=30;
putimage(x,144,buf,COPY_PUT);
ve(Root2,450,150,80,11);
delay(z);
}
free(buf);
}

//==================================
//cap phat mot nut la cho cay nhi phan
PTree2 makenode (int x)
{
PTree2 p;
p= new Tree2;
(*p).So=x;
(*p).Left=NULL;
(*p).Right=NULL;
return (p);
}
//==================================
//tim mot nut co noi dung x
PTree2 search(PTree2 root ,int x)
{
PTree2 p;
if((*root).So==x) return (root);
if(root==NULL) return NULL;
p=search((*root).Left,x);
if(p==NULL)
p=search((*root).Right,x);
return p;
}
//Kiem tra 2 cay
int Test(PTree2 Root1,PTree2 Root2)
{
if(Root1 != NULL && Root2 != NULL)
if(Test((*Root1).Left,(*Root2).Left) &&Test((*Root1).Right,(*Root2).Right))return(1);
else return(0);
else
if(Root1==NULL && Root2==NULL)return(1);
else return(0);
}
//==================================

int ChieuCao(PTree2 Root)
{
int trai,phai;
if (Root==NULL) return 0;
else
{
trai=ChieuCao((*Root).Left);
phai=ChieuCao((*Root).Right);
if(trai>phai) return (trai+1);
else return (phai+1);
}
}
//==================================

void VeCay(PTree2 Root,int col,int row,int nhanh)
{
int i,x;
if(Root==Root1 || Root==Root2)
{
gotoxy(col,row);
textcolor(LIGHTCYAN);
cprintf("%d",(*Root).So);
textcolor(WHITE);
}
else
{
gotoxy(col,row);printf(" ");
gotoxy(col,row);
textcolor(LIGHTMAGENTA);
cprintf("%d",(*Root).So);
textcolor(WHITE);
}
x=wherex();
if((*Root).Left != NULL)
{
for(i=col-2;i>col-ceil(nhanh/2);i--)
{
gotoxy(i,row);
delay(15);
printf("%c",196);
}
gotoxy(i,row); delay(30); printf("%c",218);
VeCay((*Root).Left,col-ceil(nhanh/2),row+1,nhanh/2);
}

else
{
for(i=col-2;i>col-ceil(nhanh/2);i--)
{
gotoxy(i,row);
delay(15);
printf(" ");
}
gotoxy(i,row); delay(30); printf(" ");
gotoxy(i,row+1);printf(" ");
}

if((*Root).Right!=NULL)
{
for(i=x+1;i<col+ceil(nhanh/2);i++)
{
gotoxy(i,row);
delay(15);
printf("%c",196);
}
gotoxy(i,row);
delay(100);printf("%c",191);
VeCay((*Root).Right,col+ceil(nhanh/2),row+1,nhanh/2);
}
else
{
for(i=x+1;i<col+ceil(nhanh/2);i++)
{
gotoxy(i,row);
delay(15);
printf(" ");
}
gotoxy(i,row);
delay(100);printf(" ");
gotoxy(i,row+1);printf(" ");
}
}

//==================================
//ham tao cay nhi phan
void Create(PTree2 &root)
{
PTree2 k,h;
int nd,nd2,cv;

{
gotoxy(2,2);
printf(" NHAP NUT GOC:");
scanf("%d",&nd); //printf(" ");
root=makenode(nd);
if (root!=NULL)

VeCay(root,50,y+2,30); }

do {
gotoxy(16,4);printf(" ");
gotoxy(15,5);printf(" ");
gotoxy(16,3);printf(" ");
gotoxy(2,3);
printf(" YOUR SECLECT:");
scanf("%d",&cv); //printf(" ");
gotoxy(2,6);printf(" ");
gotoxy(2,8);printf(" ");
gotoxy(2,9);printf(" ");
if(cv>0&&cv<4)
{
gotoxy(2,4);
printf(" Nhap nut cha:");
scanf("%d",&nd2);
p=search(root,nd2);
if (p==NULL)
{
gotoxy(2,6);
textcolor(LIGHTGREEN);
printf (" Khong co nut cha");
}
else if(cv==1)
{
gotoxy(2,5);
printf(" Nhap nut la:");
scanf("%d",&nd);
if((*p).Left!=NULL)
{
k=(*p).Left;
(*p).Left=makenode(nd);
h=(*p).Left;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if((*p).Left==NULL)
(*p).Left=makenode(nd);

}
else if(cv==2)
{
gotoxy(2,5);
printf(" Nhap nut la:");
scanf("%d",&nd); // printf(" ");
if((*p).Right!=NULL)
{
k=(*p).Right;
(*p).Right=makenode(nd);
h=(*p).Right;
(*h).Left=(*k).Left;
(*h).Right=(*k).Right;
}
else if ((*p).Right==NULL)
(*p).Right=makenode(nd);
}
else if(cv==3)
{
gotoxy(2,5);
printf(" Nhap nut la:");
scanf("%d",&nd);
k=search(root,nd);
if(k==NULL)
{
gotoxy(2,8);
printf(" khong co nut nay");
}
else if((*k).Left!=NULL||(*k).Right!=NULL)
{
gotoxy (2,9);
printf(" %d khong phai nut la khong xoa duoc",(*k).So);
}
else if(k==(*p).Left)
(*p).Left=NULL;
else if(k==(*p).Right)
(*p).Right=NULL;
}
}

VeCay(root,50,y+2,30);
}while (cv!=0);
}


//==================================
//tao chu chay
void chuchay( int xg , int yg ,int xm , int ym , char xau[] , int time)

// xg , yg : toa do sinh van ban
// xm , xm : toa do cuoi cua van ban se di chuyen den
// time : delay time

{
int i,j=0;
if (xg==xm)
{
for (j=0 ; j<=strlen(xau) ; j++)
{
if (xau[j]!=32)
for (i=yg ; i>= ym ; i--)
{
gotoxy(xg,i);putch(xau[j]);
gotoxy(xg,i+1);putch(' ');
delay(time);
}
else {
gotoxy(xg,ym);putch(' ');
}
xg++;
}
}

}
//================================================== =============

union REGS in,out;

int callmouse()
{
in.x.ax=1;
int86(51,&in,&out);
return 1;
}
void mouseposi(int &xpos,int &ypos)
{
in.x.ax=3;
int86(51,&in,&out);
// click=out.x.bx;
xpos=out.x.cx;
ypos=out.x.dx;
}
int mousehide()
{
in.x.ax=2;
int86(51,&in,&out);
return 1;
}

//================================================== =============
//lay toa do khi click
int showmousegraphics ()
{

int x;
in.x.ax = 1;
int86 (0X33,&in,&out);

while (!kbhit ())
{
in.x.ax = 3;
int86 (0X33,&in,&out);
if (out.x.bx == 1)
{
x=out.x.dx;
//printf ("\nLeft || X - %d ", x);
if(x!=0) goto kt;
}
}
getch ();
closegraph ();
kt:return x;
}

//================================================== =============
int main()
{

a2:int x,y,a,b;
char chon;
clrscr();
int g=DETECT,m;

//clrscr();
initgraph(&g,&m,"c:\\tc\\bgi");
callmouse();
a1:settextstyle(4,HORIZ_DIR,4);
outtextxy(230,15,"MENU");
settextstyle(3,HORIZ_DIR,2);
outtextxy(250,50,"Play");
outtextxy(250,80,"Contact");
outtextxy(250,110,"View EX");
outtextxy(250,140,"Help") ;
outtextxy(250,170,"Exit");
while(1)
{
mouseposi(x,y);
gotoxy(10,9);
printf("\n\tX = %3d||Y = %3d",x,y);

if((y>55)&&(y<75)&&(x>249)&&(x<350))
{

gotoxy(30,16);printf("Ban Da Chon : Play ");
delay(1000);gotoxy(55,16);printf("3");
delay(1000);gotoxy(55,16);printf("2");
delay(1000);gotoxy(55,16);printf("1");;
mouseposi(x,y);
if((y>55)&&(y<75)&&(x>249)&&(x<350))goto play;
else goto a1;

}
else if((y>85)&&(y<100)&&(x>249)&&(x<350))
{

gotoxy(30,16);printf("Ban Da Chon : Contact ");
delay(1000);gotoxy(55,16);printf("3");
delay(1000);gotoxy(55,16);printf("2");
delay(1000);gotoxy(55,16);printf("1");
mouseposi(x,y);
if((y>85)&&(y<100)&&(x>249)&&(x<350))goto contact;
else goto a1;
}
else if((y>115)&&(y<135)&&(x>249)&&(x<350))
{

gotoxy(30,16);printf("Ban Da Chon : View EX ");
delay(1000);gotoxy(55,16);printf("3");
delay(1000);gotoxy(55,16);printf("2");
delay(1000);gotoxy(55,16);printf("1");
mouseposi(x,y);
if((y>115)&&(y<135)&&(x>249)&&(x<350))goto view;
else goto a1;
}
else if((y>145)&&(y<165)&&(x>249)&&(x<350))
{
gotoxy(30,16); printf("Ban Da Chon : Help ");
gotoxy(30,20);
printf("* Hay re chuot vao muc muon chon \n (Chu y : Cho vai giay de thuc hien lenh) ");

}

else if((y>175)&&(y<195)&&(x>249)&&(x<350))
{

gotoxy(30,16);printf("Ban Da Chon : Exit ");
delay(1000);gotoxy(55,16);printf("3");
delay(1000);gotoxy(55,16);printf("2");
delay(1000);gotoxy(55,16);printf("1");
mouseposi(x,y);
if((y>175)&&(y<195)&&(x>249)&&(x<350))
{
gotoxy(30,20);printf("Ban chon Exit(c/k ?) ");
chon=getch();
if(chon=='c'||chon=='C')goto ketthuc;
else if(chon=='k'||chon=='K')goto a1;
}
else goto a1;
}
else {gotoxy(30,16);
printf("Ban chon : Ban chua chon ");
}
}

contact:
{
closegraph ();


{
struct time t;
int i,j;
j=1;
struct date d;
gotoxy(35,4);
printf("Xin Chao ^_^ ");
gotoxy(35,5);
getdate(&d);
while(i<32000)
{
laplai:gotoxy(35,2);
printf("Ngay:%2d Thang:%2d Nam:%2d\n",d.da_day,d.da_mon,d.da_year);
gettime(&t);
gotoxy(35,3);
printf("%2d:%2d:%2d\n",t.ti_hour,t.ti_min,t.ti_sec);
delay(1000);
i++;
j++;
if(j%10==0)
{
textcolor(2);
chuchay(20,20,20,20,"press B to Back or E to Exit : ",10);
chon=getch();
if(chon=='b'||chon=='B') goto a2;
else if(chon=='e'||chon=='E') goto ketthuc;
}
else if(j==2) goto tt;
else goto laplai;
tt:textcolor(10);
chuchay(42,16,42,10,"Truong Dai Hoc Bach Khoa Da Nang" , 10);
chuchay(42,16,42,11,"Lop 06T4",10);
chuchay(42,16,42,12,"Giang Vien Huong Dan : Phan Thanh Tao",10);
chuchay(42,16,42,13,"Sinh Vien Thuc Hien : Dang Van Tuan",10);
chuchay(42,16,42,14," : Le Minh Long",10);
textcolor(5);
chuchay(35,16,35,15,"(Vui long cho vai giay de chuyen cong viec)",2);
}
}
}

view:
{
closegraph ();


{
struct time t;
int i,j;
j=1;
struct date d;
gotoxy(35,4);
printf("Xin Chao ^_^ ");
gotoxy(35,5);
getdate(&d);
while(i<32000)
{
laplai1:gotoxy(35,2);
printf("Ngay:%2d Thang:%2d Nam:%2d\n",d.da_day,d.da_mon,d.da_year);
gettime(&t);
gotoxy(35,3);
printf("%2d:%2d:%2d\n",t.ti_hour,t.ti_min,t.ti_sec);
delay(1000);
i++;
j++;
if(j%10==0)
{
textcolor(2);
chuchay(20,20,20,20,"press B to Back or E to Exit : ",20);
chon=getch();
if(chon=='b'||chon=='B') goto a2;
else if(chon=='e'||chon=='E')goto ketthuc;
}
else if(j==2) goto tt1;
else goto laplai1;
tt1:textcolor(10);
chuchay(15,15,15,15,"Bai Tap : So sanh cau truc hai cay nhi phan",50);
textcolor(5);
chuchay(17,17,17,17,"(Vui long cho vai giay de chuyen cong viec)",2);
}
}
}


play:
{
//x=0;
//y=0;
closegraph ();

clrscr();
textcolor(LIGHTRED);
cprintf("\n\n\n\n HUONG DAN CACH NHAP CAY NHI PHAN");
// printf("\n\n");
textcolor(YELLOW);
// printf("\n\n");
gotoxy(7,7);
cprintf("1. Chon 1 de nhap nut con ben trai");
gotoxy(7,8);
cprintf("2. Chon 2 de nhap nut con ben phai");
gotoxy(7,9);
cprintf("3. Chon 3 de xoa nut la");
gotoxy(7,10);
cprintf("4. Chon 0 de dung nhap");
textcolor(WHITE);
gotoxy(7,12);
cprintf("==> PRESS ANY BUTTON!! ") ;
getch();
clrscr();
Root1=NULL;
Root2=NULL;
textcolor(YELLOW);
gotoxy(2,1);
cprintf(" NHAP CAY NHI PHAN THU I (seclect: 0,1,2,3)");
textcolor(WHITE);
Create(Root1);
clrscr();
textcolor(YELLOW);
gotoxy(2,1);
cprintf(" NHAP CAY NHI PHAN THU II (seclect: 0,1,2,3)");
textcolor(WHITE);
Create(Root2);
clrscr();
textcolor(YELLOW);
if(Test(Root1,Root2))cprintf("\n\n --> HAI CAY NHI PHAN CO CAU TRUC TUONG TU NHAU\n");
else cprintf("\n\n --> HAI CAY NHI PHAN CO CAU TRUC KHONG TUONG TU NHAU\n");
printf("\n Minh hoa -->");
textcolor(WHITE);
printf("\n\t\t\t Cay nhi phan thu nhat\n");
delay(400);
y=wherey(); // printf("%d",y); tai vi tri nay y=9
if(Root1!=NULL)

VeCay(Root1,40,y+1,40);
else
{
gotoxy(38,y+1);
textcolor(GREEN);
cprintf("NULL");
textcolor(WHITE);
}
y=y+ChieuCao(Root1)+2;
gotoxy(1,y+1);
printf(" Cay nhi phan thu hai");
delay(400);
y+=2 ;
if(Root2!=NULL)
VeCay(Root2,40,y+1,40);
else
{
gotoxy(38,y+1);
textcolor(GREEN);
cprintf("NULL");
textcolor(WHITE);
}
y=y+ChieuCao(Root2)+3;
gotoxy(26,y);
delay(200);
textcolor(YELLOW);
cprintf(" PRESS ENTER !! TO CONTINUE ");
getch();
int gdriver =DETECT,gmode ;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI") ;
ve(Root1,180,150,80,12);
delay(200);
ve(Root2,450,150,80,11);
delay(2500);
dich_cay();
outtextxy(200,400,"Press anykey to continue !");
getch();

closegraph();
do{
msg:textcolor(8);
chuchay(20,20,20,20," An E de Exit va B de tro lai MENU",100);//goto nhay;
msg1:textcolor(10);
chuchay(20,20,20,20," An E de Exit va B de tro lai MENU",100);
chon=getch();
} while((chon!='e')&&(chon!='E')&&(chon!='b')&&(chon!='B'));
if(chon=='e'||chon=='E') goto ketthuc;
else if(chon=='b'||chon=='B') goto a2;
} getch();
ketthuc:
return 0;
}


ai giỏi về đồ hoạ trong C thì giúp mình với, mình chạy nó ko thực hiện đồ hoạ được gì hết,cứ báo lỗi hoài à.mong các pro giúp mình vơi.
cám ơn nhiều!:MatCuoi (49):