Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // apple xcode
- // paulogp
- /* BI check */
- #include <stdio.h>
- #define MAX_VECT_LENGTH 50
- int vector_size (char *the_vector);
- char check_bi (char *the_vector);
- int vector_size (char *the_vector)
- {
- int i;
- for (i = 0; the_vector[i] != '\0'; i++);
- return (i - 1);
- }
- char check_bi (char the_vector[])
- {
- unsigned long the_vector_size = 0;
- unsigned long the_temp = -1; // avoid empty
- unsigned long the_num = 0;
- int i;
- // vector size
- the_vector_size = vector_size(the_vector);
- // calculus; bi: 8 - 9 digits (including control digit)
- // NOTE: if control digit is 0 is could represent 0 or 10!
- // Exception (0 or 10) not implemented
- if ((the_vector_size == 8) || (the_vector_size == 9))
- {
- for (i = 0; i < the_vector_size; i++)
- {
- // char2num
- the_temp = the_vector[i] - '0';
- // 9a + 8b + 7c + 6d + 5e + 4f + 3g + 2h + 1i
- the_num = (the_temp * (the_vector_size - i)) + the_num;
- }
- // final
- the_temp = the_num % 11;
- if (the_temp == 0)
- return 1; // valido
- else
- return -1; // invalido
- }
- else
- {
- return 0;
- }
- }
- int main (int argc, const char * argv[])
- {
- char the_vector[MAX_VECT_LENGTH];
- int the_status = 0;
- // get number
- printf("bi (incl digito de controlo): ");
- fgets(the_vector, MAX_VECT_LENGTH, stdin);
- // calculus
- the_status = check_bi(the_vector);
- switch(the_status)
- {
- case 1:
- printf("numero de BI valido\n");
- break;
- case -1:
- printf("numero de BI invalido\n");
- break;
- default:
- printf("numero de digitos BI invalido para verificacao\n");
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement