Some test that this code does not pass

Asked

Viewed 66 times

-2

I’m asking the question Voice Dial, I’ve done every test possible and can’t find the error could help me in other test that my code fails

My code

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

int indice(char *numero);
void printa(char n);

int main(int argc, char** argv)
{
  char numero[900];
  int tam, resultado, i;
  while(gets(numero) != NULL)
  {
     tam = strlen(numero);
     if(indice(numero))
     {
        resultado = indice(numero);
        resultado--;
        for(i = 0; i < resultado; i++)
        {
            printf("%c", numero[i]);
        }
        for(i = resultado; i < tam ; i++)
        {
            printa(toupper(numero[i]));
        }
        printf("\n");
     }
     else
     {
        for(i = 0; i < tam; i++)
        {
            if((numero[i] >= '0' && numero[i] <= '9') || numero[i] == '*' || numero[i] == '#')
            {
                printf("%c", numero[i]);
            }
        }
        printf("\n");
     }

}
return 0;
}

int indice(char *numero)
{
 int tam = strlen(numero), i, j, resultado = 0;
 char alfabeto[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"};
 int tam2 = strlen(alfabeto);
 for(i = 0; i < tam; i++)
 {
    for(j = 0; j < tam2 ; j++)
    {
        if(numero[i] == alfabeto[j])
        {
            resultado = i;
            break;
        }
    }
    if(resultado > 0)
    {
        break;
    }
 }
   return resultado;
 }
void printa(char n)
{
  if(n == 'A' || n == 'B' || n == 'C')
  {
    printf("2");
  }
  else if(n == 'D' || n == 'E' || n == 'F')
  {
    printf("3");
  }
  else if(n == 'G' || n == 'H' || n == 'I')
  {
    printf("4");
  }
  else if(n == 'J' || n == 'K' || n == 'L')
  {
    printf("5");
  }
  else if(n == 'M' || n == 'N' || n == 'O')
  {
    printf("6");
  }
  else if(n == 'P' || n == 'Q' || n == 'R' || n == 'S')
  {
    printf("7");
   }
  else if(n == 'T' || n == 'U' || n == 'V')
  {
    printf("8");
  }
  else if(n == 'W' || n == 'X' || n == 'Y' || n == 'Z')
  {
    printf("9");
  }
 }
  • Friend, if you don’t give a damn what that code is supposed to do or what parts are not working well, how do you expect us to tell you what is wrong ? Remember that the clearer and more detailed you are, the more likely you are to have an answer that suits you

  • To the entrance T800-RAFO0, the program goes wrong.

1 answer

1


Why not use an ASCII-phone mapping table?

int i;
char lookup[256];
char linha[300];

// Inicializa lookup.
for (i = 0; i < 256; ++i)
{
    lookup[i] = 0;
}

// Inicializa os valores possíveis de consulta.
lookup['0'] = '0'; lookup['1'] = '1'; lookup['2'] = '2';
lookup['3'] = '3'; lookup['4'] = '4'; lookup['5'] = '5';
lookup['6'] = '6'; lookup['7'] = '7'; lookup['8'] = '8';
lookup['9'] = '9'; lookup['*'] = '*'; lookup['#'] = '#';

lookup['a'] = lookup['b'] = lookup['c'] =
    lookup['A'] = lookup['B'] = lookup['C'] = '2';

lookup['d'] = lookup['e'] = lookup['f'] =
    lookup['D'] = lookup['E'] = lookup['F'] = '3';

lookup['g'] = lookup['h'] = lookup['i'] =
    lookup['G'] = lookup['H'] = lookup['I'] = '4';

lookup['j'] = lookup['k'] = lookup['l'] =
    lookup['J'] = lookup['K'] = lookup['L'] = '5';

lookup['m'] = lookup['n'] = lookup['o'] =
    lookup['M'] = lookup['N'] = lookup['O'] = '6';

lookup['p'] = lookup['q'] = lookup['r'] = lookup['s'] =
    lookup['P'] = lookup['Q'] = lookup['R'] = lookup['S'] = '7';

lookup['t'] = lookup['u'] = lookup['v'] =
    lookup['T'] = lookup['U'] = lookup['V'] = '8';

lookup['w'] = lookup['x'] = lookup['y'] = lookup['z'] =
    lookup['W'] = lookup['X'] = lookup['Y'] = lookup['Z'] = '9';

// Captura entrada e imprime saída enquanto não for EOF em stdin.
while (gets(linha) != NULL)
{
    char* p = linha;
    while(*p)
    {
        char saida = lookup[*p++];
        if (saida)
        {
            printf("%c", saida);
        }
    }
    printf("\n");
}
  • Our really hadn’t noticed this, but you have some book that can recommend me on C language

  • I have an old book that I used to read all the time, by author Herbert Schildt: C Full and Total, from Osborne Mcgraw-Hill. If you want, I’ll mail it to you.

  • How much would I have to pay ?

  • I played all the complexity of the program in the data structure; see that the while has less than 10 lines.

  • You don’t have to pay anything!

  • From some outside, when we start to comment too much on an answer, or on the question, appears the suggestion to go to a chat room in the OS; I just don’t know if we can go straight there, without waiting for this suggestion.

  • 1

    Hello Marty with your input I made the code my way and passed

  • See if it goes with the cases that have passed before. This is called regression testing.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.