Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <stdlib.h>
  6. #include <vector>
  7.  
  8. void writeFile(std::string path, std::string content, bool append = false) {
  9.     std::ofstream file;
  10.  
  11.     if (append) {
  12.         file.open(path.c_str(), std::ios::out | std::ios::app);
  13.     } else {
  14.         file.open(path.c_str(), std::ofstream::out | std::ofstream::trunc);
  15.         std::cout << path << std::endl;
  16.     }
  17.  
  18.     file << content;
  19.  
  20.     file.flush();
  21.     file.close();
  22. }
  23.  
  24. bool validatePesel(std::vector<int> pesel) {
  25.     if (pesel.size() == 11) {
  26.         int count = 0;
  27.  
  28.         int weights[10] = { 9, 7, 3, 1, 9, 7, 3, 1, 9, 7 };
  29.  
  30.         for (int i = 0; i < 10; i++) {
  31.             count += pesel[i] * weights[i];
  32.         }
  33.  
  34.         return count %10 == pesel[pesel.size() - 1];
  35.     }
  36.  
  37.     return false;
  38. }
  39.  
  40. bool isWomen(std::vector<int> pesel) {
  41.     return pesel[pesel.size() - 2] %2 == 0;
  42. }
  43.  
  44. void pesel(std::string pesel) {
  45.     std::vector<int> numbers;
  46.  
  47.     for (int i = 0; i < pesel.length(); i++) {
  48.         int number = pesel[i] - '0';
  49.         numbers.push_back(number);
  50.     }
  51.  
  52.     if (validatePesel(numbers)) {
  53.         if (isWomen(numbers)) {
  54.             writeFile("wynik2-kobiety.txt", pesel + "\n", true);
  55.         } else {
  56.             writeFile("wynik2-mezczyzni.txt", pesel + "\n", true);
  57.         }
  58.     } else {
  59.         writeFile("wynik2-pesel.txt", pesel + "\n", true);
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     std::ifstream in("dane2.txt");
  66.     std::string str;
  67.  
  68.     writeFile("wynik2-kobiety.txt", "");
  69.     writeFile("wynik2-mezczyzni.txt", "");
  70.     writeFile("wynik2-pesel.txt", "");
  71.  
  72.     while (in && std::getline(in, str)) {
  73.         if (str.length() == 0) continue;
  74.         pesel(str);
  75.     }
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement