Добавить main.cpp
This commit is contained in:
parent
b6525ee6db
commit
175c53dee4
141
main.cpp
Normal file
141
main.cpp
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <set>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
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<int> getElements(const set<Pair>& relation) {
|
||||||
|
set<int> elements;
|
||||||
|
for (const auto& p : relation) {
|
||||||
|
elements.insert(p.first);
|
||||||
|
elements.insert(p.second);
|
||||||
|
}
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Проверка рефлексивности
|
||||||
|
bool isReflexive(const set<Pair>& relation) {
|
||||||
|
set<int> 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<Pair>& 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<Pair>& 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<Pair>& 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<int>(first);
|
||||||
|
pair.second = static_cast<int>(second);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
set<Pair> 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;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user