Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <vector>
- void writeFile(std::string path, std::string content, bool append = false) {
- std::ofstream file;
- if (append) {
- file.open(path.c_str(), std::ios::out | std::ios::app);
- } else {
- file.open(path.c_str(), std::ofstream::out | std::ofstream::trunc);
- std::cout << path << std::endl;
- }
- file << content;
- file.flush();
- file.close();
- }
- bool validatePesel(std::vector<int> pesel) {
- if (pesel.size() == 11) {
- int count = 0;
- int weights[10] = { 9, 7, 3, 1, 9, 7, 3, 1, 9, 7 };
- for (int i = 0; i < 10; i++) {
- count += pesel[i] * weights[i];
- }
- return count %10 == pesel[pesel.size() - 1];
- }
- return false;
- }
- bool isWomen(std::vector<int> pesel) {
- return pesel[pesel.size() - 2] %2 == 0;
- }
- void pesel(std::string pesel) {
- std::vector<int> numbers;
- for (int i = 0; i < pesel.length(); i++) {
- int number = pesel[i] - '0';
- numbers.push_back(number);
- }
- if (validatePesel(numbers)) {
- if (isWomen(numbers)) {
- writeFile("wynik2-kobiety.txt", pesel + "\n", true);
- } else {
- writeFile("wynik2-mezczyzni.txt", pesel + "\n", true);
- }
- } else {
- writeFile("wynik2-pesel.txt", pesel + "\n", true);
- }
- }
- int main()
- {
- std::ifstream in("dane2.txt");
- std::string str;
- writeFile("wynik2-kobiety.txt", "");
- writeFile("wynik2-mezczyzni.txt", "");
- writeFile("wynik2-pesel.txt", "");
- while (in && std::getline(in, str)) {
- if (str.length() == 0) continue;
- pesel(str);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement