/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab7/task_2.cpp
189 строк
6 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include "task_2.h" #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cctype> #include <sstream> #include <stdexcept> struct Address { std::string Country; std::string City; std::string Street; std::string House; }; // �ᯮ����⥫�� �㭪樨 std::string trim(const std::string& str) { size_t start = str.find_first_not_of(" \t\n\r"); if (start == std::string::npos) return ""; size_t end = str.find_last_not_of(" \t\n\r"); return str.substr(start, end - start + 1); } std::string to_lower(const std::string& str) { std::string result = str; std::transform(result.begin(), result.end(), result.begin(), ::tolower); return result; } std::string capitalize(const std::string& str) { if (str.empty()) return str; std::string result = str; result[0] = std::toupper(result[0]); for (size_t i = 1; i < result.length(); ++i) { result[i] = std::tolower(result[i]); } return result; } // �㭪�� Parse - ࠧ��ࠥ� ��ப� �� ���������� ���� void Parse(const std::string& line, Address* const address) { if (address == nullptr) { throw std::invalid_argument("Address pointer is null"); } std::string trimmed = trim(line); if (trimmed.empty()) { throw std::invalid_argument("Empty address line"); } // ���⮩ ���ᨭ� �� ������ (����� ������ �� ����室�����) std::vector<std::string> parts; std::stringstream ss(trimmed); std::string part; while (std::getline(ss, part, ',')) { parts.push_back(trim(part)); } if (parts.size() < 4) { throw std::invalid_argument("Invalid address format: expected Country, City, Street, House"); } address->Country = parts[0]; address->City = parts[1]; address->Street = parts[2]; address->House = parts[3]; // ��ઠ �� ����� ���� if (address->Country.empty() || address->City.empty() || address->Street.empty() || address->House.empty()) { throw std::invalid_argument("All address fields must be non-empty"); } } // �㭪�� Unify - �ਢ���� ���� � ��������� ���� void Unify(Address* const address) { if (address == nullptr) { throw std::invalid_argument("Address pointer is null"); } // �ਢ���� � ��������� ���� ��࠭� address->Country = capitalize(trim(address->Country)); // �ਢ���� � ��������� ���� ��த address->City = capitalize(trim(address->City)); // �ਢ���� � ��������� ���� 㫨�� std::string street = trim(address->Street); std::string lower_street = to_lower(street); // ������ ᮪�饭�� �� ����� �������� if (lower_street.find("��-�") != std::string::npos || lower_street.find("��.") != std::string::npos || lower_street.find("�� ") != std::string::npos) { size_t pos = lower_street.find("��-�"); if (pos != std::string::npos) { street = street.substr(0, pos) + "���" + street.substr(pos + 4); } pos = lower_street.find("��."); if (pos != std::string::npos) { street = street.substr(0, pos) + "��ᯥ��" + street.substr(pos + 3); } pos = lower_street.find("�� "); if (pos != std::string::npos) { street = street.substr(0, pos) + "��ᯥ��" + street.substr(pos + 3); } } // ������ "�." �� "㫨�" if (lower_street.find("�.") != std::string::npos || lower_street.find("� ") != std::string::npos) { size_t pos = lower_street.find("�."); if (pos != std::string::npos) { street = street.substr(0, pos) + "㫨�" + street.substr(pos + 3); } pos = lower_street.find("� "); if (pos != std::string::npos) { street = street.substr(0, pos) + "㫨�" + street.substr(pos + 3); } } // ������ "���." �� "���㫮�" if (lower_street.find("���.") != std::string::npos || lower_street.find("��� ") != std::string::npos) { size_t pos = lower_street.find("���."); if (pos != std::string::npos) { street = street.substr(0, pos) + "���㫮�" + street.substr(pos + 4); } pos = lower_street.find("��� "); if (pos != std::string::npos) { street = street.substr(0, pos) + "���㫮�" + street.substr(pos + 4); } } // ����⠫����� �������� 㫨�� address->Street = capitalize(trim(street)); // �ਢ���� � ��������� ���� ����� ���� std::string house = trim(address->House); std::string lower_house = to_lower(house); // ������ ᮪�饭�� � ����� ���� if (lower_house.find("�.") != std::string::npos) { size_t pos = lower_house.find("�."); house = house.substr(0, pos) + "���" + house.substr(pos + 2); } address->House = capitalize(trim(house)); } // �㭪�� Format - �����頥� ⥪�⮢�� �।�⠢����� ���� std::string Format(const Address& address) { return address.Country + ", " + address.City + ", " + address.Street + ", " + address.House; } // �᭮���� �ணࠬ�� int task_2() { std::string line; std::cout << "������ ���� � �ଠ�: ��࠭�, ��த, ����, ���" << std::endl; std::cout << "�ਬ��: �����, ��᪢�, �. ����᪠�, �. 10" << std::endl; std::cout << "��� ��室� ������ ������ ��ப�" << std::endl; while (getline(std::cin, line)) { if (line.empty()) { break; } Address* address = new Address(); // �뤥�塞 ������ try { Parse(line, address); std::cout << "�� ���ᨭ��: " << Format(*address) << std::endl; Unify(address); std::cout << "�� 㭨䨪�樨: " << Format(*address) << "\n" << std::endl; } catch (const std::exception& e) { std::cout << "�訡��: " << e.what() << "\n" << std::endl; } delete address; // �������� ������ } return 0; }