diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..650c62d --- /dev/null +++ b/main.cpp @@ -0,0 +1,141 @@ +#include +#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); + } +}; + +// Получить множество всех элементов из отношения +set getElements(const set& relation) { + set elements; + for (const auto& p : relation) { + elements.insert(p.first); + elements.insert(p.second); + } + 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; + } + } + 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; + } + } + 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; // нашли обратную пару у не-диагональной — значит не антисимметрично + } + } + } + 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; + } + } + } + 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 << "Введите пары элементов в формате (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; + + return 0; +} \ No newline at end of file