这个程序是编译成功的,
可是运行不了!!!屏幕总是出现提示,,
好像是图形初始化没安装!!
好奇怪啊,请高手们帮忙运行看一下为什么
会这样的???
我是在win-tc里做的。
程序如下
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<time.h>
typedef struct TREE
{
char data;
struct TREE *ichild;
struct TREE *rchild;
int x;
int y;
}Tree;
struct OUTPUT
{
int x;
int y;
int num;
}s;
int nodeNUM=0;
char way;
char str[3];
void Init();
void Close();
Tree *CreatTree();
Tree *InitTree(int h,int t,int w);
void DrawTree(Tree *t);
void preorder(Tree *t);
void posorder(Tree *t);
void inorder(Tree *t);
void DrawNode(Tree *t,int color);
void Clrscr();
main()
{
Tree *root;
randomize();
root=CreatTree();
Init();
DrawTree(root);
sleep(1);
s.x=100;s.y=300;s.num=1;
preorder(root);
getch();
Clrscr();
DrawTree(root);
sleep(1);
s.x=100;s.y=350;s.num=1;
inorder(root);
getch();
Clrscr();
DrawTree(root);
sleep(1);
s.x=100;s.y=400;s.num=1;
posorder(root);
Close();
}
void Clrscr()
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
bar(0,20,640,280);
}
Tree *CreatTree()
{
Tree *root;
Clrscr();
printf("please input n\n");
printf("1.computer creat\n");
printf("2.people creat\n");
way=getch();
if(way!='2')
way='1';
if(way=='2')
printf("please creat the treen\n");
root=InitTree(1,320,150);
system("pause");
return root;
}
Tree *InitTree(int h,int t,int w)
{
char ch;
int n;
Tree *node;
if(way=='2')
scanf("%c",&ch);
else
{
n=random(5);
if(n==0&&nodeNUM>=3)
ch='.';
else
ch=65+random(25);
}
if(ch=='.')
return NULL;
else
{
if(h==6||nodeNUM==26)
return NULL;
node=(Tree*)malloc(sizeof(Tree));
node->data=ch;
node->x=t;
node->y=h*50;
nodeNUM++;
node->ichild=InitTree(h+1,t-w,w/2);
node->rchild=InitTree(h+1,t+w,w/2);
}return node;
}
void DrawTree(Tree *t)
{
if(t!=NULL)
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
fillellipse(t->x,t->y,9,9);
setcolor(WHITE);
circle(t->x,t->y,10);
sprintf(str,"%c",t->data);
outtextxy(t->x-3,t->y-2,str);
if(t->ichild!=NULL)
{
line(t->x-5,t->y+12,t->ichild->x+5,t->ichild->y-12);
DrawTree(t->ichild);
}
if(t->rchild!=NULL)
{ line(t->x+5,t->y+12,t->ichild->x-5,t->ichild->y-12);
DrawTree(t->rchild);
}
}
}
void DrawNode(Tree *t,int color)
{
setcolor(YELLOW);
setfillstyle(SOLID_FILL,YELLOW);
fillellipse(t->x,t->y,10,10);
setcolor(RED);
sprintf(str,"%c",t->data);
outtextxy(t->x-3,t->y-2,str);
setcolor(color);
outtextxy(s.x,s.y,str);
setcolor(RED);
sprintf(str,"%d",s.num);
outtextxy(t->x-3,t->y-20,str);
s.num++;
sleep(1);
}
void preorder(Tree *t)
{
if(t!=NULL)
{
s.x+=15;
DrawNode(t,GREEN);
preorder(t->ichild);
preorder(t->rchild);
}
}
void inorder(Tree *t)
{if(t!=NULL)
{
inorder(t->ichild);
s.x+=15;
DrawNode(t,YELLOW);
inorder(t->rchild);
}
}
void posorder(Tree *t)
{
if(t!=NULL)
{
posorder(t->ichild);
posorder(t->rchild);
s.x+=15;
DrawNode(t,BLUE);
}
}
void Init()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
cleardevice();
setcolor(YELLOW);
outtextxy(250,10,"anykey to continue");
setcolor(RED);
outtextxy(20,300,"preorder");
outtextxy(20,350,"inorder");
outtextxy(20,400,"posorder");
}
void Close()
{
getch();
closegraph();
}
[此贴子已经被作者于2005-12-15 23:33:09编辑过]