From 238975f6380ab0540baae638c206be76a35ba818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=A1=E5=9D=82=E6=98=B4?= Date: Thu, 5 Dec 2024 01:21:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E5=A5=BD=E8=8A=82=E7=82=B9=E6=8F=92?= =?UTF-8?q?=E5=85=A5=E5=92=8C=E8=BF=9E=E6=8E=A5=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=9C=89=E5=90=91=E5=9B=BE=E6=97=A0=E5=90=91=E5=9B=BE=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework5/CMakeLists.txt | 4 +- homework5/Graph.h | 65 +++++++++++++++++-- .../NodeIdOutOfRangeException.h | 25 +++++++ .../SameNodeConnectException.h | 22 +++++++ 4 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 homework5/GraphExceptions/NodeIdOutOfRangeException.h create mode 100644 homework5/GraphExceptions/SameNodeConnectException.h diff --git a/homework5/CMakeLists.txt b/homework5/CMakeLists.txt index 463eb54..81426e2 100644 --- a/homework5/CMakeLists.txt +++ b/homework5/CMakeLists.txt @@ -4,4 +4,6 @@ project(homework5) set(CMAKE_CXX_STANDARD 20) add_executable(test1 test1.cpp - Graph.h) + Graph.h + GraphExceptions/SameNodeConnectException.h + GraphExceptions/NodeIdOutOfRangeException.h) diff --git a/homework5/Graph.h b/homework5/Graph.h index 5ba9cd7..d3bbf15 100644 --- a/homework5/Graph.h +++ b/homework5/Graph.h @@ -6,20 +6,73 @@ #define GRAPH_H #include + +#include "GraphExceptions/NodeIdOutOfRangeException.h" +#include "GraphExceptions/SameNodeConnectException.h" using namespace std; +/* + * 简述: + * 节点=数据域+指针域 + * 第一个节点存储id + * 第二个节点存储值 + * 后续节点存储连接关系 + * 数据域: + * data:可存id,可存其他节点的id,可存数据 + * weight:边上的权 + */ + template class Graph { private: - vector ids; - Graph *node; - int max_id; + struct node{ + T data; + T weight; + struct node* next; + }; + vector nodes; + int max_size; public: Graph() { - ids.clear(); - node = NULL; - max_id = 0; + nodes.clear(); + max_size = 0; + } + void insert(T data) { + max_size++; + node insert = new node(); + insert->data=max_size; + insert->weight = 0; + insert->next=new node(); + insert->next->data=data; + nodes.push_back(insert); + } + void connect(int id1,int id2,bool flag,T weight=0) { //flag区分是否为有向图 + if(id1==id2) { + throw SameNodeConnectException("禁止连接图中的同一个点",0x001); + } + if ((id1>max_size||id2>max_size)||(id1<0)||(id2<0)) { + throw NodeIdOutOfRangeException("您提供的id超出范围",0X002); + } + node *current = nodes.at(id1); + while(current->next!=NULL) { + current = current->next; + } + current->next=new node(); + current->next->data=id2; + current->next->weight = weight; + current->next->next=NULL; + + if (flag==false) { + node *current = nodes.at(id2); + while(current->next!=NULL) { + current = current->next; + } + current->next=new node(); + current->next->data=id1; + current->next->weight = weight; + current->next->next=NULL; + } } }; diff --git a/homework5/GraphExceptions/NodeIdOutOfRangeException.h b/homework5/GraphExceptions/NodeIdOutOfRangeException.h new file mode 100644 index 0000000..42c935e --- /dev/null +++ b/homework5/GraphExceptions/NodeIdOutOfRangeException.h @@ -0,0 +1,25 @@ +// 这是一个异常,当用户给的节点id超出最大值时抛出 + +#ifndef NODEIDOUTOFRANGEEXCEPTION_H +#define NODEIDOUTOFRANGEEXCEPTION_H +#include +#include + +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 diff --git a/homework5/GraphExceptions/SameNodeConnectException.h b/homework5/GraphExceptions/SameNodeConnectException.h new file mode 100644 index 0000000..1a5f108 --- /dev/null +++ b/homework5/GraphExceptions/SameNodeConnectException.h @@ -0,0 +1,22 @@ +// 这是一个异常,当用户试图连接图中的两个相同节点时抛出 + +#ifndef SAMENODECONNECTEXCEPTION_H +#define SAMENODECONNECTEXCEPTION_H +#include +#include + + +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