Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Основы объектно-ориентированного программирования задач на графах. Учебное пособие

.pdf
Скачиваний:
0
Добавлен:
07.09.2026
Размер:
1 Мб
Скачать
☆
Приложение 4
111
for(int j=0; j<N; j++) { cout << " Matr["<< i <<"]["<< j <<"]="; cin >> Matr[i][j]; } cout <<endl; }
//------ ф у н к ц и я - операция вывода графа -------
void Graph::operator-() { for(int i=0; i<N; i++) for(int j=0; j<N; j++) { cout << " Matr["<< i <<"]["<< j <<"]="; cout << Matr[i][j] <<endl; } }
//-----ф у н к ц и я - операция объединения вершин ---
void Graph::operator++() { bool *mas = new bool[N]; for(int i=0; i<N; i++) if(Matr[x1-1][i] || Matr[x2-1][i]) mas[i]=1; else mas[i]=0; mas[x1-1]=0; mas[x2-1]=0; cout<<"\n Tops "<<x1<<" OR "<<x2<< " adjacen­cy tops: "; for(int i=0; i<N; i++) if(mas[i]) cout<< i+1<<" "; delete mas; }
Приложения
112
//------ ф у н к ц и я - операция пресесечения вершин
void My_Class::operator++() { bool *mas = new bool[N]; for(int i=0; i<N; i++) if(Matr[x1-1][i] && Matr[x2-1][i]) mas[i]=1; else mas[i]=0; mas[x1-1]=0; mas[x2-1]=0; cout<<"\n Tops "<<x1<<" AND "<<x2<< " adjacency tops : "; for(int i=0; i<N; i++) if(mas[i]) cout<< i+1<<" "; delete mas; }
//-------- Г л а в н а я функция --------------------
#pragma argsused int main() {
textbackground(WHITE);
textcolor(BLACK);
clrscr(); int number; cin>>number; My_Class * graph = new My_Class(number); +(*graph); // ввод матрицы смежности графа
-(*graph); // вывод матрицы смежности графа
*(*graph); //ввод номеров двух вершин ++(*graph); graph->Graph::operator++();
*(*graph); //ввод номеров двух вершин cout<< endl<<" Tops ";
Приложение 4
113
if( !(*graph)) cout<<" Adjacent"; else cout<< " No Adjacent";
delete graph; getch(); return 0; } //----------------------------------------------------
Приложение 3
114
Приложение 5
//--------- Бинарные функции-операции ----------------
#include <vcl.h> #pragma hdrstop #include <iostream.h> #include <conio.h>
//-------о п и с а н и е класса Graph----------------
class Graph { protected: int N; bool **Matr; int x1,x2; public: Graph(int); ~Graph(); void operator+(); //ввод графа void operator-(); //вывод графа virtual void operator++(); };
//---------о п и с а н и е класса My_Class-----------
class My_Class : public Graph { public: void operator*(); //ввод номеров двух вершин int operator!(); //проверка смежности двух //вершин My_Class(int a ):Graph(a){} void operator++(); void operator>(int);
void operator||(My_Class *); int return_ij(int i,int j){return Matr[i][j];}
};
Приложение 5
115
//----ф у н к ц и я объединения двух графов-----
void My_Class::operator||(My_Class * graph_2) { //выделение динамической памяти для матрицы смежности
//объединенного графа
bool **union_matr; union_matr=new bool *[N]; for(int i=0; i<N;i++) union_matr[i]= new bool [N];
//построение матрицы смежности объединенного графа
for(int i=0; i<N; i++) for(int j=0; j<N; j++) if(Matr[i][j]||graph_2->return_ij(i,j)) union_matr[i][j]=1; else union_matr[i][j]=0;
//вывод на экран матрицы смежности объединенного графа
cout<<endl<<"Matrix of Union Graph"; for(int i=0; i<N; i++) { cout<<endl; for(int j=0; j<N; j++) cout<<union_matr[i][j]<<" "; }
//удаление из динамической памяти матрицы смежности
//объединенного графа
for(int i=0; i<N;i++) delete[] union_matr[i]; delete[] union_matr; }
Приложения
116
//----ф у н к ц и я вывода на экран вершин смежных //заданной вершине
void My_Class::operator>(int x) { cout<<" Set of Tops adjacent top "<<x<<": {"; for(int i=0; i<N; i++) if(Matr[x-1][i]) cout<<" "<<i+1<<","; cout<<" }"; }
//-----ф у н к ц и я проверки смежности двух вершин --
int My_Class::operator!() { if(Matr[x1-1][x2-1]) return 1; else return 0; }
//------ф у н к ц и я ввода номеров двух вершин-------
void My_Class::operator*() { cout<<endl<<" Input number of two tops: "; cin>>x1>>x2; }
//-------к о н с т р у к т о р класса Graph-----------
Graph::Graph(int n1) { N=n1; Matr=new bool *[N]; for(int i=0; i<N;i++) Matr[i]= new bool [N]; }
Приложение 5
117
//-------д е с т р у к т о р класса Graph-------------
Graph::~Graph() { for(int i=0; i<N;i++) delete[] Matr[i]; delete[] Matr; }
//---- ф у н к ц и я - о п е р а ц и я ввода графа ---
void Graph::operator+() { cout <<endl<<" Input Matrix of Graph"; for(int i=0; i<N; i++) { cout <<endl; for(int j=0; j<N; j++) { cout <<" Matr["<< i <<"]["<< j <<"]="; cin >> Matr[i][j]; } cout << endl; } }
//--- ф у н к ц и я - о п е р а ц и я вывода графа ---
void Graph::operator-() { for(int i=0; i<N; i++) for(int j=0; j<N; j++) { cout << " Matr["<< i <<"]["<< j <<"]="; cout << Matr[i][j] <<endl; } }
Приложения
118
//--ф у н к ц и я - о п е р а ц и я объединения вершин
void Graph::operator++() { bool *mas = new bool[N]; for(int i=0; i<N; i++) if(Matr[x1-1][i] || Matr[x2-1][i]) mas[i]=1; else mas[i]=0; mas[x1-1]=0; mas[x2-1]=0; cout<<"\n Tops "<<x1<<" OR "<<x2<< " adjacen­cy tops: "; for(int i=0; i<N; i++) if(mas[i]) cout<< i+1<<" "; delete mas; }
//--ф у н к ц и я - о п е р а ц и я пересечения вершин
void My_Class::operator++() { bool *mas = new bool[N]; for(int i=0; i<N; i++) if(Matr[x1-1][i] && Matr[x2-1][i]) mas[i]=1; else mas[i]=0; mas[x1-1]=0; mas[x2-1]=0; cout<<"\n Tops "<<x1<<" AND "<<x2<< " adjacency tops : "; for(int i=0; i<N; i++) if(mas[i]) cout<< i+1<<" "; delete mas; }
//------- Г л а в н а я функция ---------------------
#pragma argsused int main() {
Приложение 5
119
textbackground(WHITE); textcolor(BLACK);
clrscr(); int number; cout <<" Input number of Graph Tops: "; cin>>number; My_Class * graph = new My_Class(number); +(*graph); // ввод матрицы смежности первого графа //-(*graph); // вывод матрицы смежности графа
*(*graph); //ввод номеров двух вершин ++(*graph); graph->Graph::operator++();
*(*graph); //ввод номеров двух вершин cout<< " Tops "; if( !(*graph)) cout<<" Adjacent"; else cout<< " No Adjacent";
//----- вершины смежные с заданной вершиной --------
cout<<endl<<" Input number of top: "; int x1; cin>>x1; (*graph)>x1; //создание второго объекта
My_Class * graph_1 = new My_Class(number); +(*graph_1); // ввод матрицы смежности второго графа
//вывод на экран матрицы смежности объединения //двух графов (*graph)||graph_1;
delete graph; getch(); return 0; } //----------------------------------------------------
Приложения
120
Приложение 6
//------------ Иерархия классов ----------------------
#include <vcl.h> #pragma hdrstop #include <iostream.h> #include <conio.h>
//--------- о п и с а н и е класса Graph ------------
class Graph { protected: int N; public: void output_N(){cout<<N;} int return_N(){return N;} virtual void input_graph() =0; virtual void output_graph() =0; virtual int adjacent(int,int)=0; Graph(int n1){N=n1;} };
//----------- о п и с а н и е класса Matr -----------
class Matr : public Graph { protected: bool **matr; public: void input_graph(); void output_graph(); int adjacent(int,int); Matr(int); ~Matr(); };
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]