Добавил:
ivanov666
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Основы объектно-ориентированного программирования задач на графах. Учебное пособие
.pdf
Приложение 2
101
Приложение 2
//------ Простое наследование ---------------
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <conio.h>
//---------- Б а з о в ы й класс -------------------
class Graph
{
protected: int N;
bool **Matr;
public: Graph(int);
~Graph();
void input_Graph();
void out_Graph();
void adjacency(int,int);
};
//---------- П р о и з в о д н ы й класс -----------
class My_Class : public Graph
{
public: int adjacent_nodes(int, int);
My_Class(int a):Graph(a){}
};
//-------------------------------------------------
int My_Class::adjacent_nodes(int x11, int x22)
{ if(Matr[x11-1][x22-1]) return 1;
else return 0;
}

Приложения
102
//--------------------------------------------------
Graph::Graph(int n1)
{
N=n1;
Matr=new bool *[N];
for(int i=0; i<N;i++)
Matr[i]= new bool [N];
}
//--------------------------------------------------
Graph::~Graph()
{
for(int i=0; i<N;i++)
delete Matr[i];
delete Matr;
}
//--------------------------------------------------
void Graph::input_Graph()
{
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
cout << " Matr["<< i <<"]["<< j <<"]=";
cin >> Matr[i][j];
}
cout << endl;
}
//--------------------------------------------------
void Graph::out_Graph()
{
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)

Приложение 2
103
{
cout << " Matr["<< i <<"]["<< j <<"]=";
cout << Matr[i][j] <<endl;
}
}
//--------------------------------------------------
void Graph::adjacency(int x11, int x22)
{
bool *mas = new bool[N];
for(int i=0; i<N; i++)
if(Matr[x11-1][i] || Matr[x22-1][i]) mas[i]=1;
else mas[i]=0;
mas[x11-1]=0; mas[x22-1]=0;
cout<<"\n Tops "<<x11<<" and "<<x22<< " 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;
int x1,x2;
cin>>number;
My_Class * graph = new My_Class(number);
graph->input_Graph();
graph->out_Graph();
cout<<" Input number of two tops: ";

Приложения
104
cin>>x1>>x2;
graph->adjacency(x1,x2);
cout<< endl<<" Tops "<<x1<<" and "<<x2;
if(graph->adjacent_nodes(x1,x2)) cout<<" Adjacent";
else cout<< " No Adjacent";
delete graph;
getch();
return 0;
}
//----------------------------------------------------

Приложение 3
105
Приложение 3
//------- Виртуальные функции ------------------------
#include <vcl.h>
#pragma hdrstop
#include <iostream.h>
#include <conio.h>
//------- о п и с а н и е класса Graph --------------
class Graph
{
protected: int N;
bool **Matr;
public: Graph(int);
~Graph();
void input_Graph();
void out_Graph();
virtual void adjacency(int,int);
};
//----------------------------------------------------
class My_Class : public Graph
{
public: int adjacent_nodes(int, int);
My_Class(int a ):Graph(a){}
void adjacency(int,int);
};
//----------------------------------------------------
int My_Class::adjacent_nodes(int x11, int x22)
{
if(Matr[x11-1][x22-1]) return 1;
else return 0;
}

Приложения
106
//----------------------------------------------------
Graph::Graph(int n1)
{
N=n1;
Matr=new bool *[N];
for(int i=0; i<N;i++)
Matr[i]= new bool [N];
}
//----------------------------------------------------
Graph::~Graph()
{
for(int i=0; i<N;i++)
delete Matr[i];
delete Matr;
}
//----------------------------------------------------
void Graph::input_Graph()
{
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
cout << " Matr["<< i <<"]["<< j <<"]=";
cin >> Matr[i][j];
}
cout << endl;
}
//----------------------------------------------------
void Graph::out_Graph()
{
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)

Приложение 3
107
{
cout << " Matr["<< i <<"]["<< j <<"]=";
cout << Matr[i][j] <<endl;
}
}
//----------------------------------------------------
void Graph::adjacency(int x11, int x22)
{
bool *mas = new bool[N];
for(int i=0; i<N; i++)
if(Matr[x11-1][i] || Matr[x22-1][i])
mas[i]=1;
else mas[i]=0;
mas[x11-1]=0; mas[x22-1]=0;
cout<<"\n Tops "<<x11<<" OR "<<x22<< " adjacency
tops: ";
for(int i=0; i<N; i++)if(mas[i]) cout<< i+1<<" ";
delete mas;
}
//----------------------------------------------------
void My_Class::adjacency(int x11, int x22)
{
bool *mas = new bool[N];
for(int i=0; i<N; i++)
if(Matr[x11-1][i] && Matr[x22-1][i])
mas[i]=1;
else mas[i]=0;
mas[x11-1]=0; mas[x22-1]=0;
cout<<"\n Tops "<<x11<<" AND "<<x22<< " adjacency
tops : ";
for(int i=0; i<N; i++)
if(mas[i]) cout<< i+1<<" ";
delete mas;
}

Приложения
108
//----------------------------------------------------
#pragma argsused
int main()
{
textbackground(WHITE);
textcolor(BLACK);
clrscr();
int number;
int x1,x2;
cin>>number;
My_Class * graph = new My_Class(number);
graph->input_Graph();
graph->out_Graph();
cout<<" Input number of two tops: ";
cin>>x1>>x2;
graph->adjacency(x1,x2);
graph->Graph::adjacency(x1,x2);
cout<< endl<<" Tops "<<x1<<" and "<<x2;
if(graph->adjacent_nodes(x1,x2)) cout<<" Adjacent";
else cout<< " No Adjacent";
delete graph;
getch();
return 0;
}
//---------------------------------------------------

Приложение 4
109
Приложение 4
//---------- Перегрузка операций ---------------------
#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++();
};
//----- ф у н к ц и я проверки смежности двух вершин
int My_Class::operator!()

Приложения
110
{
if(Matr[x1-1][x2-1]) return 1;
else return 0;
}
//------ф у н к ц и я ввода номеров двух вершин -----
void My_Class::operator*()
{
cout<<" 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];
}
//-------- д е с т р у к т о р класса Graph ----------
Graph::~Graph()
{
for(int i=0; i<N;i++)
delete Matr[i];
delete Matr;
}
//------ ф у н к ц и я - операция ввода графа --------
void Graph::operator+()
{
for(int i=0; i<N; i++)
Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]
