Обновить main.cpp

This commit is contained in:
anqude 2025-06-10 07:03:50 +00:00
parent 175c53dee4
commit 4e525de67d

149
main.cpp
View File

@ -2,140 +2,83 @@
#include <set> #include <set>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector>
#include <unordered_map>
#include <cctype>
using namespace std; using namespace std;
// Структура для хранения пар typedef pair<string, string> Pair;
struct Pair {
int first;
int second;
bool operator<(const Pair& other) const {
return make_pair(first, second) < make_pair(other.first, other.second);
}
};
// Получить множество всех элементов из отношения set<Pair> relation;
set<int> getElements(const set<Pair>& relation) { set<string> elements;
set<int> elements;
for (const auto& p : relation) { void inputRelation() {
elements.insert(p.first); string line;
elements.insert(p.second); 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() {
bool isReflexive(const set<Pair>& relation) { for (const string& x : elements) {
set<int> elements = getElements(relation); if (relation.find({x, x}) == relation.end()) {
return false;
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; return true;
} }
// Проверка симметричности bool isSymmetric() {
bool isSymmetric(const set<Pair>& relation) { for (const Pair& p : relation) {
for (const auto& p : relation) { if (relation.find({p.second, p.first}) == relation.end()) { // проходимся по всем парам и ищем хотя бы однк не по условию
bool foundReverse = false; return false;
for (const auto& q : relation) {
if (q.first == p.second && q.second == p.first) {
foundReverse = true;
break;
}
} }
if (!foundReverse) return false;
} }
return true; return true;
} }
// Проверка кососимметричности (антисимметричности) bool isAntisymmetric() {
bool isAntisymmetric(const set<Pair>& relation) { for (const Pair& p : relation) {
for (const auto& p : relation) { if (p.first != p.second) { // a!=b и есть пара
if (p.first == p.second) continue; // пропускаем диагональные пары if (relation.find({p.second, p.first}) != relation.end()) {
for (const auto& q : relation) { return false;
if (q.first == p.second && q.second == p.first) {
return false; // нашли обратную пару у не-диагональной — значит не антисимметрично
} }
} }
} }
return true; return true;
} }
// Проверка транзитивности bool isTransitive() {
bool isTransitive(const set<Pair>& relation) { for (const Pair& p1 : relation) {
for (const auto& a : relation) { for (const Pair& p2 : relation) {
for (const auto& b : relation) { if (p1.second == p2.first) {
if (a.second == b.first) { Pair trans = {p1.first, p2.second}; // цикл вложенных пар
Pair transPair = {a.first, b.second}; if (relation.find(trans) == relation.end()) {
bool found = relation.count(transPair); return false;
if (!found) return false; }
} }
} }
} }
return true; 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() { int main() {
set<Pair> relation; cout << "Пары элементов отношения:\n";
string line; inputRelation();
cout << "Введите пары элементов в формате (a, b). Для завершения введите 'end':\n"; cout << "Рефлексивность: " << (isReflexive() ? "Да" : "Нет") << endl;
cout << "Симметричность: " << (isSymmetric() ? "Да" : "Нет") << endl;
while (getline(cin, line)) { cout << "Кососимметричность: " << (isAntisymmetric() ? "Да" : "Нет") << endl;
if (line == "end") break; cout << "Транзитивность: " << (isTransitive() ? "Да" : "Нет") << endl;
cout << "Эквивалентность: " << ((isTransitive() and isReflexive() and isSymmetric()) ? "Да" : "Нет") << endl;
Pair p; cout << "Частичный порядок: " << ((isTransitive() and isReflexive() and isAntisymmetric()) ? "Да" : "Нет") << endl;
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; return 0;
} }