84 lines
2.4 KiB
C++
84 lines
2.4 KiB
C++
#include <iostream>
|
|
#include <set>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
typedef pair<string, string> Pair;
|
|
|
|
set<Pair> relation;
|
|
set<string> 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);
|
|
}
|
|
}
|
|
|
|
bool isReflexive() {
|
|
for (const string& x : elements) {
|
|
if (relation.find({x, x}) == relation.end()) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool isSymmetric() {
|
|
for (const Pair& p : relation) {
|
|
if (relation.find({p.second, p.first}) == relation.end()) { // проходимся по всем парам и ищем хотя бы однк не по условию
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
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() {
|
|
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;
|
|
}
|
|
|
|
int main() {
|
|
cout << "Пары элементов отношения:\n";
|
|
inputRelation();
|
|
|
|
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;
|
|
} |