Advertisement
paulogp

Verificacao do BI PT

Aug 9th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. // apple xcode
  2. // paulogp
  3. /* BI check */
  4. #include <stdio.h>
  5.  
  6. #define MAX_VECT_LENGTH 50
  7.  
  8. int vector_size (char *the_vector);
  9. char check_bi (char *the_vector);
  10.  
  11. int vector_size (char *the_vector)
  12. {
  13.     int i;
  14.    
  15.     for (i = 0; the_vector[i] != '\0'; i++);
  16.    
  17.     return (i - 1);
  18. }
  19.  
  20. char check_bi (char the_vector[])
  21. {
  22.     unsigned long the_vector_size = 0;
  23.     unsigned long the_temp = -1; // avoid empty
  24.     unsigned long the_num = 0;
  25.     int i;
  26.    
  27.     // vector size
  28.     the_vector_size = vector_size(the_vector);
  29.    
  30.     // calculus; bi: 8 - 9 digits (including control digit)
  31.     // NOTE: if control digit is 0 is could represent 0 or 10!
  32.     // Exception (0 or 10) not implemented
  33.     if ((the_vector_size == 8) || (the_vector_size == 9))
  34.     {
  35.         for (i = 0; i < the_vector_size; i++)
  36.         {
  37.             // char2num
  38.             the_temp = the_vector[i] - '0';
  39.             // 9a + 8b + 7c + 6d + 5e + 4f + 3g + 2h + 1i
  40.             the_num = (the_temp * (the_vector_size - i)) + the_num;
  41.         }
  42.        
  43.         // final
  44.         the_temp = the_num % 11;
  45.         if (the_temp == 0)
  46.             return 1; // valido
  47.         else
  48.             return -1; // invalido
  49.     }
  50.     else
  51.     {
  52.         return 0;
  53.     }
  54. }
  55.  
  56. int main (int argc, const char * argv[])
  57. {
  58.     char the_vector[MAX_VECT_LENGTH];
  59.     int the_status = 0;
  60.    
  61.     // get number
  62.     printf("bi (incl digito de controlo): ");
  63.     fgets(the_vector, MAX_VECT_LENGTH, stdin);
  64.    
  65.     // calculus
  66.     the_status = check_bi(the_vector);
  67.    
  68.     switch(the_status)
  69.     {
  70.         case 1:
  71.             printf("numero de BI valido\n");
  72.             break;
  73.         case -1:
  74.             printf("numero de BI invalido\n");
  75.             break;
  76.         default:
  77.             printf("numero de digitos BI invalido para verificacao\n");
  78.     }
  79.    
  80.     return (0);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement