【LB】图形化编程与二叉树的应用
来源:互联网 发布:html编辑软件 编辑:程序博客网 时间:2024/05/16 10:24
C语言实现图形化编程,最直接的方式是通过graphics.h来实现,但它只在TC编译器中能用,要想在vc中使用,目前仅有的办法是通过easyx软件导入函数库。传送门:http://www.easyx.cn/。进去后下载,将文件解压后安装就行。图形编程既简单又有意思,还能做出很多有用的东西来,推荐大家没事去学学!
整个程序目前实现了两个功能,一个是通过输入先序序列和中序序列来画出二叉树,另一个是通过输入树的广义表(我只能这么描述了……)来画出二叉树。日后准备实现自动画出线索二叉树。这次有源码也有注释。另外得益于图形函数库,程序可以用鼠标控制!
整个程序目前实现了两个功能,一个是通过输入先序序列和中序序列来画出二叉树,另一个是通过输入树的广义表(我只能这么描述了……)来画出二叉树。日后准备实现自动画出线索二叉树。这次有源码也有注释。另外得益于图形函数库,程序可以用鼠标控制!
#include <stdio.h>#include <windows.h>#include <conio.h>#include <stdlib.h>#include <graphics.h>#include <string.h>void menu();int check_button(int,int);void button1(MOUSEMSG,int,int,int,int);void button2(MOUSEMSG,int,int,int,int);/*struct MOUSEMSG{UINT uMsg; // 当前鼠标消息bool mkCtrl; // Ctrl 键是否按下bool mkShift; // Shift 键是否按下bool mkLButton; // 鼠标左键是否按下bool mkMButton; // 鼠标中键是否按下bool mkRButton; // 鼠标右键是否按下int x; // 当前鼠标 x 坐标int y; // 当前鼠标 y 坐标int wheel; // 鼠标滚轮滚动值};*/typedef struct node{int data;struct node *lchild;struct node *rchild;}btnode; //树的结构体int temp; //根据树的深度得到初始缩减值int suojian=1; //树依次向下时的缩减变量btnode *t; //树的根节点btnode *create(char *pre,char *in,int n){btnode *s; //当前节点char *p; //中序数据的指针int k; //记录位置if(n<=0) return NULL; //如果当前递归层次没有数据,返回空指针s=(btnode *)malloc(sizeof(btnode)); //为当前节点分配内存空间s->data=*pre; //当前节点赋值(先序序列的首值)for(p=in;p<in+n;p++)if(*p==*pre) break; //查找p的位置k=p-in;s->lchild=create(pre+1,in,k); //创建当前树的左子树s->rchild=create(pre+k+1,p+1,n-k-1); //创建当前树的右子树return s; //返回当前节点}//根据先序和中序创建二叉树btnode *getree(btnode *s, char *str){btnode *p[50],*n=NULL;int top=-1,j=0,k;char ch;s=NULL;while((ch=str[j++])!='\0'){switch(ch){case '(':top++;p[top]=n;k=1;break;case ')':top--;break;case ',':k=2;break;default:n=(btnode *)malloc(sizeof(btnode)); n->data=ch;n->lchild=n->rchild=NULL;if(s==NULL) s=n;else { switch(k){case 1:p[top]->lchild=n;break;case 2:p[top]->rchild=n;break;}}}}return s;} //创建树int deep(btnode *s){int dl,dr;if(s==NULL) return 0;else{dl=deep(s->lchild);dr=deep(s->rchild);return (dl>dr)?(dl+1):(dr+1);}}//求树的深度void draw(int x,int y,char c){circle(x,y,20);Sleep(100);//画圈并暂停100msouttextxy(x-5,y-5,c); //输出元素}void print(btnode *s,int x,int y){if(s!=NULL) draw(x,y,s->data);if(s->lchild!=NULL){suojian++;//缩减值自增1line(x-14,y+14,x-temp/suojian,y+100-20);Sleep(100);//画线并暂停100msprint(s->lchild,x-temp/suojian,y+100);//递归调用左子树}if(s->rchild!=NULL){suojian++;//缩减值自增1line(x+14,y+14,x+temp/suojian,y+100-20);Sleep(100);//画线并暂停100msprint(s->rchild,x+temp/suojian,y+100);//递归调用右子树}suojian--;}//画树的递归算法void main(){IMAGE img;initgraph(1000, 700); //创建图形窗口setfillcolor(RED); //设置填充色setbkcolor(WHITE); //设置背景色settextcolor(GREEN); //设置文本颜色setcolor(BLACK); //画线的颜色setbkmode(TRANSPARENT); //透明填充loadimage(&img,"IMAGE","back");putimage(0,0,&img);menu(); print(t,500,100); //画二叉树(根节点,根节点x坐标,根节点y坐标)getch(); //暂停}void menu(){MOUSEMSG m; //鼠标变量IMAGE img;bar3d(400,200,600,250,2,true); //画实心条outtextxy(420,220,"由先序和中序画二叉树"); //实心条中的文字bar3d(400,260,600,310,2,true);outtextxy(420,280,"输入广义表画二叉树");while(1){m=GetMouseMsg(); //等待鼠标事件if(m.uMsg== WM_LBUTTONDOWN) //当鼠标左键点击时breakif(check_button(m.x,m.y)==1){button1(m,400,200,600,250);break;}else if(check_button(m.x,m.y)==2){button2(m,400,260,600,310);break;}}cleardevice(); //清除图形界面内容loadimage(&img,"IMAGE","back");putimage(0,0,&img);setlinestyle(0,2,NULL);}int check_button(int x,int y){if(x>400&&x<600&&y>200&&y<250)return 1;else if(x>400&&x<600&&y>260&&y<310)return 2;} //检查鼠标按下时的位置void button1(MOUSEMSG m,int x1,int y1,int x2,int y2){int dis=1;char xianxu[20],zhongxu[20]; //先序序列和中序序列int n,high; //n为序列长度,high为树的高度line(x1+dis,y1+dis,x2-dis,y1+dis);line(x2-dis,y1+dis,x2-dis,y2-dis);line(x2-dis,y2-dis,x1+dis,y2-dis);line(x1+dis,y2-dis,x1+dis,y1+dis);Sleep(200);//xianxu="ABDGLMHEIKCFJ";//zhongxu="MLGDHBEKIACJF"; //为当前序列赋值InputBox(xianxu,20,"请输入先序序列:");InputBox(zhongxu,20,"请输入中序序列:");n=strlen(xianxu); //n获得序列长度t=create(xianxu,zhongxu,n); //创建二叉树high=deep(t);high--; //求得当前高度减1temp=1000/high; //获得初始缩减值} //第一个按键void button2(MOUSEMSG m,int x1,int y1,int x2,int y2){int dis=1;char str[20];line(x1+dis,y1+dis,x2-dis,y1+dis);line(x2-dis,y1+dis,x2-dis,y2-dis);line(x2-dis,y2-dis,x1+dis,y2-dis);line(x1+dis,y2-dis,x1+dis,y1+dis);Sleep(200);//str="A(B(D(,G)),C(E,F))"; //为当前树赋值 InputBox(str,20,"请输入树,如:A(B(D(,G)),C(E,F))");temp=200;t=getree(t,str);} //第二个按键/*void outdata(){outtextxy(20,20,"树的线性表为:"+xxb);outtextxy(20,20,"其先序序列为:"+xianxu);outtextxy(20,20,"其先序序列为:"+zhongxu);outtextxy(20,20,"其后序序列为:"+houxu);outtextxy(20,20,"其深度为:"+sd);outtextxy(20,20,"其节点个数为:"+jd);outtextxy(20,20,"其度数为:"+ds);outtextxy(20,20,"度为1的节点个数为:"+jd1);outtextxy(20,20,"度为2的节点个数为:"+jd2);}*/ //这部分功能暂时未做
0 0
- 【LB】图形化编程与二叉树的应用
- 【LB】迷宫寻路——栈与迭代的应用
- 二叉树的图形显示
- 图形化打印二叉树
- 二叉树的应用与熟悉
- 二叉树的递归建立与应用
- 二叉树的编程与实现
- 二叉树的一些性质及其在编程中的应用
- 【笔记】树、森林与二叉树的转换与应用
- 二叉树的应用
- 二叉树的应用
- 二叉树的应用
- 二叉树的应用
- 二叉树的应用
- 二叉树的应用
- 图形化二叉树演示软件
- 图形与图像编程(一)-图形编程
- LB的阶乘
- 5种方法激发孩子音乐兴趣
- HDU - 2642 Stars
- oracle 降序索引
- 排序之直接插入排序
- POJ 1321 棋盘问题
- 【LB】图形化编程与二叉树的应用
- DicomServer测试环境搭建之一:Conquest DICOM software
- WIndows学习心得【设计一个矩形】
- 函数指针的好处
- textField
- javascript eval和JSON之间的联系
- OPENCV资料总结
- Finagle:一个支持多协议的RPC系统
- 应对涂鸦期宝宝父母需注意