Загрузка данных
#include <iostream>
#include <fstream>
#include <string>
#include <clocale>
using namespace std;
const int MAX = 20;
int A[MAX][MAX];
int n = 0;
// Ввод матрицы смежности с клавиатуры
void vvodSKlaviatury()
{
cout << "Введите количество вершин: ";
cin >> n;
cout << "Введите матрицу смежности:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> A[i][j];
}
}
cout << "Граф введен." << endl;
}
// Ввод матрицы смежности из файла
void vvodIzFaila()
{
string imyaFaila;
cout << "Введите имя файла: ";
cin >> imyaFaila;
ifstream file(imyaFaila);
if (!file)
{
cout << "Файл не найден!" << endl;
return;
}
file >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
file >> A[i][j];
}
}
file.close();
cout << "Граф загружен из файла." << endl;
}
// Вывод матрицы смежности
void matricaSmezhnosti()
{
cout << endl;
cout << "Матрица смежности:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}
// Вывод списка смежности
void spisokSmezhnosti()
{
cout << endl;
cout << "Список смежности:" << endl;
for (int i = 0; i < n; i++)
{
cout << "Вершина " << i + 1 << ": ";
for (int j = 0; j < n; j++)
{
if (A[i][j] == 1)
{
cout << j + 1 << " ";
}
}
cout << endl;
}
}
// Вывод матрицы инцидентности
void matricaIncidentnosti()
{
int nachaloRebra[MAX * MAX];
int konecRebra[MAX * MAX];
int kolichestvoReber = 0;
// Находим все ребра графа
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (A[i][j] == 1)
{
nachaloRebra[kolichestvoReber] = i;
konecRebra[kolichestvoReber] = j;
kolichestvoReber++;
}
}
}
cout << endl;
cout << "Ребра графа:" << endl;
for (int i = 0; i < kolichestvoReber; i++)
{
cout << "e" << i + 1
<< " = ("
<< nachaloRebra[i] + 1
<< ", "
<< konecRebra[i] + 1
<< ")" << endl;
}
cout << endl;
cout << "Матрица инцидентности:" << endl;
cout << " ";
for (int j = 0; j < kolichestvoReber; j++)
{
cout << "e" << j + 1 << " ";
}
cout << endl;
for (int i = 0; i < n; i++)
{
cout << i + 1 << ": ";
for (int j = 0; j < kolichestvoReber; j++)
{
if (i == nachaloRebra[j] || i == konecRebra[j])
{
cout << "1 ";
}
else
{
cout << "0 ";
}
}
cout << endl;
}
}
// Вывод списка инцидентности
void spisokIncidentnosti()
{
int nachaloRebra[MAX * MAX];
int konecRebra[MAX * MAX];
int kolichestvoReber = 0;
// Находим ребра графа
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (A[i][j] == 1)
{
nachaloRebra[kolichestvoReber] = i;
konecRebra[kolichestvoReber] = j;
kolichestvoReber++;
}
}
}
cout << endl;
cout << "Список инцидентности:" << endl;
for (int i = 0; i < n; i++)
{
cout << "Вершина " << i + 1 << ": ";
for (int j = 0; j < kolichestvoReber; j++)
{
if (i == nachaloRebra[j] || i == konecRebra[j])
{
cout << "e" << j + 1 << " ";
}
}
cout << endl;
}
}
int main()
{
setlocale(LC_ALL, "Russian");
int menu;
do
{
cout << endl;
cout << "========== МЕНЮ ==========" << endl;
cout << "1 - Ввести граф с клавиатуры" << endl;
cout << "2 - Загрузить граф из файла" << endl;
cout << "3 - Вывести матрицу смежности" << endl;
cout << "4 - Вывести список смежности" << endl;
cout << "5 - Вывести матрицу инцидентности" << endl;
cout << "6 - Вывести список инцидентности" << endl;
cout << "0 - Выход" << endl;
cout << "==========================" << endl;
cout << "Выберите пункт: ";
cin >> menu;
if (menu == 1)
{
vvodSKlaviatury();
}
if (menu == 2)
{
vvodIzFaila();
}
if (menu == 3)
{
if (n == 0)
cout << "Сначала необходимо ввести граф!" << endl;
else
matricaSmezhnosti();
}
if (menu == 4)
{
if (n == 0)
cout << "Сначала необходимо ввести граф!" << endl;
else
spisokSmezhnosti();
}
if (menu == 5)
{
if (n == 0)
cout << "Сначала необходимо ввести граф!" << endl;
else
matricaIncidentnosti();
}
if (menu == 6)
{
if (n == 0)
cout << "Сначала необходимо ввести граф!" << endl;
else
spisokIncidentnosti();
}
if (menu < 0 || menu > 6)
{
cout << "Такого пункта меню нет!" << endl;
}
} while (menu != 0);
cout << "Программа завершена." << endl;
return 0;
}