ARBOL_DRAW_1.c



/* DIBUJA UN ARBOL BINARIO EN PANTALLA
	DE DATOS LEIDOS DE UN ARCHIVO

	SE HACE USO DE LA LIBRERIA GRAPHICS
*/


#include <graphics.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void activa_modo_grafico(void);
void cierra_modo_grafico(void);
void circulo(int, int, char dato[2]);
void linea(int, int, int, int);
void muestra_arbol(void);
void enlista(struct arbol *);

#define hy(y) (y-10)
#define dy(y) (y+10)
#define texto(x) (x-3)

struct arbol
{
   int x1;
   int y1;
   int x2;
   int y2;
   char dato[2];
   struct arbol *sig;
};
struct arbol *lista;

void activa_modo_grafico(void)
{
   int gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "c:\\tc30.pp\\bgi");   // inicializa el modo grAfico
   errorcode = graphresult();  	      // lee resultado de la inicializaciOn
   if (errorcode != grOk)             // ocurriO un error
   {
      printf("Error grafico: %s\n", grapherrormsg(errorcode));
      exit(1);
   }
}

void cierra_modo_grafico(void)
{ closegraph(); }

void circulo(int x,int y, char dato[5])
{
   circle(x, y, 10); outtextxy(texto(x), texto(y), dato);
}

void linea(int x1, int y1, int x2, int y2)  // traza linea(dex,dey,ax,ay)
{
   if( (x1==x2) && (y1==y2) );
   else line(x1, dy(y1), x2, hy(y2));
}

void muestra_arbol(void)
{
   char cad[2];

   while(lista)
   {
      cad[0]=lista->dato[0];
      cad[1]='\0';
      circulo(lista->x2,lista->y2,cad);
      linea(lista->x1,lista->y1,lista->x2,lista->y2);
      lista = lista->sig;
   }
}

void enlista(struct arbol *nodo)
{
   struct arbol *copia_del_nodo=NULL;

   copia_del_nodo = (struct arbol *)malloc(sizeof(struct arbol));
   copia_del_nodo->x1 = nodo->x1; copia_del_nodo->y1 = nodo->y1;
   copia_del_nodo->x2 = nodo->x2; copia_del_nodo->y2 = nodo->y2;
   sprintf(copia_del_nodo->dato,"%s",nodo->dato);
   copia_del_nodo->dato[2]='\0';
   copia_del_nodo->sig = NULL;
   if(!lista) lista = copia_del_nodo;
   else
   {
      copia_del_nodo->sig = lista;
      lista = copia_del_nodo;
   }
}

void lee_datos(char arch_in[12])
{
   struct arbol *nodo=NULL;
   FILE *file_in;
   char lectura[5];

   if ((file_in = fopen(arch_in,"rt")) == NULL)		// archivo de entrada
      {  printf("No se puede abrir el archivo"); return; }
   fgets(lectura, 5, file_in);
   nodo = (struct arbol *)malloc(sizeof(struct arbol));
   while (strncmp(lectura,"ok",2) != 0)
   {
				  nodo->x1 = atoi(lectura);
      fgets(lectura, 5, file_in); nodo->y1 = atoi(lectura);
      fgets(lectura, 5, file_in); nodo->x2 = atoi(lectura);
      fgets(lectura, 5, file_in); nodo->y2 = atoi(lectura);
      fgets(lectura, 5, file_in); sprintf(nodo->dato, "%s",lectura);
      nodo->dato[2]='\0';
      enlista(nodo);
      fgets(lectura, 5, file_in);
   }
   fclose(file_in);
}

void main(void)
{
   lista = NULL;
   system("gen_arb.exe");
   lee_datos("datos.arb");
   activa_modo_grafico();
   muestra_arbol();
   getch();
   cierra_modo_grafico();
}