problemas con una producción como doble

  • arminho1
  • Newbie
  • Newbie
  • No Avatar
  • Registrado: Oct 04, 2003
  • Mensajes: 6
  • Status: Offline

Nota Octubre 26th, 2003, 9:09 pm

hola,
Yo escribí un programa que convierte una cadena de dígitos a un doble value.this funciona siempre y cuando no hay ningún punto en el ejemplo string.for, cuando la cadena es 1234, la salida es 1234,0000
pero cuando la entrada es como 0,1234 entonces el fails.who programa me puede ayudar.

Código: [ Select ]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double prob1(char * s); 
char inbuf[20];

int main()
{
    char inbuf[20];
  double i;

    while (fgets(inbuf, 20, stdin) != NULL)
    {
            i = prob1(inbuf);
            printf("n = %f\n", i);
  }
}

    double prob1(char * s)
    {
        int i,len;
        double digit,num;
        
        i=0;

        len = strlen(s);
        num = 0;
        while (i<len-1)
        {
            
            digit = s[i] - '0';
            num = num * 10 + digit;
            i++;
            
        }
            return num;
    }
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. double prob1(char * s); 
  5. char inbuf[20];
  6. int main()
  7. {
  8.     char inbuf[20];
  9.   double i;
  10.     while (fgets(inbuf, 20, stdin) != NULL)
  11.     {
  12.             i = prob1(inbuf);
  13.             printf("n = %f\n", i);
  14.   }
  15. }
  16.     double prob1(char * s)
  17.     {
  18.         int i,len;
  19.         double digit,num;
  20.         
  21.         i=0;
  22.         len = strlen(s);
  23.         num = 0;
  24.         while (i<len-1)
  25.         {
  26.             
  27.             digit = s[i] - '0';
  28.             num = num * 10 + digit;
  29.             i++;
  30.             
  31.         }
  32.             return num;
  33.     }


gracias
  • Anonymous
  • Bot
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Octubre 26th, 2003, 9:09 pm

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • Avatar de Usuario
  • Registrado: Dic 20, 2002
  • Mensajes: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Nota Octubre 30th, 2003, 7:11 pm

¿Cuál es el error que está recibiendo? Y ¿estás seguro de que son capaces de manejar el. carácter correctamente?
Ozzu Hosting - Want your website on a fast server like Ozzu?

Publicar Información

  • Total de mensajes en este tema: 2 mensajes
  • Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 271 invitados
  • No puede abrir nuevos temas en este Foro
  • No puede responder a temas en este Foro
  • No puede editar sus mensajes en este Foro
  • No puede borrar sus mensajes en este Foro
  • No puede enviar adjuntos en este Foro
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC