Материал: ИКПИ. Пример отчета по практике

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

Таблица 26. Файл lib/mobile_numbers_range.h lib/mobile_numbers_range.h

#ifndef MOBILE_NUMBERS_RANGE_H #define MOBILE_NUMBERS_RANGE_H

#include <list> #include <cmath> #include <string> #include <algorithm> #include <stdexcept>

#include "mobile_number_defines.h"

class MobileNumbersRange

{

public:

MobileNumbersRange();

MobileNumbersRange(std::string, std::string, std::string, std::wstring, std::wstring); MobileNumbersRange(int, int, int, std::wstring, std::wstring);

// getters

int operator_code_i() const; int caller_code_from_i() const; int caller_code_to_i() const;

std::string operator_code() const; std::string caller_code_from() const; std::string caller_code_to() const; std::wstring operator_name() const; std::wstring region() const;

// setters

void set_operator_code_i(int); void set_caller_code_i(int, int);

void set_operator_code(const std::string &); void set_operator_code(const std::wstring &);

void set_caller_code(const std::string &, const std::string &); void set_caller_code(const std::wstring &, const std::wstring &); void set_operator_name(const std::wstring &);

void set_region(const std::wstring &); std::list<std::string> split();

private:

int m_operator_code, m_caller_code_from, m_caller_code_to; std::wstring m_operator_name, m_region;

};

#endif // MOBILE_NUMBERS_RANGE_H

Таблица 27. Файл lib/mobile_numbers_range.cpp lib/mobile_numbers_range.cpp

#include "mobile_numbers_range.h"

MobileNumbersRange::MobileNumbersRange() {}

MobileNumbersRange::MobileNumbersRange(std::string operator_code, std::string caller_code_from, std::string caller_code_to, std::wstring mobile_operator, std::wstring region)

{

set_operator_code(operator_code); set_caller_code(caller_code_from, caller_code_to); set_operator_name(mobile_operator); set_region(region);

}

MobileNumbersRange::MobileNumbersRange(int operator_code, int caller_code_from, int caller_code_to,

std::wstring mobile_operator, std::wstring region)

{

set_operator_code_i(operator_code); set_caller_code_i(caller_code_from, caller_code_to);

31

lib/mobile_numbers_range.cpp

set_operator_name(mobile_operator); set_region(region);

}

// getters

int MobileNumbersRange::operator_code_i() const

{

return m_operator_code;

}

int MobileNumbersRange::caller_code_from_i() const

{

return m_caller_code_from;

}

int MobileNumbersRange::caller_code_to_i() const

{

return m_caller_code_to;

}

std::string MobileNumbersRange::operator_code() const

{

std::string s = std::to_string(m_operator_code);

if (s.size() < MobileNumberDefines::OPERATOR_CODE_DIGITS)

{

return std::string(MobileNumberDefines::OPERATOR_CODE_DIGITS - s.size(), L'0') + s;

}

return s;

}

std::string MobileNumbersRange::caller_code_from() const

{

std::string s = std::to_string(m_caller_code_from);

if (s.size() < MobileNumberDefines::CALLER_CODE_DIGITS)

{

return std::string(MobileNumberDefines::CALLER_CODE_DIGITS - s.size(), L'0') + s;

}

return s;

}

std::string MobileNumbersRange::caller_code_to() const

{

std::string s = std::to_string(m_caller_code_to);

if (s.size() < MobileNumberDefines::CALLER_CODE_DIGITS)

{

return std::string(MobileNumberDefines::CALLER_CODE_DIGITS - s.size(), L'0') + s;

}

return s;

}

std::wstring MobileNumbersRange::operator_name() const

{

return m_operator_name;

}

std::wstring MobileNumbersRange::region() const

{

return m_region;

}

// setters

void MobileNumbersRange::set_operator_code_i(int operator_code)

{

if (operator_code < 0 || operator_code > MobileNumberDefines::MAX_OPERATOR_CODE)

{

throw std::invalid_argument("set_operator_code_i -> operator_code = " + std::to_string(operator_code));

}

m_operator_code = operator_code;

}

void MobileNumbersRange::set_caller_code_i(int caller_code_from, int caller_code_to)

32

lib/mobile_numbers_range.cpp

{

if (caller_code_from < 0 || caller_code_from > MobileNumberDefines::MAX_CALLER_CODE)

{

throw std::invalid_argument("set_caller_code_i -> caller_code_from = " + std::to_string(caller_code_from));

}

if (caller_code_to < 0 || caller_code_to > MobileNumberDefines::MAX_CALLER_CODE)

{

throw std::invalid_argument("set_caller_code_i -> caller_code_to = " + std::to_string(caller_code_to));

}

m_caller_code_from = caller_code_from; m_caller_code_to = caller_code_to;

}

void MobileNumbersRange::set_operator_code(const std::wstring &operator_code)

{

set_operator_code(std::string(operator_code.begin(), operator_code.end()));

}

void MobileNumbersRange::set_operator_code(const std::string &operator_code)

{

if (operator_code.size() != MobileNumberDefines::OPERATOR_CODE_DIGITS)

{

throw std::invalid_argument("set_operator_code -> operator_code.size() != " + std::to_string(MobileNumberDefines::OPERATOR_CODE_DIGITS));

}

auto is_operator_code = std::all_of(operator_code.begin(), operator_code.end(), static_cast<int (*)(int)>(std::isdigit));

if (!is_operator_code)

{

throw std::invalid_argument("set_operator_code -> operator_code -- invalid argument: " + std::string(operator_code.begin(), operator_code.end()));

}

m_operator_code = std::stoi(operator_code);

}

