/*Este programa es un ejemplo de base de datos primitivos muy a mi estilo*/
//Una lista doblemente enlazada Ejemplo
//Probado bajo Borland C++ Builder 6 con Code Guard y full debug
//inserta nombres en la ultima parte
//borra nombres sin importar la posicion
//muestra los nombres en retroceso y en anverso
//Guarda la lista en modo texto en un archivo
//carga la lista del archivo
#include <stdio.h>
#include <alloc.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
// ------------------------
void insertar(void);
void extraer(void);
void visualizar(void);
void guardar(void);
void cargar(void);
// -------------------------
struct listad
{
char prod[20];
int cant;
int desc;
int precio;
struct listad *ant;
struct listad *sig;
} *lista=NULL, *ante=NULL, *viz=NULL, *temp=NULL;
struct DB
{
char p[20];
int c;
int d;
int pr;
}*BD;
int n=0;
//-------------------------
main()
{
char op;
do
{
clrscr();
gotoxy(30,8);
printf("1.- Insertar");
gotoxy(30,10);
printf("2.-Borrar");
gotoxy(30,12);
printf("3.-Ver lista");
gotoxy(30,14);
printf("4.-Guardar lista");
gotoxy(30,16);
printf("5.-Cargar archivo");
op = getch();
switch (op)
{
case '1':
insertar();
break;
case '2':
extraer();
break;
case '3':
visualizar();
break;
case '4':
guardar();
break;
case '5':
cargar();
}
}while (op != 27);
lista = ante;
while(lista!=NULL)
{
ante = lista;
lista = lista->ant;
free(ante);
}
free(lista);
free(ante);
free(viz);
free(temp);
return 0;
}
//Ahora si a empezar
//-----------------------------------------------
void insertar(void)
{
//insertamos un elemento en la lista
clrscr();
lista = (struct listad *)malloc(sizeof(struct listad));
n = n+1;
printf("Escribe el producto %d: ",n);
gets(lista->prod);
printf("Escribe la cantidad %d: ",n);
scanf("%d",&lista->cant);
printf("Escribe el Descuento %d: ",n);
scanf("%d",&lista->desc);
printf("Escribe el precio %d: ",n);
scanf("%d",&lista->precio);
lista->sig = NULL;
if (ante == NULL)
{
lista->ant = NULL;
ante = lista;
}
else
{
lista->ant = ante;
ante->sig = lista;
ante = lista;
}
}
//------------------------------------------------
void extraer(void)
{
//borramos un elemento de la lista
char tempo[10];
if (ante == NULL)
return;
clrscr();
printf("Escribe que producto quieres borrar (junto con su descuento, precio y cantidad) : ");
gets(tempo);
lista = ante;
while(lista !=NULL)
{
if(strcmp(lista->prod,tempo)== 0)
{
if (strcmp(ante->prod, tempo)==0)
ante = lista->ant;
viz = lista->ant;
temp = lista->sig;
if (viz == NULL && temp == NULL)
{
free(lista);
ante = NULL;
return;
}
free(lista);
if (temp != NULL && viz != NULL)
viz->sig = temp;
else if (temp == NULL)
viz->sig = NULL;
if (temp != NULL)
temp->ant = viz;
else
temp = viz;
if (viz != NULL)
lista = viz;
else
lista = temp;
printf("Fue eliminado %s\n",tempo);
}
lista = lista->ant;
}
printf("se termino");
getch();
}
//--------------------------------------------------
void visualizar (void)
{
//vemos que hay en la lista
if (ante == NULL)
return;
lista = ante;
clrscr();
printf("Producto Cantidad Descuento Precio\n");
while(lista!=NULL)
{
printf("%s %d %d %d\n",lista->prod,lista->cant,lista->desc,lista->precio);
lista=lista->ant;
}
getch();
}
//---------------------------------------------------
//---------------------------------------------------
void guardar (void)
{
//esta funci¢n guardara en un archivo la lista
//primero la muestra (solo para acomodar el puntero)
if (ante==NULL)
return;
clrscr();
//ahora si
FILE *archivo;
if((archivo=fopen("Inventario.bdc","wb"))!=NULL)
{
lista = ante;
while(lista!=NULL)
{
strcpy(BD->p,lista->prod);
BD->c = lista->cant;
BD->d = lista->desc;
BD->pr = lista->precio;
printf("%s",lista->prod);
fwrite(BD, sizeof(struct DB), 1, archivo);
lista=lista->ant;
}
fclose(archivo);
printf("\nLa Base de Datos fue guardado con exito");
getch();
}
else
{
fclose(archivo);
printf("Error al intentar crear la base de datos.");
getch();
exit(1);
}
}
//--------------------------------------------------------------------------
void cargar(void)
{
FILE *archivo1;
clrscr();
if((archivo1=fopen("Inventario.bdc","rb"))!=NULL)
{
do
{
lista = (struct listad *)malloc(sizeof(struct listad));
fread(BD,sizeof(struct DB),1,archivo1);
if(feof(archivo1))
{
free(BD);
free(lista);
continue;
}
strcpy(lista->prod,BD->p);
printf("%s",BD->p);
lista->cant = BD->c;
lista->desc = BD->d;
lista->precio = BD->pr;
lista->sig = NULL;
if (ante == NULL)
{
lista->ant = NULL;
ante = lista;
}
else
{
lista->ant = ante;
ante->sig = lista;
ante = lista;
}
}while(!feof(archivo1) && ante != NULL);
fclose(archivo1);
printf("\nLa Base de Datos fue cargado con Exito.Ahora te la mostraremos\nPresiona una tecla para continuar");
getch();
visualizar();
}
else
{
fclose(archivo1);
printf("Error al intentar abrir la base de datos");
getch();
exit(1);
}
}