Ориентированные, неориентированные графы. Выводит связанные вершины по матрице смежности
Код C++
//Graph.h
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <vector>
class Graph
{
public:
Graph(){}
virtual ~Graph(){}
void SetSize(int size_);
const int GetSize() const {return size;}
virtual void Input()=0;
void Output();
void Resize();
protected:
std::vector<std::vector<int> > VecGraph;
int size;
};
class OrGraph:public Graph
{
public:
OrGraph(){}
~OrGraph(){}
void Input();
};
class UnOrGraph:public Graph
{
public:
UnOrGraph(){}
~UnOrGraph(){}
void Input();
};
#endif
Код C++
//Graph.cpp
#include "Graph.h"
#include <iostream>
#include <algorithm>
#include <iterator>
//Functions for abstract Graph
void Graph::SetSize(int size_)
{
size=size_;
Resize();
}
void Graph::Output()
{
for(int i=0; i<size; ++i)
{
std::copy(VecGraph[i].begin(), VecGraph[i].end(), std::ostream_iterator<int>(std::cout, " "));
std::cout<<std::endl;
}
for(int i=0; i<size; ++i)
{
for(int j=0; j<size; ++j)
{
if(VecGraph[i][j]==1)
std::cout<<i+1<<" node is connected with "<< j+1 <<" node.\n";
}
}
}
void Graph::Resize()
{
VecGraph.resize(size);
for(int i=0; i<size; ++i)
VecGraph[i].resize(size);
}
//Functions for UnOrGraph
void UnOrGraph::Input()
{
for(int i=0; i<size; ++i)
{
for(int j=0; j<size; ++j)
{
if(i>j)
continue;
std::cout<<"Enter 1 for connect "<< i+1 <<" node with "<< j+1 <<" node: ";
std::cin>>VecGraph[i][j];
if(i!=j)
VecGraph[j][i]=VecGraph[i][j];
}
}
}
//Functions for OrGraph
void OrGraph::Input()
{
for(int i=0; i<size; ++i)
{
for(int j=0; j<size; ++j)
{
std::cout<<"Enter 1 for connect "<< i+1 <<" node with "<< j+1 <<" node: ";
std::cin>>VecGraph[i][j];
}
}
}
Код C++
//Main.cpp
#include <iostream>
#include <stdexcept>
#include "Graph.h"
int main()
{
try
{
int size=0;
int state=0;
Graph*pOb;
std::cout<<"Enter numb of nodes: ";
std::cin>>size;
if(size<0)
throw "Numb of nodes can`t be less then zero. End of program\n";
else if(size==0)
{
std::cout<<"It is empty graph\n";
return 0;
}
std::cout<<"Enter 1 for unor graph and 2 for or graph: ";
std::cin>>state;
if((state!=1)&&(state!=2))
{
throw "You can only input 1 for unor graph or two for or graph. End of program\n";
}
if(state==2)
pOb=new OrGraph;
else if(state==1)
pOb=new UnOrGraph;
pOb->SetSize(size);
pOb->Input();
pOb->Output();
}
catch(char*str)
{
std::cerr<<str;
return EXIT_FAILURE;
}
system("pause");
return 0;
}
Комментарии:
Нету комментариев для вывода...