Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int parseChar(char c)
  5. {
  6.   int res;
  7.   switch (c)
  8.   {
  9.     case '1':
  10.       res = 1;
  11.       break;
  12.     case '2':
  13.       res = 2;
  14.       break;
  15.     case '3':
  16.       res = 3;
  17.       break;
  18.     case '4':
  19.       res = 4;
  20.       break;
  21.     case '5':
  22.       res = 5;
  23.       break;
  24.     case '6':
  25.       res = 6;
  26.       break;
  27.     case '7':
  28.       res = 7;
  29.       break;
  30.     case '8':
  31.       res = 8;
  32.       break;
  33.     case '9':
  34.       res = 9;
  35.       break;
  36.     case '0':
  37.       res = 0;
  38.       break;
  39.     default:
  40.       res = -1;
  41.       break;
  42.   }
  43.   return res;
  44. }
  45.  
  46. bool validatePESEL(char* pesel)
  47. {
  48.   int weights[] = {1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1};
  49.   int digit;
  50.   int sum = 0;
  51.   for(int i = 0; i < 11; ++i)
  52.   {
  53.     digit = parseChar(pesel[i]);
  54.     if(digit == -1)//niecyfra
  55.       return false;
  56.     sum += weights[i] * digit;
  57.   }
  58.   if(sum != 0 && sum % 10 == 0) return true;
  59.   else return false;
  60. }
  61.  
  62. int main()
  63. {
  64.   int num;
  65.   cin >> num;
  66.   cin.get();
  67.   for(int i = 0; i < num; ++i)
  68.   {
  69.     char currentPESEL[12];
  70.     cin.getline(currentPESEL, 12);
  71.     if(validatePESEL(currentPESEL)) cout << "D\n";
  72.     else cout << "N\n";
  73.   }
  74.   cin.get();
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement