Mostrando entradas con la etiqueta Binario. Mostrar todas las entradas
Mostrando entradas con la etiqueta Binario. Mostrar todas las entradas

Reloj Binario

Es un Reloj binario hecho en Turbo C++ sin usar la librería graphics.

#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <iostream.h>
#include <time.h>
#include <bios.h>
#include <math.h>
class Hora{
            public:
                        Hora();
                        void estableceHora(int,int,int);
                        void imprimeMilitar();
                        void imprimeEstandar();
                        void numero(int);
                        void ventana(int,int,int,int);
                        void puntos();
            private:
                        int x;
                        int y;
                        int hora;
                        int minuto;
                        int segundo;
                        //int temp;
};
Hora::Hora(){
            hora=minuto=segundo=0;  //contructor inicia valores en cero
};
void Hora::estableceHora(int h,int m,int s){
            hora=(h > 0 && h < 24)? h: 0;
            minuto=(m > 0 && m < 60)? m:0;
            segundo=(s > 0 && s < 60)? s:0;
}
void Hora::imprimeMilitar(){
            //gotoxy(40,22);
            //cout<<(hora < 10 ? "0": "") << hora << ":"
            //<<(minuto < 10 ? "0": "") <<minuto;
}
void Hora::imprimeEstandar(){
            gotoxy(40,20);
cout<<((hora==0 || hora==12) ? 12 : hora % 12)
                        <<":" <<(minuto < 10 ? "0":"")<< minuto
                        <<":" <<(segundo < 10 ? "0":"")<<segundo
                        <<(hora<12 ? " AM": " PM");
            //window(53,21,54,22);
            //textbackground(BLACK);
            //clrscr();

}
void Hora::ventana(int x1,int y1,int x2, int y2){
//textbackground(BLACK);
//clrscr();
            window(x1,y1,x2,y2);
            textbackground(GREEN);
            clrscr();
}
void Hora::puntos(){
            textbackground(BLACK);
            clrscr();
            window(30,15,31,16);
            hora>=20 ? textbackground(4):textbackground(6);
            clrscr();
            window(30,18,31,19);
            hora>=10 && hora<20 ? textbackground(4):textbackground(6);
            clrscr();
            window(33,9,34,10);
            hora-10==8 || hora==8 ?          textbackground(4):textbackground(6);
            hora-10==9 || hora==9 ?          textbackground(4):textbackground(6);
            clrscr();
            window(33,12,34,13);
            hora==4 || hora-10==4 ||hora-20==4 ||
            hora==5 || hora-10==5 ||hora-20==5 ||
            hora==6 || hora-10==6 ||hora-20==6 ||
            hora==7 || hora-10==7 ||hora-20==7?textbackground(4):textbackground(6);
            clrscr();
            window(33,15,34,16);
            hora==2 || hora-10==2 ||hora-20==2 ||
            hora==3 || hora-10==3 ||hora-20==3 ||
            hora==6 || hora-10==6 ||hora-20==6 ||
            hora==7 || hora-10==7 ||hora-20==7?textbackground(4):textbackground(6);
            clrscr();
            window(33,18,34,19);
            hora==1 || hora-10==1 ||hora-20==1 ||
            hora==3 || hora-10==3 ||hora-20==3 ||
            hora==5 || hora-10==5 ||hora-20==5 ||
            hora==7 || hora-10==7 ||hora-20==7 ||
            hora==9 || hora-10==9 ?textbackground(4):textbackground(6);
            clrscr();
            window(36,12,37,13);
            minuto>=40 && minuto <=59 ?textbackground(4):textbackground(6);
            clrscr();
            window(36,15,37,16);
            minuto>=20 && minuto <=39 ?textbackground(4):textbackground(6);
            clrscr();
            window(36,18,37,19);
            (minuto>=10 && minuto <=19)||
            (minuto>=30 && minuto <=39)||
            (minuto>=50 && minuto <=59) ?textbackground(4):textbackground(6);
            clrscr();
            window(39,9,40,10);
            minuto-((minuto/10)*10)==9 ||
            minuto-((minuto/10)*10)==8 ?textbackground(4):textbackground(6);
            clrscr();
            window(39,12,40,13);
            minuto-((minuto/10)*10)==4 ||
            minuto-((minuto/10)*10)==5 ||
            minuto-((minuto/10)*10)==6 ||
            minuto-((minuto/10)*10)==7 ?textbackground(4):textbackground(6);
            clrscr();
            window(39,15,40,16);
            minuto-((minuto/10)*10)==2 ||
            minuto-((minuto/10)*10)==3 ||
            minuto-((minuto/10)*10)==6 ||
            minuto-((minuto/10)*10)==7 ?textbackground(4):textbackground(6);
            clrscr();
            window(39,18,40,19);
            minuto-((minuto/10)*10)==1 ||
            minuto-((minuto/10)*10)==3 ||
            minuto-((minuto/10)*10)==5 ||
            minuto-((minuto/10)*10)==7 ||
            minuto-((minuto/10)*10)==9 ?textbackground(4):textbackground(6);
            clrscr();

            window(42,12,43,13);
            segundo>=40 && segundo <=59 ?textbackground(4):textbackground(6);
            clrscr();
            window(42,15,43,16);
            segundo>=20 && segundo <=39 ?textbackground(4):textbackground(6);
            clrscr();
            window(42,18,43,19);
            (segundo>=10 && segundo <=19)||
            (segundo>=30 && segundo <=39)||
            (segundo>=50 && segundo <=59) ?textbackground(4):textbackground(6);
            clrscr();
            window(45,9,46,10);
            segundo-((segundo/10)*10)==9 ||
            segundo-((segundo/10)*10)==8 ?textbackground(4):textbackground(6);
            clrscr();
            window(45,12,46,13);
            segundo-((segundo/10)*10)==4 ||
            segundo-((segundo/10)*10)==5 ||
            segundo-((segundo/10)*10)==6 ||
            segundo-((segundo/10)*10)==7 ?textbackground(4):textbackground(6);
            clrscr();
            window(45,15,46,16);
            segundo-((segundo/10)*10)==2 ||
            segundo-((segundo/10)*10)==3 ||
            segundo-((segundo/10)*10)==6 ||
            segundo-((segundo/10)*10)==7 ?textbackground(4):textbackground(6);
            clrscr();
            window(45,18,46,19);
            segundo-((segundo/10)*10)==1 ||
            segundo-((segundo/10)*10)==3 ||
            segundo-((segundo/10)*10)==5 ||
            segundo-((segundo/10)*10)==7 ||
            segundo-((segundo/10)*10)==9 ?textbackground(4):textbackground(6);
            clrscr();
            window(50,18,51,19);
            textbackground(BLACK);
            clrscr();
}

void main(){
clrscr();
            Hora h;
            int horas, minutos, segundos, tmp;
            long bios_time;
            float seg;
            //h.ventana(2,2,14,6);
            while(!kbhit()){
                        bios_time = biostime(0, 0L);
                        horas=int(bios_time / CLK_TCK / 3600);
                        minutos=int (bios_time / CLK_TCK / 60) % 60;
                        seg=(bios_time / CLK_TCK)-float((horas-1)*3600);
                        segundos=int (seg)%60;
                        h.estableceHora(horas,minutos,segundos);
                                   gotoxy(2,2);
                                    //cout <<"La hora militar es: ";
                                   h.imprimeMilitar();
                                   gotoxy(2,4);
                                   //cout <<"La hora estandar es: ";
                                   h.imprimeEstandar();
                                   h.puntos();
                                   //h.ventana(2,2,14,6);
            }

}

Conversor entre bases (hexadecial a binario y otras)

Convierte un número de una base a otra, por ejemplo de binario (base 2) a decimal (base 10). Permite cualquier combinación.


#define TEST

#include <stdlib.h>
#ifdef TEST
 #include <stdio.h>
#endif

/*
**  Calling parameters: 1 - Number string to be converted
**                      2 - Buffer for the  converted output
**                      3 - Radix (base) of the input
**                      4 - Radix of the output
**
**  Returns: Pointer to converted output
*/

char *base_convert(const char *in, char *out, int rin, int rout)
{
      long n;
      char *dummy;

      n = strtol(in, &dummy, rin);
      return itoa(n, out, rout);
}

#ifdef TEST

int main(int argc, char *argv[])
{
      int rin, rout;
      char buf[40];

      if (4 > argc)
      {
            puts("Usage: BASCNVRT <number> <base_in> <base_out>");
            return(-1);
      }
      rin  = atoi(argv[2]);
      rout = atoi(argv[3]);
      printf("%s (base %d) = %s (base %d)n", argv[1], rin,
            base_convert((const char *)argv[1], buf, rin, rout), rout);
      return 0;
}


#endif

Arbol binario de busqueda

El programa genera un arbol binario, captura caracteres y los introduce en el arbol, despues muestra los recorridos en preorden, inorden y postorden. Maneja memoria dinamica.


//PROGRAMA QUE CAPTURA UNA CADENA DE CARACTERES DE MAXIMO 200 ELEMENTOS Y
//CREA UN ARBOL DE BUSQUEDA CON LOS CARACTERES DE LA CADENA Y REALIZA RECORRIDOS
// EN PREORDEN,ENTREORDEN Y POSTORDEN.
 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
 
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
struct nodoarbol{                        //ESTRUCTURA DEL ARBOL
         struct nodoarbol *izqnodo;
         int info;
         struct nodoarbol *dernodo;
         };
typedef struct nodoarbol NODO;    //DEFINICION DE TIPO NODO
typedef NODO *ARBOL;               //DECLARACION DE VARIABLE PUNTERO A NODO
 
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
void insertanodonuevo(ARBOL *,int);    //DECLARACION DE FUNCIONES
void inorden(ARBOL);
void preorden(ARBOL);
void postorden(ARBOL);
void treefree(ARBOL);
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
 
/*-----------------------<FUNCION PRINCIPAL>--------------------------------*/
 
 
main(){
int i;                          //CONTADOR
char newnod,chain[200],elementos;    //DECLARACION DE CADENA,BANDERA Y VARIABLE QUE CONTIENE EL NUEVO VALOR A INSERTAR EN EL ARBOL
clrscr();
ARBOL raiz=NULL;                //DECLARACION DE VARIABLE DE TIPO ARBOL
printf("\n\n\tIntroduzca una cadena de caracteres (max. 200 elementos):\n");
gets(chain);
elementos=strlen(chain);         //CHECA EL TAMA¥O DE LA CADENA Y ESTABLECE EL NUMERO DE NODOS DEL ARBOL
for(i=1;i<=elementos;i++)  {
       newnod=chain[i-1];
     insertanodonuevo(&raiz,newnod);
 
}
printf("\n\n preorden ¯¯\t");
preorden(raiz);                 //LLAMADO A FUNCION DE RECORRIDO EN PREORDEN
printf("\n\n inorden  ¯¯\t");
inorden(raiz);                  //LLAMADO A FUNCION DE RECORRIDO EN INORDEN
printf("\n\n postorden ¯¯\t");
postorden(raiz);                //LLAMADO A FUNCION DE RECORRIDO EN POSTORDEN
getch();
treefree(raiz);            //LIBERACION DE MEMORIA DEL ARBOL.
raiz=NULL;                  //ASIGNACION DE UN VALOR NULO A LA RAIZ.
return 0;
}
 
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
/*-CREA UN NUEVO NODO Y COLOCA LOS VALORES DEL NUEVO ELEMENTO EN LA POSICION CORRESPONDIENTE */
 
