2 次代码提交 7b74b2150c ... bd6da91190

作者 SHA1 备注 提交日期
  m513t20 bd6da91190 graphs_readfile_but_bugged 1 年之前
  m513t20 4ab863208d graphs+reading_testgraph_bugged 1 年之前
共有 5 个文件被更改,包括 144 次插入39 次删除
  1. 1 2
      graph/FS.cpp
  2. 2 26
      graph/FS.h
  3. 75 1
      graph/graph.cpp
  4. 42 5
      graph/graph.h
  5. 24 5
      graph/main.cpp

+ 1 - 2
graph/FS.cpp

@@ -1,6 +1,5 @@
-#include "FS.h"
+#include "graph.h"
 #include <queue>
-#include"graph.h"
 
 using namespace std;
 

+ 2 - 26
graph/FS.h

@@ -1,26 +1,2 @@
-#pragma once
-#include "graph.h"
-class BFS
-{
-    typedef set<Node*>::const_iterator node_iterator;
-    const Graph& graph;
-public:
-    BFS(const Graph& agraph) : graph(agraph) {}
-    bool connected(Node* begin, Node* end);
-    friend Graph;
-    friend Node;
-};
-
-class DFS {
-    typedef set<Node*>::const_iterator node_iterator;
-private:
-    const Graph& graph;
-    set<Node*> visited;
-    bool connected(Node* begin, Node* end, int depth);
-public:
-
-    DFS(const Graph& agraph) : graph(agraph) {}
-    bool connected(Node* begin, Node* end);
-    friend Graph;
-    friend Node;
-};
+//#pragma once
+//#include "graph.h"

+ 75 - 1
graph/graph.cpp

@@ -11,6 +11,10 @@ void Node::remove_neighboor(Node* neighboor)
 	neighboors.erase(neighboor);
 }
 
+Node::Node()
+{
+}
+
 Node::Node(const string& aname)
 {
 	name = aname;
@@ -21,11 +25,23 @@ const string Node::get_name() const
 	return name;
 }
 
+const bool& Node::operator==(const Node& r)
+{
+	return name == r.name;
+}
+
 
 
 void Graph::add_node(Node* node)
 {
-	nodes.insert(node);
+	if (taken_names[node->name]) {
+		throw Graph_exception();
+
+	}
+	else {
+		taken_names[node->name] = true;
+		nodes.insert(node);
+	}
 }
 
 void Graph::remove_node(Node* node)
@@ -60,6 +76,64 @@ void Graph::remove_edge(Node* begin, Node* end)
 
 
 
+int get_number_from_string(int start, string word) {
+	int dec = 1, res = 0;
+	while (start>=0 && word[start] >= 48 && word[start] <= 57 ) {
+		res += ((word[start] - 48) * dec);
+		dec *= 10;
+		start--;
+	}
+	return res;
+
+
+}
+
+
+void Graph::read_file(string file_name)
+{
+
+	ifstream input;
+	input.open(file_name);
+	map <string, Node> saved;
+	if (input.is_open()){
+		string line;
+		getline(input, line);
+		while (getline(input,line)) {
+			string fir="", sec="";
+			int splitter = 0;
+			while (line[splitter] != '\t')
+			{
+				fir += line[splitter];
+				splitter++;
+			}
+			splitter++;
+			while (line[splitter])
+			{
+				sec += line[splitter];
+				splitter++;
+			}
+
+
+			Node s = taken_names[fir] ? saved[fir] : Node(fir);
+			Node f = taken_names[sec] ? saved[sec] : Node(sec);
+
+			saved[fir] = s;
+			saved[sec] = f;
+
+			add_node(&s);
+			add_node(&f);
+
+			add_edge(&s, &f);
+
 
 
 
+
+		}
+	
+	}
+
+
+}
+
+

+ 42 - 5
graph/graph.h

@@ -1,10 +1,10 @@
 
 #include <string>
+#include <map>
 #include <set>
-using namespace std;
-
-
+#include <fstream>
 
+using namespace std;
 class Node {
 
 	typedef set<Node*>::const_iterator node_iterator;
@@ -16,6 +16,7 @@ private:
 	void remove_neighboor(Node* neighboor);
 
 public:
+	Node();
 	Node(const string& aname);
 	const string get_name() const;
 
@@ -27,20 +28,26 @@ public:
 		return neighboors.end();
 	};
 
+	const bool& operator == (const Node& r);
+
 
 	friend class Graph;
 
 
 
+
+
+
 };
 
+
 class Graph
 {
-
 	typedef set<Node*>::const_iterator node_iterator;
-	
 private:
+	map <string, bool> taken_names;
 	set<Node*> nodes;
+	
 public:
 	Graph() {};
 	void add_node(Node* node);
@@ -48,6 +55,7 @@ public:
 	void add_edge(Node* begin, Node* end);
 	void remove_edge(Node* begin, Node* end);
 
+	void read_file(string file_name);
 
 
 	node_iterator begin() {
@@ -57,5 +65,34 @@ public:
 	node_iterator end() {
 		return nodes.end();
 	};
+	friend class Node;
 };
 
+class Graph_exception {};
+
+
+class BFS
+{
+    typedef set<Node*>::const_iterator node_iterator;
+    const Graph& graph;
+public:
+    BFS(const Graph& agraph) : graph(agraph) {}
+    bool connected(Node* begin, Node* end);
+    friend Graph;
+    friend Node;
+};
+
+
+class DFS {
+    typedef set<Node*>::const_iterator node_iterator;
+private:
+    const Graph& graph;
+    set<Node*> visited;
+    bool connected(Node* begin, Node* end, int depth);
+public:
+
+    DFS(const Graph& agraph) : graph(agraph) {}
+    bool connected(Node* begin, Node* end);
+    friend Graph;
+    friend Node;
+};

+ 24 - 5
graph/main.cpp

@@ -4,15 +4,26 @@
 #include <iostream>
 #include <string>
 #include "graph.h"
-#include "FS.h"
 
 using namespace std;
 
+void check();
+
 int main()
 {
-    string a = "a", b = "b", c = "c",d="d",e="e";
-    Node A(a),B(b),C(c),D(d),E(e);
-    cout << A.get_name() << " " << B.get_name() << " "<< C.get_name();
+    Graph gr;
+    gr.read_file("testgraph.txt");
+
+}
+
+
+
+
+
+void check() {
+    string a = "a", b = "b", c = "c", d = "d", e = "e";
+    Node A(a), B(b), C(c), D(d), E(e), E2(e);
+    cout << A.get_name() << " " << B.get_name() << " " << C.get_name() << endl;
     Graph gr;
     gr.add_node(&A);
     gr.add_node(&B);
@@ -20,6 +31,14 @@ int main()
     gr.add_node(&D);
     gr.add_node(&E);
 
+    try {
+        gr.add_node(&E2);
+    }
+    catch (Graph_exception& ex) {
+        cout << "Name is already taken\n";
+    }
+
+
     gr.add_edge(&A, &B);
     gr.add_edge(&A, &C);
     gr.add_edge(&B, &C);
@@ -27,7 +46,7 @@ int main()
     gr.add_edge(&D, &C);
     DFS fs(gr);
 
-    cout << fs.connected(&B,&D) << " " << fs.connected(&D, &E) << " " << "END\n";
+    cout << fs.connected(&B, &D) << " " << fs.connected(&D, &E) << " " << "END\n";
 }
 
 // Запуск программы: CTRL+F5 или меню "Отладка" > "Запуск без отладки"