Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************************************
- * cmusic
- *
- * Dec 11 15:30:35 2007
- * Copyleft 2007 Brandford
- * brandford.tm@gmail.com | victorhugo.morales@redsalud.gov.cl
- *
- * Update: Apr 20 2013: Project Version - Integrated on Operating System (Unix Family)
- * 548 Lines of Code
- *********************************************************************************************/
- /*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*******************************************************************************************
- * DECLARACION DE VARIABLES GLOBALES *
- *******************************************************************************************/
- #define MAX 17500
- /*******************************************************************************************
- * DECLARACION DE ESTRUCTURAS *
- *******************************************************************************************/
- struct TIE {
- char valider;
- char code[20];
- int type;
- char art[30];
- char CD[40];
- int sex;
- int val;
- };
- struct TIE reg;
- struct TIE *p_tie;
- struct TIE2 {
- char art[30];
- char code[30];
- int val;
- };
- struct TIE2 regplus;
- /*******************************************************************************************
- * DECLARACION DE FUNCIONES *
- *******************************************************************************************/
- void front(void);
- void exitc(void);
- int menu(void);
- int ingresar(struct TIE *, FILE **pplus);
- int leer(struct TIE *);
- int listar(struct TIE *, long search, int *);
- int listarall(struct TIE *, long search);
- long readn(void);
- int delete(FILE **parch);
- int deletereg(FILE **parch);
- int listarplus(struct TIE2 *, long search);
- /*******************************************************************************************
- * VARIABLES GLOBALES *
- *******************************************************************************************/
- char man[]= {"HOMBRE"};
- char girl[]= {"MUJER"};
- char rock[]= {"ROCK"};
- char salsa[]= {"SALSA"};
- char roman[]= {"ROMANTICA"};
- /*******************************************************************************
- * FUNCION PRINCIPAL *
- *******************************************************************************/
- int main(void)
- {
- FILE *parch, *ptemp, *pplus;
- static int resp;
- static long search;
- short int i;
- int control[20];
- int sea_reg;
- int deleter;
- atexit(exitc);
- front();
- parch= fopen("general.dat" , "r+b");
- if(!parch)
- {
- parch= fopen("general.dat" , "w+b");
- }
- if(parch == NULL)
- {
- system("clear");
- printf("\n\n\n\t\t\t");
- puts("ERROR AL ABIR EL ARCHIVO . . .");
- getchar();
- exit(-1);
- }
- do {
- resp= menu();
- switch(resp)
- {
- case '1':
- {
- ingresar(®, &pplus);
- fseek(parch, 0, SEEK_END);
- fwrite(®, sizeof(struct TIE), 1, parch);
- break;
- }
- case '2':
- {
- system("clear");
- printf("\t\t=======================\n");
- printf("\t\tBUSQUEDA DE REGISTROS\n");
- printf("\t\t=======================\n\n\n");
- printf("\n====|Registro|==========================|Artista|==============\n\n\n");
- /*if(reg.valid != 1)
- exit(1); */
- rewind(parch);
- search= 0;
- while( fread(®, sizeof(struct TIE), 1, parch) )
- {
- listar(®, search++, control);
- }
- printf("\n\n===> Ingrese Registro a consultar: \t");
- //scanf("%d", &sea_reg);
- sea_reg= readn();
- for(i=0 ; i<search ; i++)
- {
- if( control[i] == sea_reg)
- {
- fseek(parch, sea_reg*sizeof(struct TIE), SEEK_SET);
- fread(®, sizeof(struct TIE), 1, parch);
- leer(®);
- }
- }
- break;
- }
- case '3':
- {
- system("clear");
- rewind(parch);
- search= 0;
- printf("\t\t==================\n");
- printf("\t\t LISTAR REGISTROS\n");
- printf("\t\t==================\n\n\n\n");
- while( fread(®, sizeof(struct TIE), 1, parch) )
- {
- listarall(®, search++);
- }
- break;
- }
- case '4':
- {
- delete(&parch);
- deletereg(&parch);
- break;
- }
- case '5':
- {
- FILE *pplus;
- pplus= fopen("plus.dat" , "r+b");
- if(pplus == NULL)
- {
- system("clear");
- printf("\n\n\n\tERROR NO HA INGRESADO NINGUN REGISTRO . . . ");
- getchar();
- exit(-1);
- }
- system("clear");
- printf("\t\t==============================\n");
- printf("\t\t LISTAR REGISTROS MAS CAROS\n");
- printf("\t\t==============================\n\n");
- printf("\n//Mayor a 17500//\n\n\n");
- rewind(pplus);
- search= 0;
- while( fread(®plus, sizeof(struct TIE), 1, pplus) )
- {
- listarplus(®plus, search++);
- }
- break;
- case '0':
- {
- return -1;
- break;
- }
- }
- default: system("clear");
- }
- } while( resp != '0');
- fclose(pplus);
- fclose(parch);
- return 0;
- }
- /*******************************************************************************
- * ELIMINA UN REGISTRO *
- *******************************************************************************/
- int delete(FILE **parch)
- {
- static int deleter;
- int search;
- int control[20];
- system("clear");
- printf("\t\t\t===================\n");
- printf("\t\t\t BORRAR REGISTROS\n");
- printf("\t\t\t===================\n\n\n");
- rewind(*parch);
- search= 0;
- printf("\n====|Registro|==========================|Artista|==============\n\n\n");
- while( fread(®, sizeof(struct TIE), 1, *parch) )
- {
- listar(®, search++, control);
- }
- printf("\n\n\n\nIngrese Numero registro a eliminar: \t");
- deleter= readn();
- fseek(*parch, deleter*sizeof(struct TIE), SEEK_SET);
- fread(®, sizeof(struct TIE), 1, *parch);
- reg.valider= 'N';
- fseek(*parch, deleter*sizeof(struct TIE), SEEK_SET);
- fwrite(®, sizeof(struct TIE), 1, *parch);
- }
- /*******************************************************************************
- * ELIMINA UN REGISTRO MARCADO COMO BORRADO, REINICIANDO EL CONTADOR A 0
- *******************************************************************************/
- int deletereg(FILE **parch)
- {
- FILE *ptemp;
- struct TIE reg;
- ptemp= fopen("general.tmp" , "wb");
- if( ptemp == NULL )
- {
- system("clear");
- printf("\n\n\n\n\n\t\t\t");
- printf("ERROR AL ABRIR EL ARCHIVO");
- getchar();
- exit(-1);
- }
- rewind(*parch);
- while( fread(®, sizeof(struct TIE), 1, *parch) )
- {
- if( reg.valider == 'S' )
- {
- fwrite(®, sizeof(struct TIE), 1, ptemp);
- }
- }
- fclose(ptemp);
- fclose(*parch);
- remove("general.bak");
- rename("general.dat", "general.bak" );
- rename("general.tmp", "general.dat" );
- *parch= fopen("general.dat" , "r+b");
- }
- /*******************************************************************************
- * LISTA TODOS LOS REGISTROS VALIDOS, CON TODA LA INFORMACION INGRESADA *
- *******************************************************************************/
- int listarall(struct TIE *reg, long search)
- {
- if( reg->valider == 'S' )
- {
- printf("\n\n|===================[\t\t%d\t\t]=========================|\n", search);
- printf("\nNombre Artista\t\t\t: %s", reg -> art);
- printf("\nNombre CD\t\t\t: %s", reg -> CD);
- if(reg -> sex == 1)
- printf("\nSexo del Artista\t\t: %s", man);
- else printf("\nSexo del Artista\t\t: %s", girl);
- if( reg -> type == 1)
- printf("\nEstilo de Musica\t\t: %s\n", rock);
- else if( reg -> type == 2)
- printf("\nEstilo de Musica\t\t: %s\n", salsa);
- else printf("\nEstilo de Musica\t\t: %s\n", roman);
- printf("\nPrecio del CD\t\t\t: %d", reg->val);
- printf("\nCodigo del Venta\t\t: %s", reg->code);
- //printf("\n
- }
- //printf("\n");
- getchar();
- }
- /*******************************************************************************
- * LISTA TODOS LOS REGITROS, PERO SOLO CON EL NOMBRE DEL ARTISTA *
- *******************************************************************************/
- int listar(struct TIE *reg, long search, int *control)
- {
- if(reg -> valider == 'S')
- {
- control[search]= search;;
- printf("[\t%d\t]\t\t[\t%s\t]", search, reg-> art);
- }
- printf("\n");
- }
- /*******************************************************************************
- * LISTA LOS REGISTROS CON LOS ARTISTAS CON CDS MAS CAROS *
- *******************************************************************************/
- int listarplus(struct TIE2 *regplus, long search)
- {
- if(regplus -> val >= MAX)
- {
- printf("\n\n|===================[\t\t%d\t\t]=========================|\n", search);
- printf("\nNombre Artista\t\t\t: %s", regplus -> art);
- printf("\nValor del CD\t\t\t: %d", regplus -> val);
- printf("\nCodigo del CD\t\t\t: %s", regplus -> code);
- }
- getchar();
- }
- /*******************************************************************************
- * BUSCA UN REGISTRO, MEDIANTE EL INGRESO DE ALGUN NUMERO DE REGISTRO *
- *******************************************************************************/
- int leer(struct TIE *reg)
- {
- printf("\n\n================================================================\n");
- if( reg->valider == 'S' )
- {
- printf("\nNombre Artista\t\t\t: %s", reg -> art);
- printf("\nNombre CD\t\t\t: %s", reg -> CD);
- if(reg -> sex == 1)
- printf("\nSexo del Artista\t\t: %s", man);
- else printf("\nSexo del Artista\t\t: %s", girl);
- if( reg -> type == 1)
- printf("\nEstilo de Musica\t\t: %s", rock);
- else if( reg -> type == 2)
- printf("\nEstilo de Musica\t\t: %s", salsa);
- else printf("\nEstilo de Musica\t\t: %s", roman);
- printf("\nPrecio del CD\t\t\t: %d", reg->val);
- printf("\nCodigo del Venta\t\t: %s", reg->code);
- }
- printf("\n\n================================================================");
- printf("\nPresione <ENTER> para continuar . . .");
- getchar();
- }
- /*******************************************************************************
- * INGRESA LA INFORMACION A LOS FICHEROS GENERAL.DAT Y PLUS.DAT *
- *******************************************************************************/
- int ingresar(struct TIE *reg, FILE **pplus)
- {
- system("clear");
- short int i;
- static char type[10];
- static char sex[10];
- static char val[10];
- static char code[20];
- printf("\t\t======================\n");
- printf("\t\t INGRESO DE REGISTROS\n");
- printf("\t\t======================\n\n\n\n");
- reg->valider= 'S';
- printf("Ingrese Nombre del Artista\t\t: ");
- fgets(reg->art, 30, stdin);
- for((i= strlen(reg->art) -1) ; (i && reg->art[i] < ' ') ; i--) // !
- reg->art[i]= 0;
- printf("\nIngrese Nombre del CD\t\t\t: ");
- fgets(reg->CD, 40, stdin);
- for((i= strlen(reg->CD) -1) ; (i && reg->CD[i] < ' ') ; i--)
- reg->CD[i]= 0;
- do {
- printf("\nSexo: 1(masculino), 2(femenino)\t\t: ");
- fgets(sex, 10, stdin);
- }while( ! ((sex[0] >= '1') && (sex[0] <= '2')) );
- reg->sex= atoi(sex);
- do {
- printf("\nTipo 1(rock), 2(salsa), 3(romantica)\t: ");
- fgets(type, 10, stdin);
- }while( ! ((type[0] >= '1') && (type[0] <= '3')) );
- reg->type= atoi(type);
- printf("\nNumero de codigo del CD\t\t\t: ");
- fgets(reg->code, 15, stdin);
- printf("\nValor Venta del CD\t\t\t: ");
- fgets(val, 10, stdin);
- reg->val= atoi(val);
- if(reg->val >= MAX)
- {
- *pplus= fopen("plus.dat" , "r+b");
- if(!*pplus)
- {
- *pplus= fopen("plus.dat" , "w+b");
- }
- if(pplus == NULL)
- {
- printf("\n\n\n\n\t\t\t");
- printf("ERROR AL ABRIR EL ARCHIVO");
- getchar();
- exit(-1);
- }
- strcpy(regplus.art, reg->art);
- strcpy(regplus.code, reg->code);
- regplus.val= reg->val;
- fseek(*pplus, 0, SEEK_END);
- fwrite(®plus, sizeof(struct TIE), 1, *pplus);
- fclose(*pplus);
- }
- }
- /*******************************************************************************
- * MENU PRINCIPAL QUE DERIVA A LAS DIFERENTES FUNCIONES *
- *******************************************************************************/
- int menu(void)
- {
- static char resp[3];
- do {
- system("clear");
- printf("\t================================================================\n");
- printf("\t\t\tM E N U P R I N C I P A L\n");
- printf("\t================================================================\n\n\n");
- printf("\n1- INGRESAR/AGREGAR DATOS");
- printf("\n2- BUSCAR DATOS");
- printf("\n3- LISTAR TODOS LOS DATOS");
- printf("\n4- ELIMINAR REGISTROS");
- printf("\n5- IMPRIMIR ARTISTAS MAS CAROS");
- printf("\n0- SALIR");
- printf("\n\n\nIngrese Opcion\t: ");
- fgets(resp, 3, stdin);
- } while( ! ((resp[0] >= '0') && (resp[0] <= '9')) );
- return (resp[0]);
- }
- /*******************************************************************************
- * LEE UN NUMERO, LO RETORNA MEDIANTE LA FUNCION ATOI QUE TOLERA QUE SEA CHAR *
- *******************************************************************************/
- long readn(void)
- {
- static char selected[3];
- fgets(selected, 3, stdin);
- return atoi(selected);
- }
- /*******************************************************************************
- * VENTANA DE BIENVENIDA *
- *******************************************************************************/
- void front(void)
- {
- system("clear");
- printf("\n\n\n\n\n\n\n");
- printf("==================================================================\n");
- printf("\t\t C - M U S I C\n");
- printf("==================================================================\n\n\n\n\n");
- printf("\n\n\n\nPresione <ENTER> para continuar . . .");
- getchar();
- }
- /*******************************************************************************
- * VENTANA SALIDA, MEDIANTE FUNCION ATEXIT *
- *******************************************************************************/
- void exitc(void)
- {
- system("clear");
- printf("\n\n\n\n\n\n\n");
- printf("\t\t F I N D E L P R O G R A M A . . .\n\n\n\n\n\n");
- printf("Presione cualquier tecla para salir . . .");
- getchar();
- system("clear");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement