Advertisement
Brandford

cmusic

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