bug:connect函数在处理插入已存在连接时的业务逻辑错误

This commit is contained in:
2024-12-05 15:24:28 +08:00
parent b0f712fa4a
commit 08f6f4b0f4
6 changed files with 206 additions and 85 deletions
+2 -3
View File
@@ -4,6 +4,5 @@ project(homework5)
set(CMAKE_CXX_STANDARD 20)
add_executable(test1 test1.cpp
Graph.h
GraphExceptions/SameNodeConnectException.h
GraphExceptions/NodeIdOutOfRangeException.h)
MatrixGraph.h
GraphExceptions/InsertExistedConnectException.h)
-81
View File
@@ -1,81 +0,0 @@
//
// Created by 31416 on 24-12-4.
//
#ifndef GRAPH_H
#define GRAPH_H
#include<vector>
#include "GraphExceptions/NodeIdOutOfRangeException.h"
#include "GraphExceptions/SameNodeConnectException.h"
using namespace std;
/*
* 简述:
* 节点=数据域+指针域
* 第一个节点存储id
* 第二个节点存储值
* 后续节点存储连接关系
* 数据域:
* data:可存id,可存其他节点的id,可存数据
* weight:边上的权
*/
template<typename T>
class Graph {
private:
struct node{
T data;
T weight;
struct node* next;
};
vector<node*> nodes;
int max_size;
public:
Graph() {
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;
insert->next->next=NULL;
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;
}
}
};
#endif //GRAPH_H
@@ -0,0 +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
+136
View File
@@ -0,0 +1,136 @@
//
// 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
+35
View File
@@ -0,0 +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
+7 -1
View File
@@ -20,10 +20,16 @@
// 输入第1个4表示有4个顶点,第2个4表示有4条边。之后的4行输入代表4条边的顶点。
// 输出的前4行为邻接矩阵,之后空一行,然后输出的“0 1 3 2”是深度优先遍历序列。
# include "Graph.h"
#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;
}