• Miércoles 1 de Mayo de 2024, 22:34

Autor Tema:  Validar Un Valor Decimal  (Leído 2872 veces)

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Validar Un Valor Decimal
« en: Martes 18 de Agosto de 2009, 06:41 »
0
ENCUAENTRAN ALGUN ERROR EN ESTE CODIGO....
NO ME CORRE OK...

Código: C++
  1.  
  2. # include <stdio.h>
  3. # include <conio.h>
  4. # include <string.h>
  5. # include <stdlib.h>
  6. char cadena[100];
  7. int estado=0;
  8. int i=0;
  9. main ()
  10. {
  11.     printf("INGRESE UN VALOR DECIMAL  :");
  12.     gets(cadena);
  13.    
  14.     for
  15.         (i=0;((i<strlen(cadena))&&(estado!=3));i++)
  16.         {
  17.             if (estado==0)
  18.                 {
  19.                     if ((cadena[i]>=48)&&(cadena[i]<=57))
  20.                         estado=1;
  21.                     if (cadena[i]=='.')
  22.                         estado=2;
  23.                     else estado=4;
  24.                 }
  25.             else
  26.             if (estado==1)
  27.                 {
  28.                     if ((cadena[i] >=48)&&(cadena[i]<=57))
  29.                         estado=1;
  30.                     if (cadena[i]=='.')
  31.                         estado=2;
  32.                     else estado=5;
  33.                 }
  34.             else
  35.             if (estado==2)
  36.                 {
  37.                     if ((cadena[i] >=48)&&(cadena[i]<=57))
  38.                         estado=3;
  39.                     else estado=6;
  40.                 }
  41.             else  
  42.             if (estado==3)
  43.                 {
  44.                     if ((cadena[i] >=48)&&(cadena[i]<=57))
  45.                         estado=3;
  46.                     else estado=7;
  47.                 }
  48.         }
  49.             if (estado==1)
  50.                 printf("EL NUMERO ES DECIMAL");  
  51.         {
  52.             if (estado==2)
  53.                 printf("EL NUMERO CONTIENE PUNTO");  
  54.                 {
  55.                     if (estado==3)
  56.                         printf("EL NUMERO DECIMAL ES CORRECTO");
  57.                     {
  58.                         if (estado==4)
  59.                             printf("ERROR: DEBE EMPEZAR CON NUMERO O PUNTO");
  60.                         {
  61.                             if (estado==5)
  62.                                 printf("ERROR: SOLO SE ACEPTAN NUMEROS");
  63.                             {
  64.                                 if (estado==6)
  65.                                     printf("DEBE INGRESAR PUNTO DECIMAL");
  66.                                 {
  67.                                     if (estado==7)
  68.                                         printf("SOLO SE HACEPTAN NUMEROS");  
  69.                                 }
  70.                             }
  71.                         }
  72.                     }
  73.                 }
  74.             getch();   
  75.         }
  76. }
  77.  
  78.  
« última modificación: Martes 18 de Agosto de 2009, 22:29 por infonet »

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Re: Validar Un Valor Decimal
« Respuesta #1 en: Martes 18 de Agosto de 2009, 22:28 »
0
SOLUCIONADO......
Código: C++
  1.  
  2.  
  3. # include <stdio.h>
  4. # include <conio.h>
  5. # include <string.h>
  6. # include <stdlib.h>
  7. char cadena[100];
  8. int estado=0;
  9. int i=0;
  10.  
  11. main ()
  12.  
  13. {
  14.     printf("INGRESE UN VALOR DECIMAL  :");
  15.     gets(cadena);
  16.     for
  17.  (i=0;((i<strlen(cadena))&&(estado!=3));i++)
  18.  {
  19.      if (estado==0)
  20.      {
  21.       if ((cadena[i]>=48)&&(cadena[i]<=57))
  22.    estado=1;
  23.  
  24.       else
  25.  
  26.          if (cadena[i]=='.')
  27.       estado=2;
  28.          else estado=4;
  29.      }
  30.  
  31.      else //else1
  32.  
  33.      {
  34.         if (estado==1)
  35.         {
  36.      if ((cadena[i] >=48)&&(cadena[i]<=57))
  37.    estado=1;
  38.  
  39.      else
  40.  
  41.          if (cadena[i]=='.')
  42.       estado=2;
  43.          else estado=5;
  44.         }
  45.  
  46.         else //else2
  47.  
  48.         {
  49.      if (estado==2)
  50.      {
  51.        if ((cadena[i] >=48)&&(cadena[i]<=57))
  52.      estado=3;
  53.        else estado=6;
  54.      }
  55.  
  56.      else //else3
  57.      {
  58.  
  59.         if (estado==3)
  60.         {
  61.    if ((cadena[i] >=48)&&(cadena[i]<=57))
  62.         estado=3;
  63.    else estado=7;
  64.         }
  65.  
  66.      }//else3
  67.   }//else2
  68.     }//else1
  69.  }// for
  70.      if (estado==1)
  71.   printf("EL NUMERO ES DECIMAL");
  72.      if (estado==2)
  73.   printf("EL NUMERO CONTIENE PUNTO");
  74.      if (estado==3)
  75.   printf("EL NUMERO DECIMAL ES CORRECTO");
  76.      if (estado==4)
  77.   printf("ERROR: DEBE EMPEZAR CON NUMERO O PUNTO");
  78.      if (estado==5)
  79.   printf("ERROR: SOLO SE ACEPTAN NUMEROS");
  80.      if (estado==6)
  81.   printf("DEBE INGRESAR PUNTO DECIMAL");
  82.      if (estado==7)
  83.   printf("SOLO SE HACEPTAN NUMEROS");
  84.  
  85.      getch();
  86. }
  87.  
  88.  
  89.  

Dynyck

  • Nuevo Miembro
  • *
  • Mensajes: 17
    • Ver Perfil
Re: Validar Un Valor Decimal
« Respuesta #2 en: Martes 18 de Agosto de 2009, 23:05 »
0
Una duda... Será que solo quieres validar como dice el título... entonces yo ocuparía directamente una entrada tipo float y luego la compararía con la parte entera, algo así:
Código: C++
  1.  
  2. float num;
  3. std::cin>>num;
  4. if(num!=(int)num){std::cout<<"El numero contiene punto y es decimal"<<std::endl;}
  5.  

Disculpas si me he equivocado.

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Re: Validar Un Valor Decimal
« Respuesta #3 en: Martes 18 de Agosto de 2009, 23:12 »
0
a ver probraremos con el float....
Muchas Gracias estimado....

Dynyck

  • Nuevo Miembro
  • *
  • Mensajes: 17
    • Ver Perfil
Re: Validar Un Valor Decimal
« Respuesta #4 en: Martes 18 de Agosto de 2009, 23:32 »
0
Fijate porque la verdad no me acuerdo si era (int) var o int (var), es simplemente comparar la parte entera con el número.