Brandford

cmusic

Oct 26th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.64 KB | None | 0 0
  1. /********************************************************************************************
  2. *  cmusic
  3. *
  4. *  Dec 11 15:30:35 2007
  5. *  Copyleft  2007  Brandford
  6. *
  7. *  Update: Apr 20 2013: Project Version - Integrated on Operating System (Unix Family)
  8. *  548 Lines of Code
  9. *********************************************************************************************/
  10.  
  11. /*
  12.  *  This program is free software; you can redistribute it and/or modify
  13.  *  it under the terms of the GNU General Public License as published by
  14.  *  the Free Software Foundation; either version 2 of the License, or
  15.  *  (at your option) any later version.
  16.  *
  17.  *  This program is distributed in the hope that it will be useful,
  18.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  *  GNU General Public License for more details.
  21.  *
  22.  *  You should have received a copy of the GNU General Public License
  23.  *  along with this program; if not, write to the Free Software
  24.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31.  
  32. /*******************************************************************************************
  33.  *                      DECLARACION DE VARIABLES GLOBALES                          *
  34.  *******************************************************************************************/
  35. #define MAX 17500
  36.  
  37. /*******************************************************************************************
  38.  *                      DECLARACION DE ESTRUCTURAS                                 *
  39.  *******************************************************************************************/
  40. struct TIE {
  41.     char valider;
  42.     char code[20];  
  43.     int type;
  44.     char art[30];
  45.     char CD[40];
  46.     int sex;
  47.     int val;
  48. };
  49. struct TIE reg;
  50. struct TIE *p_tie;
  51.    
  52. struct TIE2 {
  53.     char art[30];
  54.     char code[30];
  55.     int val;
  56. };
  57. struct TIE2 regplus;
  58.    
  59.    
  60. /*******************************************************************************************
  61.  *                      DECLARACION DE FUNCIONES                                           *
  62.  *******************************************************************************************/
  63. void front(void);
  64. void exitc(void);
  65. int menu(void);
  66. int ingresar(struct TIE *, FILE **pplus);
  67. int leer(struct TIE *);      
  68. int listar(struct TIE *, long search, int *);
  69. int listarall(struct TIE *, long search);
  70. long readn(void);
  71. int delete(FILE **parch);
  72. int deletereg(FILE **parch);
  73. int listarplus(struct TIE2 *, long search);
  74.  
  75.  
  76. /*******************************************************************************************
  77.  *                     VARIABLES GLOBALES                                                  *
  78.  *******************************************************************************************/                                                                                                                                              
  79. char man[]= {"HOMBRE"};
  80. char girl[]= {"MUJER"};
  81. char rock[]= {"ROCK"};
  82. char salsa[]= {"SALSA"};
  83. char roman[]= {"ROMANTICA"};
  84.  
  85.  
  86. /*******************************************************************************
  87.  *                      FUNCION PRINCIPAL                                      *
  88.  *******************************************************************************/
  89. int main(void)
  90. {
  91.     FILE *parch, *ptemp, *pplus;
  92.    
  93.     static int resp;
  94.     static long search;
  95.     short int i;
  96.     int control[20];
  97.     int sea_reg;
  98.     int deleter;
  99.    
  100.     atexit(exitc);
  101.     front();
  102.        
  103.     parch= fopen("general.dat" , "r+b");
  104.     if(!parch)
  105.     {
  106.         parch= fopen("general.dat" , "w+b");
  107.        
  108.     }
  109.     if(parch == NULL)
  110.     {
  111.         system("clear");
  112.         printf("\n\n\n\t\t\t");
  113.         puts("ERROR AL ABIR EL ARCHIVO . . .");
  114.         getchar();
  115.         exit(-1);
  116.     }      
  117.        
  118.     do {
  119.             resp= menu();
  120.             switch(resp)
  121.             {
  122.                 case '1':
  123.                 {
  124.                     ingresar(&reg, &pplus);
  125.                     fseek(parch, 0, SEEK_END);
  126.                     fwrite(&reg, sizeof(struct TIE), 1, parch);
  127.                     break;
  128.                 }
  129.                
  130.                 case '2':
  131.                 {
  132.                     system("clear");
  133.                     printf("\t\t=======================\n");
  134.                     printf("\t\tBUSQUEDA DE REGISTROS\n");
  135.                     printf("\t\t=======================\n\n\n");
  136.                     printf("\n====|Registro|==========================|Artista|==============\n\n\n");
  137.                     /*if(reg.valid != 1)
  138.                         exit(1); */                    
  139.                     rewind(parch);
  140.                     search= 0;
  141.                     while( fread(&reg, sizeof(struct TIE), 1, parch) )
  142.                     {
  143.                         listar(&reg, search++, control);
  144.                     }
  145.                             printf("\n\n===> Ingrese Registro a consultar: \t");
  146.                             //scanf("%d", &sea_reg);
  147.                             sea_reg= readn();
  148.                             for(i=0 ; i<search ; i++)
  149.                             {
  150.                                 if( control[i] == sea_reg)
  151.                                 {
  152.                                     fseek(parch, sea_reg*sizeof(struct TIE), SEEK_SET);
  153.                                     fread(&reg, sizeof(struct TIE), 1, parch);
  154.                                     leer(&reg);    
  155.                                 }
  156.                             }
  157.                    
  158.                    
  159.                     break;
  160.                 }
  161.                
  162.                 case '3':
  163.                 {
  164.                     system("clear");
  165.                     rewind(parch);
  166.                     search= 0;
  167.                     printf("\t\t==================\n");
  168.                     printf("\t\t LISTAR REGISTROS\n");
  169.                     printf("\t\t==================\n\n\n\n");
  170.                    
  171.                     while( fread(&reg, sizeof(struct TIE), 1, parch) )
  172.                     {
  173.                         listarall(&reg, search++);
  174.                     }
  175.                                        
  176.                     break;                 
  177.                 }
  178.                
  179.                 case '4':
  180.                 {
  181.                    
  182.                     delete(&parch);
  183.                    
  184.                     deletereg(&parch);
  185.  
  186.                     break;
  187.                 }
  188.                
  189.                 case '5':
  190.                 {
  191.                    
  192.                     FILE *pplus;
  193.                     pplus= fopen("plus.dat" , "r+b");
  194.                     if(pplus == NULL)
  195.                     {
  196.                         system("clear");
  197.                         printf("\n\n\n\tERROR NO HA INGRESADO NINGUN REGISTRO . . . ");
  198.                         getchar();
  199.                         exit(-1);
  200.                     }
  201.                     system("clear");
  202.                     printf("\t\t==============================\n");
  203.                     printf("\t\t LISTAR REGISTROS MAS CAROS\n");
  204.                     printf("\t\t==============================\n\n");
  205.                     printf("\n//Mayor a 17500//\n\n\n");
  206.                    
  207.                     rewind(pplus);
  208.                     search= 0;                 
  209.                     while( fread(&regplus, sizeof(struct TIE), 1, pplus) )
  210.                     {
  211.                         listarplus(&regplus, search++);
  212.                        
  213.                     }
  214.                    
  215.                     break;
  216.                
  217.                 case '0':
  218.                      {
  219.                         return -1;
  220.                         break;
  221.                 }
  222.             }
  223.                                    
  224.                
  225.                 default: system("clear");
  226.                    
  227.                
  228.             }
  229.         } while( resp != '0');
  230.     fclose(pplus);
  231.     fclose(parch);
  232.     return 0;
  233. }
  234.  
  235.  
  236. /*******************************************************************************
  237.  *                     ELIMINA UN REGISTRO                                     *
  238.  *******************************************************************************/
  239. int delete(FILE **parch)
  240. {
  241.     static int deleter;
  242.     int search;
  243.     int control[20];
  244.    
  245.     system("clear");
  246.     printf("\t\t\t===================\n");
  247.     printf("\t\t\t BORRAR REGISTROS\n");
  248.     printf("\t\t\t===================\n\n\n");
  249.    
  250.     rewind(*parch);
  251.     search= 0;
  252.     printf("\n====|Registro|==========================|Artista|==============\n\n\n");
  253.     while( fread(&reg, sizeof(struct TIE), 1, *parch) )
  254.     {
  255.         listar(&reg, search++, control);
  256.     }
  257.    
  258.    
  259.    
  260.     printf("\n\n\n\nIngrese Numero registro a eliminar: \t");
  261.     deleter= readn();
  262.    
  263.     fseek(*parch, deleter*sizeof(struct TIE), SEEK_SET);
  264.     fread(&reg, sizeof(struct TIE), 1, *parch);
  265.            
  266.     reg.valider= 'N';
  267.    
  268.     fseek(*parch, deleter*sizeof(struct TIE), SEEK_SET);
  269.     fwrite(&reg, sizeof(struct TIE), 1, *parch);
  270.        
  271.        
  272. }
  273.  
  274.  
  275. /*******************************************************************************
  276.  * ELIMINA UN REGISTRO MARCADO COMO BORRADO, REINICIANDO EL CONTADOR A 0
  277.  *******************************************************************************/                                                                
  278. int deletereg(FILE **parch)
  279. {
  280.     FILE *ptemp;
  281.     struct TIE reg;
  282.    
  283.     ptemp= fopen("general.tmp" , "wb");
  284.     if( ptemp == NULL )
  285.     {
  286.         system("clear");
  287.         printf("\n\n\n\n\n\t\t\t");
  288.         printf("ERROR AL ABRIR EL ARCHIVO");
  289.         getchar();
  290.         exit(-1);
  291.     }
  292.    
  293.     rewind(*parch);
  294.     while( fread(&reg, sizeof(struct TIE), 1, *parch) )
  295.     {
  296.         if( reg.valider == 'S' )
  297.         {
  298.             fwrite(&reg, sizeof(struct TIE), 1, ptemp);
  299.         }
  300.     }
  301.    
  302.     fclose(ptemp);
  303.     fclose(*parch);
  304.     remove("general.bak");
  305.     rename("general.dat", "general.bak" );
  306.     rename("general.tmp", "general.dat" );
  307.     *parch= fopen("general.dat" , "r+b");
  308. }
  309.    
  310.    
  311. /*******************************************************************************
  312.  *  LISTA TODOS LOS REGISTROS VALIDOS, CON TODA LA INFORMACION INGRESADA       *
  313.  *******************************************************************************/
  314. int listarall(struct TIE *reg, long search)
  315. {
  316.    
  317.     if( reg->valider == 'S' )
  318.     {
  319.         printf("\n\n|===================[\t\t%d\t\t]=========================|\n", search);
  320.         printf("\nNombre Artista\t\t\t: %s", reg -> art);
  321.         printf("\nNombre CD\t\t\t: %s", reg -> CD);
  322.         if(reg -> sex == 1)
  323.             printf("\nSexo del Artista\t\t: %s", man);
  324.         else printf("\nSexo del Artista\t\t: %s", girl);
  325.         if( reg -> type == 1)
  326.             printf("\nEstilo de Musica\t\t: %s\n", rock);
  327.         else if( reg -> type == 2)
  328.             printf("\nEstilo de Musica\t\t: %s\n", salsa);
  329.         else printf("\nEstilo de Musica\t\t: %s\n", roman);
  330.         printf("\nPrecio del CD\t\t\t: %d", reg->val); 
  331.         printf("\nCodigo del Venta\t\t: %s", reg->code);
  332.         //printf("\n           
  333.     }
  334.     //printf("\n");
  335.  
  336.     getchar();
  337. }  
  338.  
  339.  
  340. /*******************************************************************************
  341.  * LISTA TODOS LOS REGITROS, PERO SOLO CON EL NOMBRE DEL ARTISTA               *
  342.  *******************************************************************************/
  343. int listar(struct TIE *reg, long search, int *control)
  344. {
  345.    
  346.     if(reg -> valider == 'S')
  347.     {
  348.         control[search]= search;;
  349.         printf("[\t%d\t]\t\t[\t%s\t]", search, reg-> art);
  350.  
  351.     }
  352.    
  353.     printf("\n");
  354.  
  355. }
  356.  
  357.  
  358. /*******************************************************************************
  359.  * LISTA LOS REGISTROS CON LOS ARTISTAS CON CDS MAS CAROS                      *
  360.  *******************************************************************************/
  361. int listarplus(struct TIE2 *regplus, long search)
  362. {
  363.  
  364.     if(regplus -> val >= MAX)
  365.     {
  366.         printf("\n\n|===================[\t\t%d\t\t]=========================|\n", search);
  367.         printf("\nNombre Artista\t\t\t: %s", regplus -> art);
  368.         printf("\nValor del CD\t\t\t: %d", regplus -> val);
  369.         printf("\nCodigo del CD\t\t\t: %s", regplus -> code);
  370.        
  371.     }
  372.    
  373.     getchar();
  374. }
  375.        
  376.        
  377. /*******************************************************************************
  378.  * BUSCA UN REGISTRO, MEDIANTE EL INGRESO DE ALGUN NUMERO DE REGISTRO          *
  379.  *******************************************************************************/
  380. int leer(struct TIE *reg)
  381. {
  382.     printf("\n\n================================================================\n");
  383.     if( reg->valider == 'S' )
  384.     {
  385.  
  386.         printf("\nNombre Artista\t\t\t: %s", reg -> art);
  387.         printf("\nNombre CD\t\t\t: %s", reg -> CD);
  388.         if(reg -> sex == 1)
  389.             printf("\nSexo del Artista\t\t: %s", man);
  390.         else printf("\nSexo del Artista\t\t: %s", girl);
  391.         if( reg -> type == 1)
  392.             printf("\nEstilo de Musica\t\t: %s", rock);
  393.         else if( reg -> type == 2)
  394.             printf("\nEstilo de Musica\t\t: %s", salsa);
  395.         else printf("\nEstilo de Musica\t\t: %s", roman);  
  396.         printf("\nPrecio del CD\t\t\t: %d", reg->val); 
  397.         printf("\nCodigo del Venta\t\t: %s", reg->code);       
  398.     }
  399.     printf("\n\n================================================================");
  400.     printf("\nPresione <ENTER> para continuar . . .");
  401.     getchar();
  402.    
  403. }
  404.  
  405.  
  406. /*******************************************************************************
  407.  * INGRESA LA INFORMACION A LOS FICHEROS GENERAL.DAT Y PLUS.DAT                *
  408.  *******************************************************************************/
  409. int ingresar(struct TIE *reg, FILE **pplus)
  410. {
  411.     system("clear");
  412.    
  413.     short int i;
  414.     static char type[10];
  415.     static char sex[10];
  416.     static char val[10];
  417.     static char code[20];
  418.    
  419.     printf("\t\t======================\n");
  420.     printf("\t\t INGRESO DE REGISTROS\n");
  421.     printf("\t\t======================\n\n\n\n");
  422.     reg->valider= 'S';
  423.     printf("Ingrese Nombre del Artista\t\t: ");
  424.     fgets(reg->art, 30, stdin);
  425.     for((i= strlen(reg->art) -1) ; (i && reg->art[i] < ' ') ; i--) // !
  426.         reg->art[i]= 0;
  427.     printf("\nIngrese Nombre del CD\t\t\t: ");
  428.     fgets(reg->CD, 40, stdin);
  429.     for((i= strlen(reg->CD) -1) ; (i && reg->CD[i] < ' ') ; i--)
  430.         reg->CD[i]= 0;
  431.    
  432.     do {
  433.         printf("\nSexo: 1(masculino), 2(femenino)\t\t: ");
  434.         fgets(sex, 10, stdin);
  435.     }while( ! ((sex[0] >= '1') && (sex[0] <= '2')) );
  436.     reg->sex= atoi(sex);
  437.    
  438.     do {
  439.         printf("\nTipo 1(rock), 2(salsa), 3(romantica)\t: ");
  440.         fgets(type, 10, stdin);
  441.     }while( ! ((type[0] >= '1') && (type[0] <= '3')) );
  442.     reg->type= atoi(type);
  443.    
  444.  
  445.     printf("\nNumero de codigo del CD\t\t\t: ");
  446.     fgets(reg->code, 15, stdin);
  447.    
  448.     printf("\nValor Venta del CD\t\t\t: ");
  449.     fgets(val, 10, stdin);
  450.     reg->val= atoi(val);
  451.    
  452.     if(reg->val >= MAX)
  453.     {
  454.            
  455.            *pplus= fopen("plus.dat" , "r+b");
  456.             if(!*pplus)
  457.             {
  458.                 *pplus= fopen("plus.dat" , "w+b");
  459.             }
  460.             if(pplus == NULL)
  461.             {
  462.                 printf("\n\n\n\n\t\t\t");
  463.                 printf("ERROR AL ABRIR EL ARCHIVO");
  464.                 getchar();
  465.                 exit(-1);
  466.             }
  467.    
  468.             strcpy(regplus.art, reg->art);
  469.             strcpy(regplus.code, reg->code);
  470.             regplus.val= reg->val;
  471.    
  472.             fseek(*pplus, 0, SEEK_END);
  473.             fwrite(&regplus, sizeof(struct TIE), 1, *pplus);
  474.            fclose(*pplus);
  475.     }
  476.        
  477.        
  478.    
  479.    
  480. }
  481.  
  482.    
  483. /*******************************************************************************
  484.  *    MENU PRINCIPAL QUE DERIVA A LAS DIFERENTES FUNCIONES                     *
  485.  *******************************************************************************/
  486. int menu(void)
  487. {
  488.     static char resp[3];
  489.     do {
  490.     system("clear");
  491.     printf("\t================================================================\n");
  492.     printf("\t\t\tM E N U    P R I N C I P A L\n");
  493.     printf("\t================================================================\n\n\n");
  494.     printf("\n1- INGRESAR/AGREGAR DATOS");
  495.     printf("\n2- BUSCAR DATOS");
  496.     printf("\n3- LISTAR TODOS LOS DATOS");
  497.     printf("\n4- ELIMINAR REGISTROS");
  498.     printf("\n5- IMPRIMIR ARTISTAS MAS CAROS");
  499.     printf("\n0- SALIR");
  500.     printf("\n\n\nIngrese Opcion\t: ");
  501.     fgets(resp, 3, stdin);
  502.     } while( ! ((resp[0] >= '0') && (resp[0] <= '9')) );
  503.     return (resp[0]);  
  504. }
  505.  
  506.  
  507. /*******************************************************************************
  508.  *  LEE UN NUMERO, LO RETORNA MEDIANTE LA FUNCION ATOI QUE TOLERA QUE SEA CHAR *
  509.  *******************************************************************************/
  510. long readn(void)
  511. {
  512.     static char selected[3];
  513.     fgets(selected, 3, stdin);
  514.     return atoi(selected);
  515. }
  516.  
  517.  
  518. /*******************************************************************************
  519.  *                     VENTANA DE BIENVENIDA                                   *
  520.  *******************************************************************************/
  521. void front(void)
  522. {
  523.      system("clear");
  524.      
  525.      printf("\n\n\n\n\n\n\n");
  526.      printf("==================================================================\n");
  527.      printf("\t\t       C  -  M  U  S  I  C\n");
  528.      printf("==================================================================\n\n\n\n\n");
  529.      printf("\n\n\n\nPresione <ENTER> para continuar . . .");
  530.      getchar();
  531. }
  532.  
  533.  
  534. /*******************************************************************************
  535.  *   VENTANA SALIDA, MEDIANTE FUNCION ATEXIT                                   *
  536.  *******************************************************************************/
  537. void exitc(void)
  538. {
  539.      system("clear");
  540.      
  541.      printf("\n\n\n\n\n\n\n");
  542.      printf("\t\t F I N   D E L   P R O G R A M A . . .\n\n\n\n\n\n");
  543.      printf("Presione cualquier tecla para salir . . .");
  544.      getchar();
  545.      system("clear");      
  546. }
Advertisement
Add Comment
Please, Sign In to add comment