From 4e525de67d53d23e7c01ddbd74bbbc1a5fce46e0 Mon Sep 17 00:00:00 2001 From: anqude Date: Tue, 10 Jun 2025 07:03:50 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20main.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 149 +++++++++++++++++-------------------------------------- 1 file changed, 46 insertions(+), 103 deletions(-) diff --git a/main.cpp b/main.cpp index 650c62d..9fe4686 100644 --- a/main.cpp +++ b/main.cpp @@ -2,140 +2,83 @@ #include #include #include -#include -#include -#include using namespace std; -// Структура для хранения пар -struct Pair { - int first; - int second; - - bool operator<(const Pair& other) const { - return make_pair(first, second) < make_pair(other.first, other.second); - } -}; +typedef pair Pair; -// Получить множество всех элементов из отношения -set getElements(const set& relation) { - set elements; - for (const auto& p : relation) { - elements.insert(p.first); - elements.insert(p.second); +set relation; +set elements; + +void inputRelation() { + string line; + while (getline(cin, line)) { + if (line == "end") break; + istringstream iss(line); + string a, b; + if (!(iss >> a >> b)) { + cout << "Ошибка ввода: " << line << endl; + continue; + } + relation.insert({a, b}); + elements.insert(a); + elements.insert(b); } - return elements; } -// Проверка рефлексивности -bool isReflexive(const set& relation) { - set elements = getElements(relation); - - for (int x : elements) { - bool hasSelfPair = false; - for (const auto& p : relation) { - if (p.first == x && p.second == x) { - hasSelfPair = true; - break; - } +bool isReflexive() { + for (const string& x : elements) { + if (relation.find({x, x}) == relation.end()) { + return false; } - if (!hasSelfPair) return false; } return true; } -// Проверка симметричности -bool isSymmetric(const set& relation) { - for (const auto& p : relation) { - bool foundReverse = false; - for (const auto& q : relation) { - if (q.first == p.second && q.second == p.first) { - foundReverse = true; - break; - } +bool isSymmetric() { + for (const Pair& p : relation) { + if (relation.find({p.second, p.first}) == relation.end()) { // проходимся по всем парам и ищем хотя бы однк не по условию + return false; } - if (!foundReverse) return false; } return true; } -// Проверка кососимметричности (антисимметричности) -bool isAntisymmetric(const set& relation) { - for (const auto& p : relation) { - if (p.first == p.second) continue; // пропускаем диагональные пары - for (const auto& q : relation) { - if (q.first == p.second && q.second == p.first) { - return false; // нашли обратную пару у не-диагональной — значит не антисимметрично +bool isAntisymmetric() { + for (const Pair& p : relation) { + if (p.first != p.second) { // a!=b и есть пара + if (relation.find({p.second, p.first}) != relation.end()) { + return false; } } } return true; } -// Проверка транзитивности -bool isTransitive(const set& relation) { - for (const auto& a : relation) { - for (const auto& b : relation) { - if (a.second == b.first) { - Pair transPair = {a.first, b.second}; - bool found = relation.count(transPair); - if (!found) return false; +bool isTransitive() { + for (const Pair& p1 : relation) { + for (const Pair& p2 : relation) { + if (p1.second == p2.first) { + Pair trans = {p1.first, p2.second}; // цикл вложенных пар + if (relation.find(trans) == relation.end()) { + return false; + } } } } return true; } -// Парсинг строки вида "(x, y)" -bool parsePair(const string& line, Pair& pair) { - if (line.size() < 5 || line[0] != '(' || line.back() != ')') return false; - - size_t commaPos = line.find(','); - if (commaPos == string::npos || commaPos == 1 || commaPos == line.size() - 2) return false; - - string firstStr = line.substr(1, commaPos - 1); - string secondStr = line.substr(commaPos + 1, line.size() - commaPos - 2); - - // Убираем пробелы - firstStr.erase(remove_if(firstStr.begin(), firstStr.end(), ::isspace), firstStr.end()); - secondStr.erase(remove_if(secondStr.begin(), secondStr.end(), ::isspace), secondStr.end()); - - char* end; - long first = strtol(firstStr.c_str(), &end, 10); - if (*end != '\0') return false; - - long second = strtol(secondStr.c_str(), &end, 10); - if (*end != '\0') return false; - - pair.first = static_cast(first); - pair.second = static_cast(second); - return true; -} - int main() { - set relation; - string line; + cout << "Пары элементов отношения:\n"; + inputRelation(); - cout << "Введите пары элементов в формате (a, b). Для завершения введите 'end':\n"; - - while (getline(cin, line)) { - if (line == "end") break; - - Pair p; - if (parsePair(line, p)) { - relation.insert(p); - } else { - cout << "Ошибка ввода: неверный формат пары '" << line << "'. Используйте формат '(a, b)'.\n"; - return 1; - } - } - - cout << "\nРезультат анализа:\n"; - cout << "Рефлексивность: " << (isReflexive(relation) ? "Да" : "Нет") << endl; - cout << "Симметричность: " << (isSymmetric(relation) ? "Да" : "Нет") << endl; - cout << "Кососимметричность: " << (isAntisymmetric(relation) ? "Да" : "Нет") << endl; - cout << "Транзитивность: " << (isTransitive(relation) ? "Да" : "Нет") << endl; + cout << "Рефлексивность: " << (isReflexive() ? "Да" : "Нет") << endl; + cout << "Симметричность: " << (isSymmetric() ? "Да" : "Нет") << endl; + cout << "Кососимметричность: " << (isAntisymmetric() ? "Да" : "Нет") << endl; + cout << "Транзитивность: " << (isTransitive() ? "Да" : "Нет") << endl; + cout << "Эквивалентность: " << ((isTransitive() and isReflexive() and isSymmetric()) ? "Да" : "Нет") << endl; + cout << "Частичный порядок: " << ((isTransitive() and isReflexive() and isAntisymmetric()) ? "Да" : "Нет") << endl; return 0; } \ No newline at end of file