Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/06/2010, 07:41
minette1988
 
Fecha de Ingreso: febrero-2010
Mensajes: 258
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: imprimir la deuda mayor de un cliente

Disculpad por no resaltar el código, lo muestro resaltado

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LEE_CHAR(c)\
  6.         c=getchar();\
  7.         while((c=='\n') || (c==' ') || (c=='\t'))\
  8.             c=getchar();
  9.  
  10. #define LEE_CAD(c,n) { int i=0; \
  11.                      c[i]=getchar();\
  12.              while((c[i]=='\n') || (c[i]=='\t')) c[i]=getchar();\
  13.              while ((c[i]!='\n') && (i < (n-1))){ \
  14.                 i++;\
  15.             c[i]=getchar(); }\
  16.              c[i]='\0'; }
  17.  
  18. #define TAM_NOMBRE 100
  19. #define TAM_DIR 100
  20. #define NUM_CLIENTES 10
  21.  
  22. struct cliente{
  23.     char nombre[TAM_NOMBRE];
  24.         char direccion[TAM_DIR];
  25.         double deuda;
  26. };
  27. int main(){
  28.     void crear_fich();
  29.         char sel;
  30.     FILE *pf; //Puntero a fichero
  31.  
  32.         /* Abre el fichero para trabajar con él en Lectura / Escritura */
  33.         if((pf = fopen("datos.dat", "wb+")) == NULL) {/*"rb+"*/
  34.            /* Si no existe, ejecuta el módulo que lo cree */
  35.        crear_fich();
  36.        /* Una vez creado lo habre en lectura/escritura */
  37.            if((pf = fopen("datos.dat", "rb+")) == NULL) {
  38.         perror("Fichero no accesible");
  39.                 exit(1);
  40.        }
  41.         }
  42.  
  43.     do{
  44.         printf("MENÚ\n----\n");
  45.        
  46.         printf("1). Dar de alta un cliente.\n");
  47.                 printf("2). Mostrar la deuda mayor.\n");
  48.                 printf("3). salir y eliminar.\n");
  49.                 printf("0). Salir\n\nOpción(0-2): ");
  50.  
  51.         do{
  52.             LEE_CHAR(sel);
  53.         }while( (sel<'0') || (sel>'3') );
  54.  
  55.         switch(sel){
  56.                 case '1':{
  57.                         crear_fich(pf);
  58.                         /* Una vez creado lo abre en lectura/escritura */
  59.                         if((pf = fopen("datos.dat", "rb+")) == NULL) {
  60.                         perror("Fichero no accesible");
  61.                                 exit(2);
  62.                         }
  63.                     break;}
  64.                                 case '2':{ deuda_mayor(pf);
  65.                                         break;}
  66.                                 case '3':{ fclose(pf); pf=NULL; remove("datos.dat"); sel= '0';
  67.                     break;}
  68.                 } //switch
  69.     }while(sel!='0');
  70.  
  71.     exit(0);
  72. }//main()
  73. ////////////////////////////////////////
  74. void crear_fich(FILE* pf) {
  75.          struct cliente cli[NUM_CLIENTES];
  76.          
  77.          char otro;
  78.  
  79.      if((pf = fopen("datos.dat", "ab")) == NULL) {
  80.         perror("Al crear el fichero de datos");
  81.                 exit(2);
  82.          }
  83.          
  84.          /* Lectura de los ciclistas */
  85.          do {
  86.        
  87.         cli.deuda = 0.0;
  88.                 printf("Nombre: "); LEE_CAD(cli.nombre, TAM_NOMBRE);
  89.                 printf("Dirección: "); LEE_CAD(cli.direccion, TAM_DIR);
  90.                 printf("Deuda: "); scanf("%lf", &cli.deuda);
  91.                 /*Escribe el cliente en el fichero */
  92.                 fwrite(&cli, sizeof(struct cliente), 1, pf);
  93.         printf("¿Otro? (s/n) ");
  94.                 LEE_CHAR(otro);
  95.         }while((otro=='s') || (otro=='S'));
  96.     if(fclose(pf) == EOF){
  97.         printf("Error al cerrar el fichero, compruebe si hay información.\n"); 
  98.                 exit(3);
  99.         }
  100. } /* crear_fich()*/
  101. //////////////////////////////////
  102. void deuda_mayor(FILE *pf){
  103.         struct cliente cli[NUM_CLIENTES];
  104.        
  105.         int deuda_mayor=0;
  106.  
  107.            if(cli.deuda > cli[deuda_mayor].deuda);
  108.               deuda_mayor=cli.deuda;
  109.        
  110.            printf("%s %.2lf\n", cli[deuda_mayor].nombre,cli[deuda_mayor].deuda);
  111.  
  112. }