😭
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(homework5)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(test1 test1.cpp
|
||||
MatrixGraph.h
|
||||
GraphExceptions/InsertExistedConnectException.h)
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(homework5)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(test1 test1.cpp
|
||||
MatrixGraph.h
|
||||
GraphExceptions/InsertExistedConnectException.h)
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
// 这是一个异常,当用户尝试插入已存在的连接时抛出
|
||||
|
||||
#ifndef INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
#define INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
||||
class InsertExistedConnectException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
InsertExistedConnectException():message("You input id is out of range"){}
|
||||
InsertExistedConnectException(const std::string& msg) : message(msg) {}
|
||||
InsertExistedConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~InsertExistedConnectException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
// 这是一个异常,当用户尝试插入已存在的连接时抛出
|
||||
|
||||
#ifndef INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
#define INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
||||
class InsertExistedConnectException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
InsertExistedConnectException():message("You input id is out of range"){}
|
||||
InsertExistedConnectException(const std::string& msg) : message(msg) {}
|
||||
InsertExistedConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~InsertExistedConnectException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //INSERTEXISTEDCONNECTEXCEPTION_H
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
// 这是一个异常,当用户给的节点id超出最大值时抛出
|
||||
|
||||
#ifndef NODEIDOUTOFRANGEEXCEPTION_H
|
||||
#define NODEIDOUTOFRANGEEXCEPTION_H
|
||||
#include <exception>
|
||||
#include<string>
|
||||
|
||||
class NodeIdOutOfRangeException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
NodeIdOutOfRangeException():message("You input id is out of range"){}
|
||||
NodeIdOutOfRangeException(const std::string& msg) : message(msg) {}
|
||||
NodeIdOutOfRangeException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~NodeIdOutOfRangeException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //NODEIDOUTOFRANGEEXCEPTION_H
|
||||
// 这是一个异常,当用户给的节点id超出最大值时抛出
|
||||
|
||||
#ifndef NODEIDOUTOFRANGEEXCEPTION_H
|
||||
#define NODEIDOUTOFRANGEEXCEPTION_H
|
||||
#include <exception>
|
||||
#include<string>
|
||||
|
||||
class NodeIdOutOfRangeException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
NodeIdOutOfRangeException():message("You input id is out of range"){}
|
||||
NodeIdOutOfRangeException(const std::string& msg) : message(msg) {}
|
||||
NodeIdOutOfRangeException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~NodeIdOutOfRangeException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //NODEIDOUTOFRANGEEXCEPTION_H
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
// 这是一个异常,当用户试图连接图中的两个相同节点时抛出
|
||||
|
||||
#ifndef SAMENODECONNECTEXCEPTION_H
|
||||
#define SAMENODECONNECTEXCEPTION_H
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
||||
class SameNodeConnectException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
SameNodeConnectException():message("Don't connect same node"){}
|
||||
SameNodeConnectException(const std::string& msg) : message(msg) {}
|
||||
SameNodeConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~SameNodeConnectException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
};
|
||||
#endif //SAMENODECONNECTEXCEPTION_H
|
||||
// 这是一个异常,当用户试图连接图中的两个相同节点时抛出
|
||||
|
||||
#ifndef SAMENODECONNECTEXCEPTION_H
|
||||
#define SAMENODECONNECTEXCEPTION_H
|
||||
#include <exception>
|
||||
#include <string>
|
||||
|
||||
|
||||
class SameNodeConnectException : public std::exception{
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
SameNodeConnectException():message("Don't connect same node"){}
|
||||
SameNodeConnectException(const std::string& msg) : message(msg) {}
|
||||
SameNodeConnectException(const std::string& msg,int errCode):message(msg+" Err code: "+std::to_string(errCode)){}
|
||||
|
||||
virtual ~SameNodeConnectException() noexcept {}
|
||||
virtual const char* what() const noexcept override {
|
||||
return message.c_str();
|
||||
}
|
||||
};
|
||||
#endif //SAMENODECONNECTEXCEPTION_H
|
||||
|
||||
+140
-136
@@ -1,136 +1,140 @@
|
||||
//
|
||||
// Created by 31416 on 24-12-4.
|
||||
//
|
||||
|
||||
#ifndef LIST_GRAPH_H
|
||||
#define LIST_GRAPH_H
|
||||
|
||||
#include <sstream>
|
||||
#include<vector>
|
||||
|
||||
#include "GraphExceptions/InsertExistedConnectException.h"
|
||||
#include "GraphExceptions/NodeIdOutOfRangeException.h"
|
||||
#include "GraphExceptions/SameNodeConnectException.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* 简述:
|
||||
* 节点=数据域+指针域
|
||||
* 第一个节点存储id
|
||||
* 第二个节点存储值
|
||||
* 后续节点存储连接关系
|
||||
* 数据域:
|
||||
* data:可存id,可存其他节点的id,可存数据
|
||||
* weight:边上的权
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
class ListGraph {
|
||||
private:
|
||||
struct node{
|
||||
T data;
|
||||
T weight;
|
||||
struct node* next;
|
||||
};
|
||||
vector<node*> nodes;
|
||||
bool flag; //定义是否有向
|
||||
bool isWeighted; //定义是否有权
|
||||
|
||||
public:
|
||||
ListGraph() {
|
||||
nodes.clear();
|
||||
flag = false;
|
||||
isWeighted = false;
|
||||
}
|
||||
ListGraph(bool flag) {
|
||||
nodes.clear();
|
||||
this->flag = flag;
|
||||
isWeighted = false;
|
||||
}
|
||||
ListGraph(bool flag,bool isWeighted) {
|
||||
nodes.clear();
|
||||
this->flag = flag;
|
||||
this->isWeighted = isWeighted;
|
||||
}
|
||||
void insert(T data) {
|
||||
node* insert = new node();
|
||||
insert->data=nodes.size();
|
||||
insert->weight = 0;
|
||||
insert->next=new node();
|
||||
insert->next->data=data;
|
||||
insert->next->next=NULL;
|
||||
nodes.push_back(insert);
|
||||
}
|
||||
void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图
|
||||
if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) {
|
||||
throw NodeIdOutOfRangeException("您提供的id超出范围",0X002);
|
||||
}
|
||||
if(id1==id2) {
|
||||
throw SameNodeConnectException("禁止连接图中的同一个点",0x001);
|
||||
}
|
||||
node *current = nodes.at(id1);
|
||||
int current_id = 0;
|
||||
while(current->next!=NULL) {
|
||||
if((current->next->data==id2)&&(current_id>1)) {
|
||||
if (current->next->weight==weight) {
|
||||
throw InsertExistedConnectException("禁止插入已存在的连接",0x003);
|
||||
}
|
||||
current->next->weight=weight;
|
||||
}
|
||||
current = current->next;
|
||||
current_id++;
|
||||
}
|
||||
current->next=new node();
|
||||
current->next->data=id2;
|
||||
current->next->weight = weight;
|
||||
current->next->next=NULL;
|
||||
|
||||
if (!flag) {
|
||||
node *current2 = nodes.at(id2);
|
||||
int current2_id = 0;
|
||||
while(current2->next!=NULL) {
|
||||
if((current2->next->data==id2)&&(current2_id>1)) {
|
||||
if (current2->next->weight==weight) {
|
||||
throw InsertExistedConnectException("禁止插入已存在的连接",0x003);
|
||||
}
|
||||
current2->next->weight=weight;
|
||||
}
|
||||
current2 = current2->next;
|
||||
current2_id++;
|
||||
}
|
||||
current2->next=new node();
|
||||
current2->next->data=id1;
|
||||
current2->next->weight = weight;
|
||||
current2->next->next=NULL;
|
||||
}
|
||||
}
|
||||
string to_string() {
|
||||
stringstream ss;
|
||||
for (int i=0;i<=nodes.size()-1;i++) {
|
||||
node* current = nodes.at(i);
|
||||
while(current!=NULL) {
|
||||
if(current->next==NULL) {
|
||||
if (isWeighted) {
|
||||
ss<<current->data<<"|"<<current->weight;
|
||||
}
|
||||
else {
|
||||
ss<<current->data;
|
||||
}
|
||||
}else {
|
||||
if (isWeighted){
|
||||
ss<<current->data<<"|"<<current->weight<<" ";
|
||||
}
|
||||
else {
|
||||
ss<<current->data<<" ";
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
ss<<endl;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //LIST_GRAPH_H
|
||||
//
|
||||
// Created by 31416 on 24-12-4.
|
||||
//
|
||||
|
||||
#ifndef LIST_GRAPH_H
|
||||
#define LIST_GRAPH_H
|
||||
|
||||
#include <sstream>
|
||||
#include<vector>
|
||||
|
||||
#include "GraphExceptions/InsertExistedConnectException.h"
|
||||
#include "GraphExceptions/NodeIdOutOfRangeException.h"
|
||||
#include "GraphExceptions/SameNodeConnectException.h"
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* 简述:
|
||||
* 节点=数据域+指针域
|
||||
* 第一个节点存储id
|
||||
* 第二个节点存储值
|
||||
* 后续节点存储连接关系
|
||||
* 数据域:
|
||||
* data:可存id,可存其他节点的id,可存数据
|
||||
* weight:边上的权
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
class ListGraph {
|
||||
private:
|
||||
struct node{
|
||||
T data;
|
||||
T weight;
|
||||
struct node* next;
|
||||
};
|
||||
vector<node*> nodes;
|
||||
bool flag; //定义是否有向
|
||||
bool isWeighted; //定义是否有权
|
||||
|
||||
public:
|
||||
ListGraph() {
|
||||
nodes.clear();
|
||||
flag = false;
|
||||
isWeighted = false;
|
||||
}
|
||||
ListGraph(bool flag) {
|
||||
nodes.clear();
|
||||
this->flag = flag;
|
||||
isWeighted = false;
|
||||
}
|
||||
ListGraph(bool flag,bool isWeighted) {
|
||||
nodes.clear();
|
||||
this->flag = flag;
|
||||
this->isWeighted = isWeighted;
|
||||
}
|
||||
void insert(T data) {
|
||||
node* insert = new node();
|
||||
insert->data=nodes.size();
|
||||
insert->weight = 0;
|
||||
insert->next=new node();
|
||||
insert->next->data=data;
|
||||
insert->next->next=NULL;
|
||||
nodes.push_back(insert);
|
||||
}
|
||||
void connect(int id1,int id2,T weight=0) { //flag区分是否为有向图
|
||||
if ((id1>nodes.size()-1||id2>nodes.size()-1)||(id1<0)||(id2<0)) {
|
||||
throw NodeIdOutOfRangeException("您提供的id超出范围",0X002);
|
||||
}
|
||||
if(id1==id2) {
|
||||
throw SameNodeConnectException("禁止连接图中的同一个点",0x001);
|
||||
}
|
||||
node *current = nodes.at(id1);
|
||||
//跳过id节点和值节点
|
||||
for(int i=0;i<2;i++) {
|
||||
current = current->next;
|
||||
}
|
||||
while(current->next!=NULL) {
|
||||
if(current->next->data==id2) {
|
||||
if (current->next->weight==weight) {
|
||||
throw InsertExistedConnectException("禁止插入已存在的连接",0x003);
|
||||
}
|
||||
current->next->weight=weight;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
current->next=new node();
|
||||
current->next->data=id2;
|
||||
current->next->weight = weight;
|
||||
current->next->next=NULL;
|
||||
|
||||
if (!flag) {
|
||||
node *current2 = nodes.at(id2);
|
||||
//跳过id节点和值节点
|
||||
for(int i=0;i<2;i++) {
|
||||
current = current->next;
|
||||
}
|
||||
while(current2->next!=NULL) {
|
||||
if((current2->next->data==id2)) {
|
||||
if (current2->next->weight==weight) {
|
||||
throw InsertExistedConnectException("禁止插入已存在的连接",0x003);
|
||||
}
|
||||
current2->next->weight=weight;
|
||||
}
|
||||
current2 = current2->next;
|
||||
}
|
||||
current2->next=new node();
|
||||
current2->next->data=id1;
|
||||
current2->next->weight = weight;
|
||||
current2->next->next=NULL;
|
||||
}
|
||||
}
|
||||
string to_string() {
|
||||
stringstream ss;
|
||||
for (int i=0;i<=nodes.size()-1;i++) {
|
||||
node* current = nodes.at(i);
|
||||
while(current!=NULL) {
|
||||
if(current->next==NULL) {
|
||||
if (isWeighted) {
|
||||
ss<<current->data<<"|"<<current->weight;
|
||||
}
|
||||
else {
|
||||
ss<<current->data;
|
||||
}
|
||||
}else {
|
||||
if (isWeighted){
|
||||
ss<<current->data<<"|"<<current->weight<<" ";
|
||||
}
|
||||
else {
|
||||
ss<<current->data<<" ";
|
||||
}
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
ss<<endl;
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //LIST_GRAPH_H
|
||||
|
||||
+35
-35
@@ -1,35 +1,35 @@
|
||||
//
|
||||
// Created by 31416 on 24-12-5.
|
||||
//
|
||||
|
||||
#ifndef MATRIXGRAPH_H
|
||||
#define MATRIXGRAPH_H
|
||||
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
class MatrixGraph {
|
||||
private:
|
||||
vector<T> data;
|
||||
vector<vector<bool> > matrix;
|
||||
int nodeCount;
|
||||
public:
|
||||
MatrixGraph() {
|
||||
matrix.clear();
|
||||
data.clear();
|
||||
nodeCount = 0;
|
||||
}
|
||||
void insert(T value) {
|
||||
data.push_back(value);
|
||||
nodeCount++;
|
||||
matrix.push_back(vector<bool>(nodeCount, false));
|
||||
for(int i = 0; i < nodeCount-1; ++i) {
|
||||
matrix[i].push_back(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //MATRIXGRAPH_H
|
||||
//
|
||||
// Created by 31416 on 24-12-5.
|
||||
//
|
||||
|
||||
#ifndef MATRIXGRAPH_H
|
||||
#define MATRIXGRAPH_H
|
||||
|
||||
#include<vector>
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
class MatrixGraph {
|
||||
private:
|
||||
vector<T> data;
|
||||
vector<vector<bool> > matrix;
|
||||
int nodeCount;
|
||||
public:
|
||||
MatrixGraph() {
|
||||
matrix.clear();
|
||||
data.clear();
|
||||
nodeCount = 0;
|
||||
}
|
||||
void insert(T value) {
|
||||
data.push_back(value);
|
||||
nodeCount++;
|
||||
matrix.push_back(vector<bool>(nodeCount, false));
|
||||
for(int i = 0; i < nodeCount-1; ++i) {
|
||||
matrix[i].push_back(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //MATRIXGRAPH_H
|
||||
|
||||
+35
-35
@@ -1,35 +1,35 @@
|
||||
// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。
|
||||
// 输入:
|
||||
// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如:
|
||||
// 4 4
|
||||
// 0 1
|
||||
// 1 3
|
||||
// 0 3
|
||||
// 0 2
|
||||
// 输出:
|
||||
// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。
|
||||
// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。
|
||||
// 例如,对于上面的示例输入,输出为:
|
||||
// 0 1 1 1
|
||||
// 1 0 0 1
|
||||
// 1 0 0 0
|
||||
// 1 1 0 0
|
||||
// 从顶点0开始的深度优先遍历序列:
|
||||
// 0 1 3 2
|
||||
// 说明
|
||||
// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。
|
||||
// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。
|
||||
|
||||
#include"ListGraph.h"
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ListGraph<int> list_graph;
|
||||
list_graph.insert(1);
|
||||
list_graph.insert(2);
|
||||
list_graph.connect(0,1);
|
||||
list_graph.connect(1,0);
|
||||
cout << list_graph.to_string() << endl;
|
||||
return 0;
|
||||
}
|
||||
// 利用邻接矩阵存储无向图,并从0号顶点开始进行深度优先遍历。
|
||||
// 输入:
|
||||
// 输入第一行是两个整数n e,其中n表示顶点数(则顶点编号为0至n-1),e表示图中的边数。之后有e行信息输入,每行输入表示一条边,格式是“顶点1 顶点2”,把边插入图中。如:
|
||||
// 4 4
|
||||
// 0 1
|
||||
// 1 3
|
||||
// 0 3
|
||||
// 0 2
|
||||
// 输出:
|
||||
// 先输出存储图的邻接矩阵,同一行元素之间空1格,最后一个元素之后不要有空格。
|
||||
// 之后空一行后输出从0号顶点开始的深度优先遍历序列,顶点编号之间空1格。
|
||||
// 例如,对于上面的示例输入,输出为:
|
||||
// 0 1 1 1
|
||||
// 1 0 0 1
|
||||
// 1 0 0 0
|
||||
// 1 1 0 0
|
||||
// 从顶点0开始的深度优先遍历序列:
|
||||
// 0 1 3 2
|
||||
// 说明
|
||||
// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。
|
||||
// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。
|
||||
|
||||
#include"ListGraph.h"
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ListGraph<int> list_graph;
|
||||
list_graph.insert(1);
|
||||
list_graph.insert(2);
|
||||
list_graph.connect(0,1);
|
||||
list_graph.connect(1,0);
|
||||
cout << list_graph.to_string() << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user