From ba9943117d85caf53b4c8db83f986f0d03d35d4e Mon Sep 17 00:00:00 2001 From: dheeraj bhosale Date: Thu, 8 Oct 2020 14:27:26 +0530 Subject: [PATCH] Add Graph demos in Cpp. --- demos/cpp/.vscode/launch.json | 9 ++- demos/cpp/.vscode/tasks.json | 44 ++++++------ demos/cpp/object_Oriented_Graph.cpp | 78 ++++++++++++++++++++++ demos/cpp/structural_Programming_Graph.cpp | 52 +++++++++++++++ 4 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 demos/cpp/object_Oriented_Graph.cpp create mode 100644 demos/cpp/structural_Programming_Graph.cpp diff --git a/demos/cpp/.vscode/launch.json b/demos/cpp/.vscode/launch.json index 1c812cf..0f5a20d 100644 --- a/demos/cpp/.vscode/launch.json +++ b/demos/cpp/.vscode/launch.json @@ -8,12 +8,17 @@ "name": "g++ build and debug active file", "type": "cppdbg", "request": "launch", - "program": "${fileDirname}/${fileBasenameNoExtension}", + "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, + "logging": { + "trace": true, + "traceResponse": true, + "engineLogging": false + }, "MIMode": "gdb", "setupCommands": [ { @@ -23,7 +28,7 @@ } ], "preLaunchTask": "g++ build active file", - "miDebuggerPath": "/usr/bin/gdb" + "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe" } ] } \ No newline at end of file diff --git a/demos/cpp/.vscode/tasks.json b/demos/cpp/.vscode/tasks.json index 366a347..be5bdfe 100644 --- a/demos/cpp/.vscode/tasks.json +++ b/demos/cpp/.vscode/tasks.json @@ -1,25 +1,25 @@ { - // See https://go.microsoft.com/fwlink/?LinkId=733558 + // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format - "version": "2.0.0", - "tasks": [ - { - "type": "shell", - "label": "g++ build active file", - "command": "/usr/bin/g++", - "args": [ - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "/usr/bin" - }, - "problemMatcher": [ - "$gcc" - ], - "group": "build" - } - ] + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "g++ build active file", + "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe", + "args": [ + "-g", + "${file}", + "-o", + "${workspaceRoot}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": "build" + } + ] } \ No newline at end of file diff --git a/demos/cpp/object_Oriented_Graph.cpp b/demos/cpp/object_Oriented_Graph.cpp new file mode 100644 index 0000000..79754d3 --- /dev/null +++ b/demos/cpp/object_Oriented_Graph.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +#include + +using namespace jsoncons; // for convenience +using namespace std; + +// visualize `myGraphJson`! +string myGraphJson = "{\"kind\":{\"graph\":true}," + "\"nodes\":[{\"id\":\"1\"},{\"id\":\"2\"}]," + "\"edges\":[{\"from\":\"1\",\"to\":\"2\"}]}"; + +class Graph +{ +private: + char *tempPtr = NULL; + char *constPtr = NULL; + ojson j; + +public: + Graph() + { + tempPtr = new char[myGraphJson.length() + 1]; + strcpy(tempPtr, myGraphJson.c_str()); + constPtr = tempPtr; + j = ojson::parse(constPtr); + } + + ~Graph() + { + delete[] constPtr; + constPtr = NULL; + tempPtr = NULL; + } + void addNode(string NodeValue) + { + multimap Node; + Node.emplace("id", NodeValue); + Node.emplace("label", NodeValue); + Node.emplace("color", "orange"); + j["nodes"].push_back(Node); + } + + void addEdge(string from, string to) + { + multimap Edge; + Edge.emplace("from", from); + Edge.emplace("to", to); + Edge.emplace("color", "blue"); + j["edges"].push_back(Edge); + } + + void visualize() + { + myGraphJson = ""; + j.dump(myGraphJson); + } +}; + +int main() +{ + Graph *g1 = new Graph(); // apply breakpoint here + // put command once "-exec set print elements 0" in debug console + g1->visualize(); + g1->addNode("3"); + g1->visualize(); + g1->addNode("4"); + g1->visualize(); + g1->addEdge("1", "3"); + g1->visualize(); + g1->addEdge("1", "4"); + g1->visualize(); + + return 0; +} diff --git a/demos/cpp/structural_Programming_Graph.cpp b/demos/cpp/structural_Programming_Graph.cpp new file mode 100644 index 0000000..3b96fb8 --- /dev/null +++ b/demos/cpp/structural_Programming_Graph.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include + +using namespace jsoncons; // for convenience +using namespace std; + +void addNode(ojson &j, string NodeValue) +{ + multimap Node; + Node.emplace("id", NodeValue); + Node.emplace("label", NodeValue); + Node.emplace("color", "orange"); + j["nodes"].push_back(Node); +} +void addEdge(ojson &j, string from, string to) +{ + multimap Edge; + Edge.emplace("from", from); + Edge.emplace("to", to); + Edge.emplace("color", "blue"); + j["edges"].push_back(Edge); +} + +int main() +{ + // visualize `myGraphJson`! + string myGraphJson = "{\"kind\":{\"graph\":true}," + "\"nodes\":[{\"id\":\"1\"},{\"id\":\"2\"}]," + "\"edges\":[{\"from\":\"1\",\"to\":\"2\"}]}"; + + char *tempPtr = new char[myGraphJson.length() + 1]; + strcpy(tempPtr, myGraphJson.c_str()); + char *constPtr = tempPtr; + ojson j = ojson::parse(constPtr); + + string temps1, temps2, temps3; // apply breakpoint here + // put command once "-exec set print elements 0" in debug console + addNode(j, "3"); + j.dump(temps1); // visualize temps1 + cout << myGraphJson; + addNode(j, "4"); + addEdge(j, "1", "3"); + j.dump(temps2); // visualize temps2 + cout << myGraphJson; + addEdge(j, "1", "4"); + j.dump(temps3); // visualize temps3 + cout << myGraphJson; +} \ No newline at end of file