void MobileNumbersRange::set_caller_code(const std::wstring &caller_code_from, const std::wstring &caller_code_to)

{

set_caller_code(std::string(caller_code_from.begin(), caller_code_from.end()), std::string(caller_code_to.begin(), caller_code_to.end()));

}

void MobileNumbersRange::set_caller_code(const std::string &caller_code_from, const std::string &caller_code_to)

{

if (caller_code_from.size() != MobileNumberDefines::CALLER_CODE_DIGITS)

{

throw std::invalid_argument("set_caller_code -> caller_code_from.size() != " + std::to_string(MobileNumberDefines::CALLER_CODE_DIGITS));

}

if (caller_code_to.size() != MobileNumberDefines::CALLER_CODE_DIGITS)

{

throw std::invalid_argument("set_caller_code -> caller_code_to.size() != " + std::to_string(MobileNumberDefines::CALLER_CODE_DIGITS));

}

auto is_caller_code_from = std::all_of(caller_code_from.begin(), caller_code_from.end(), static_cast<int (*)(int)>(std::isdigit));

if (!is_caller_code_from)

{

throw std::invalid_argument("set_caller_code -> caller_code_from -- invalid argument: " + std::string(caller_code_from.begin(), caller_code_from.end()));

}

auto is_caller_code_to = std::all_of(caller_code_to.begin(), caller_code_to.end(), static_cast<int (*)(int)>(std::isdigit));

if (!is_caller_code_to)

{

throw std::invalid_argument("set_caller_code -> caller_code_to -- invalid argument: " + std::string(caller_code_to.begin(), caller_code_to.end()));

}

m_caller_code_from = std::stoi(caller_code_from); m_caller_code_to = std::stoi(caller_code_to);

33

lib/mobile_numbers_range.cpp

}

void MobileNumbersRange::set_operator_name(const std::wstring &mobile_operator)

{

m_operator_name = mobile_operator;

}

void MobileNumbersRange::set_region(const std::wstring &region)

{

m_region = region;

}

std::list<std::string> MobileNumbersRange::split()

{

auto get_number_of_digits = [](int x) -> int

{

return static_cast<int>(std::log10(x) + 1);

};

std::list<std::string> range_splitted; int cct = m_caller_code_to;

while (cct >= m_caller_code_from)

{

int digits_init = get_number_of_digits(cct);

if (cct % 10 == 9 && cct - m_caller_code_from >= 9)

{

int digits = get_number_of_digits(cct - m_caller_code_from + 10) - 1; int pw = static_cast<int>(std::pow(10, digits));

for (int i = 0; i <= digits; ++i, pw /= 10)

{

if (cct % pw == pw - 1)

{

int t = cct / pw; std::string t1; if (t != 0)

{

t1 = std::to_string(t);

}

std::string t2 = std::string(static_cast<size_t>(digits_init) - t1.size(), '?'); range_splitted.push_back(t1 + t2);

break;

}

}

cct -= pw;

}

else

{

if (range_splitted.empty() || range_splitted.back() != "?" || cct != 0)

{

range_splitted.push_back(std::to_string(cct)); --cct;

}

else

{

break;

}

}

}

for (auto &x : range_splitted)

{

if (x.size() < MobileNumberDefines::CALLER_CODE_DIGITS)

{

x = std::string(MobileNumberDefines::CALLER_CODE_DIGITS - x.size(), '0') + x;

}

}

return range_splitted;

}

34

Таблица 28. Файл lib/mobile_numbers_data.h lib/mobile_numbers_data.h

#ifndef MOBILE_NUMBERS_DATA_H #define MOBILE_NUMBERS_DATA_H

#include <istream> #include <string> #include <regex> #include <algorithm> #include <stdexcept>

#include "mobile_numbers_range.h" #include "digits_tree.h"

#include "mobile_number_defines.h"

class MobileNumbersData

{

public:

MobileNumbersData();

void add_data(std::wistream &);

MobileNumbersRange find_with_check(std::string) const; MobileNumbersRange find_without_check(std::string) const; std::string generate() const;

static bool is_mobile_number(const std::string &);

private:

DigitsTree<> m_data;

};

#endif // MOBILE_NUMBERS_DATA_H

Таблица 29. Файл lib/mobile_numbers_data.cpp lib/mobile_numbers_data.cpp

#include "mobile_numbers_data.h"

MobileNumbersData::MobileNumbersData()

{

}

void MobileNumbersData::add_data(std::wistream &stream)

{

bool maybe_header = true; MobileNumbersRange range;

std::match_results<std::wstring::const_iterator> sm; for (std::wstring line; std::getline(stream, line);)

{

if (std::regex_match(line, sm, MobileNumberDefines::mobile_numbers_range_regex))

{

range.set_operator_code(sm.str(1)); range.set_caller_code(sm.str(2), sm.str(3)); range.set_operator_name(sm.str(4)); range.set_region(sm.str(5));

std::string operator_code = range.operator_code(); auto range_splitted = range.split();

for (const auto &x : range_splitted)

{

m_data.push(operator_code + x, range);

}

}

else

{

if (maybe_header && !(L'0' <= line[0] && line[0] <= L'9'))

{

maybe_header = false;

}

else

{

throw std::invalid_argument( std::string("Argument is invalid: ") + std::string(line.begin(), line.end()));

}

}

35