TOPIC

5% de erro

Daniel_Alencar asked 4 years ago

Dá uma oLhada aí alguém por favor não tenho a mínima ideia de como resolver e dá somente 5% de erro...

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>

void func(char string[]);

int main()
{
    char string[51], *test, numeros[]={"0123456789"};
    int tam, i;
    unsigned long int numero;
    while(gets(string) != NULL)
    {
        setbuf(stdin, NULL);
        tam = strlen(string);
        if(!tam)
            printf("error\n");
        else
        {
            for(i=0;i<tam;i++)
            {
                if(string[i]=='o' || string[i]=='O')
                    string[i]='0';
                if(string[i]=='l')
                    string[i]='1';
            }
            func(string);
            for(i=0;i<tam;i++)
            {
                test = strchr(numeros, string[i]);
                if(test==NULL)
                    break;
            }
            if(test==NULL)
                printf("error\n");
            else
            {
                sscanf(string,"%u", &numero);
                if(numero <= 2147483647 && numero >= 0)
                    printf("%u\n", numero);
                else
                    printf("error\n");
            }
        }
    }
}

void func(char string[])
{
    int tam, i, j;
    char *teste1, *teste2;
    tam = strlen(string);

    for(i=0;i<tam;i++)
    {
        if(string[i]==',' || string[i]==' ')
        {
            for(j=i;j<tam;j++)
                string[j]=string[j+1];
        }
    }
    teste1 = strchr(string,',');
    teste2 = strchr(string,' ');
    if(teste1==NULL && teste2==NULL)
        return;
    else
        func(string);
}

Remember not post solutions. Your post may be reviewed by our moderators.

  • tmjunior replied 4 years ago

    Como o valor pode ter 50 dígitos %u não é suficiente. Long em c tem 32 bits, portanto não aumenta a quantidade de bits do inteiro que é por padrão de 32bits, ambos são definidos igualmente. Para utilizar inteiros até 64 bits utilize long long ou long long int.

    De qualquer forma 50 dígitos não cabem em 64 bits. Lhe resta restringir a quantidade de dígitos antes de converter.

    MOD