void insertanodonuevo(ARBOL *rarbol,int nuevo){
 if(*rarbol==NULL){                          //CREACION DE UN NUEVO NODO
 *rarbol=(NODO *)malloc(sizeof(NODO));
 if(*rarbol!=NULL){
             //ASIGNACION DE VALORES NUEVOS EN EL NODO NUEVO
 (*rarbol)->info=nuevo;
 (*rarbol)->izqnodo =NULL;
 (*rarbol)->dernodo=NULL;
 }
 else{printf("\n­­­­ Memoria No Disponible !!!!\n");}
 }
 else
         if(nuevo<(*rarbol)->info)  //checa si el elemento nuevo es mayor que el elemento padre
                 insertanodonuevo(&((*rarbol)->izqnodo),nuevo);  //coloca el elemento a la izquierda del padre o raiz
         else
                 if(nuevo>(*rarbol)->info)  //checa si el elemento nuevo es menor que el elemento padre
                          insertanodonuevo(&((*rarbol)->dernodo),nuevo);  //coloca el elemento a la derecha del padre o raiz
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
//FUNCION ITERATIVA LA CUAL RECORRE EL ARBOL IMPRIMIENDO SIEMPRE EL VALOR
//QUE CONTIENE LA RAIZ,DESPUES LA RAMA IZQUIERDA,LUEGO LA RAMA DERECHA,SIEMPRE
//Y CUANDO LA RAIZ SEA DIFERENTE DE UN VALOR NULO, SI ES NULO SALTA A LA SIGUIENTE INSTRUCCION.
void preorden(ARBOL rarbol){
if(rarbol!=NULL){
printf(" %c ",rarbol->info);
preorden(rarbol->izqnodo);
preorden(rarbol->dernodo);
 
}
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
//FUNCION ITERATIVA LA CUAL RECORRE EL ARBOL BUSCANDO EL NODO MAS IZQUIERDO
//QUE CONTIENE EL ARBOL O SEA HASTA QUE LA RAMA DEL ULTIMO NODO SEA NULO,LUEGO LA IMPRIME,DESPUES
//DESPUES LA RAIZ DEL SUB-ARBOL,Y LUEGO EL NODO DE LA DERECHA.
 
void inorden(ARBOL rarbol){
if(rarbol!=NULL){
inorden(rarbol->izqnodo);
printf(" %c ",rarbol->info);
inorden(rarbol->dernodo);
 
}
 
}
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
//FUNCION ITERATIVA LA CUAL RECORRE EL ARBOL BUSCANDO EL NODO QUE ESTA MAS A LA IZQUIERDA
//LUEGO EL NODO DE LA DERECHA Y LUEGO LA RAIZ DE ESE SUB-ARBOL
void postorden(ARBOL rarbol){
if(rarbol!=NULL){
postorden(rarbol->izqnodo);
postorden(rarbol->dernodo);
printf(" %c ",rarbol->info);
}
}
/**-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
//FUNCION ITERATIVA IDENTICA AL RECORRIDO EN POSTORDEN LA UNICA DIFERENCIA
//ES QUE EN VEZ DE IMPRIMIR EN PANTALLA EL VALOR DE UN NODO ESTE ES
//ELIMINADO DEL ARBOL LIBERANDO LA MEMORIA CON LA FUNCION free(), ELEGI ESTA
//FORMA YA QUE SE ELIMINA PRIMERO LOS NODOS HIJO DE EL SUB-ARBOL Y LUEGO LA RAIZ
//YA QUE SI SE ELIMINA LA RAIZ PRIMERO, LOS DATOS DE LOS HIJOS SE DESCONECTAN
//DEL ARBOL PERO LA MEMORIA QUE OCUPABAN SIGUE SIENDO UTILIZADA Y DE ESTA FORMA
//SE ELIMINA EL ARBOL DE ABAJO HACIA ARRIBA (O SEA DE LOS HIJOS A LA RAIZ).
 
void treefree(ARBOL rarbol){
if(rarbol!=NULL){
treefree(rarbol->izqnodo);
treefree(rarbol->dernodo);
free(rarbol);
}

}