Обновить main.cpp
This commit is contained in:
parent
175c53dee4
commit
4e525de67d
147
main.cpp
147
main.cpp
@ -2,140 +2,83 @@
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Структура для хранения пар
|
||||
struct Pair {
|
||||
int first;
|
||||
int second;
|
||||
typedef pair<string, string> Pair;
|
||||
|
||||
bool operator<(const Pair& other) const {
|
||||
return make_pair(first, second) < make_pair(other.first, other.second);
|
||||
}
|
||||
};
|
||||
set<Pair> relation;
|
||||
set<string> elements;
|
||||
|
||||
// Получить множество всех элементов из отношения
|
||||
set<int> getElements(const set<Pair>& relation) {
|
||||
set<int> elements;
|
||||
for (const auto& p : relation) {
|
||||
elements.insert(p.first);
|
||||
elements.insert(p.second);
|
||||
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<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;
|
||||
}
|
||||
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<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;
|
||||
}
|
||||
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<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; // нашли обратную пару у не-диагональной — значит не антисимметрично
|
||||
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<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;
|
||||
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<int>(first);
|
||||
pair.second = static_cast<int>(second);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main() {
|
||||
set<Pair> 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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user