diff --git a/VolkovAA/lab3/DOC/OtchyotLR2.docx b/VolkovAA/lab3/DOC/OtchyotLR2.docx new file mode 100644 index 000000000..7e47d8757 Binary files /dev/null and b/VolkovAA/lab3/DOC/OtchyotLR2.docx differ diff --git a/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj b/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj new file mode 100644 index 000000000..2035d9ddb --- /dev/null +++ b/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323} + Win32Proj + generate_graph + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + graph_lib.lib;%(AdditionalDependencies) + ../Debug + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + true + true + graph_lib.lib;%(AdditionalDependencies) + ../Release + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj.filters b/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj.filters new file mode 100644 index 000000000..974ba5b96 --- /dev/null +++ b/VolkovAA/lab3/build/lab3/generate_graph/generate_graph.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + \ No newline at end of file diff --git a/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj b/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj new file mode 100644 index 000000000..6954820c1 --- /dev/null +++ b/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj @@ -0,0 +1,146 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8} + Win32Proj + graph_lib + 8.1 + + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + ../../../include + + + Windows + + + + + + + Level3 + Disabled + _DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + ../../../include + + + Windows + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj.filters b/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj.filters new file mode 100644 index 000000000..205e1b71b --- /dev/null +++ b/VolkovAA/lab3/build/lab3/graph_lib/graph_lib.vcxproj.filters @@ -0,0 +1,41 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/VolkovAA/lab3/build/lab3/lab3.sln b/VolkovAA/lab3/build/lab3/lab3.sln new file mode 100644 index 000000000..7e8eb9fbe --- /dev/null +++ b/VolkovAA/lab3/build/lab3/lab3.sln @@ -0,0 +1,54 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab3", "lab3\lab3.vcxproj", "{3611F5EE-B3FD-4484-A7E2-3B2B91F25284}" + ProjectSection(ProjectDependencies) = postProject + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8} = {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "graph_lib", "graph_lib\graph_lib.vcxproj", "{AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "generate_graph", "generate_graph\generate_graph.vcxproj", "{07D8BD44-E0DD-4B88-9BFF-83757A2BE323}" + ProjectSection(ProjectDependencies) = postProject + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8} = {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8} + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Debug|x64.ActiveCfg = Debug|x64 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Debug|x64.Build.0 = Debug|x64 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Debug|x86.ActiveCfg = Debug|Win32 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Debug|x86.Build.0 = Debug|Win32 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Release|x64.ActiveCfg = Release|x64 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Release|x64.Build.0 = Release|x64 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Release|x86.ActiveCfg = Release|Win32 + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284}.Release|x86.Build.0 = Release|Win32 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Debug|x64.ActiveCfg = Debug|x64 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Debug|x64.Build.0 = Debug|x64 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Debug|x86.ActiveCfg = Debug|Win32 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Debug|x86.Build.0 = Debug|Win32 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Release|x64.ActiveCfg = Release|x64 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Release|x64.Build.0 = Release|x64 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Release|x86.ActiveCfg = Release|Win32 + {AAC63DB2-CE7F-45B9-B5A0-1D0F57F595E8}.Release|x86.Build.0 = Release|Win32 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Debug|x64.ActiveCfg = Debug|x64 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Debug|x64.Build.0 = Debug|x64 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Debug|x86.ActiveCfg = Debug|Win32 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Debug|x86.Build.0 = Debug|Win32 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Release|x64.ActiveCfg = Release|x64 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Release|x64.Build.0 = Release|x64 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Release|x86.ActiveCfg = Release|Win32 + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj b/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj new file mode 100644 index 000000000..57958c95d --- /dev/null +++ b/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + {3611F5EE-B3FD-4484-A7E2-3B2B91F25284} + Win32Proj + lab3 + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + graph_lib.lib;%(AdditionalDependencies) + ../Debug + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + true + true + graph_lib.lib;%(AdditionalDependencies) + ../Release + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj.filters b/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj.filters new file mode 100644 index 000000000..0e0bed61a --- /dev/null +++ b/VolkovAA/lab3/build/lab3/lab3/lab3.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + \ No newline at end of file diff --git a/VolkovAA/lab3/include/Deijkstra.h b/VolkovAA/lab3/include/Deijkstra.h new file mode 100644 index 000000000..9d81862d7 --- /dev/null +++ b/VolkovAA/lab3/include/Deijkstra.h @@ -0,0 +1,16 @@ +#pragma once +#include "Graph.h" +#include "d-heap.h" +#include "priority_queue.h" +#include + +class DataFloat : public Data { +public: + DataFloat(int v, float dist); + int v; +}; + +class Dijkstra { +public: + static void dijkstra(Graph *&graph, int s, float *&distance, int *&up); +}; \ No newline at end of file diff --git a/VolkovAA/lab3/include/Graph.h b/VolkovAA/lab3/include/Graph.h new file mode 100644 index 000000000..d856f3fe7 --- /dev/null +++ b/VolkovAA/lab3/include/Graph.h @@ -0,0 +1,47 @@ +#pragma once +#include +#include +#include + +#define maxVerticesSize 1000 + +class Edge { +public: + int N; + int K; + float weight; + + Edge(int N, int K, float weight); +}; + +class Graph { +private: + int n; + int m; + int m_cur; + Edge** edges; + int* vertices; +public: + Graph(int n); + Graph(int n, int m); + ~Graph(); + + void generateGraph(float minRange, float maxRange); + void addEdge(int N, int K, float weight); + void delEdge(int N, int K); + + int getVerticesNum(); + int getEdgeSize(); + int getRealSize(); + Edge** getEdgeSet(); + Edge* getEdge(int j); + float getWeight(int N, int K); + + void printList(); + +private: + void generateVertices(int &N, int &K); + float generateWeight(float minRange, float maxRange); + void cleaner(); + int findEdge(int N, int K); +}; diff --git a/VolkovAA/lab3/include/d-heap.h b/VolkovAA/lab3/include/d-heap.h new file mode 100644 index 000000000..d3b36c82a --- /dev/null +++ b/VolkovAA/lab3/include/d-heap.h @@ -0,0 +1,42 @@ +#pragma once +#include +#include +#include "Graph.h" + +#define maxSize 1000 +typedef int dataType; + +class Data { +public: + float priorities; +}; + +class DAryHeap { +protected: + Data **keys; + int d; + int lastIdx; +public: + DAryHeap(int d); + DAryHeap(const DAryHeap &heap); + ~DAryHeap(); + + void add(Data *&key); + void addSet(Data **key, int num); + Data* erase(); + Data* erase(int i); + + void transposition(int i, int j); + void surfacing(int i); + void immersion(int i); + + void spudding(); + + int isFull(); + int isEmpty(); + +private: + int minChild(int i); +}; + + diff --git a/VolkovAA/lab3/include/priority_queue.h b/VolkovAA/lab3/include/priority_queue.h new file mode 100644 index 000000000..db16c5a2a --- /dev/null +++ b/VolkovAA/lab3/include/priority_queue.h @@ -0,0 +1,47 @@ +#pragma once + +#include "d-heap.h" + +enum QueueID { + HEAP = 0, +}; + +class PriorityQueue { +public: + PriorityQueue() {}; + + virtual void push(Data *&key) = 0; + virtual Data* pop() = 0; + virtual void refresh() = 0; + + virtual int isFull() = 0; + virtual int isEmpty() = 0; +}; + +class PriorityQueueHeap : public PriorityQueue { +protected: + DAryHeap *heap; +public: + PriorityQueueHeap(int d = 4); + PriorityQueueHeap(const PriorityQueueHeap &queue); + PriorityQueueHeap(Data **keys, int num, int d = 4); + ~PriorityQueueHeap(); + + virtual void push(Data *&key); + virtual Data* pop(); + virtual void refresh(); + + virtual int isFull(); + virtual int isEmpty(); +}; +class QueueFactory { +public: + static PriorityQueue* createQueue(QueueID qid) + { + PriorityQueue *queue; + + queue = new PriorityQueueHeap(); + + return queue; + } +}; \ No newline at end of file diff --git a/VolkovAA/lab3/samples/generate_graph.vcxproj b/VolkovAA/lab3/samples/generate_graph.vcxproj new file mode 100644 index 000000000..cef8bf5eb --- /dev/null +++ b/VolkovAA/lab3/samples/generate_graph.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + {07D8BD44-E0DD-4B88-9BFF-83757A2BE323} + Win32Proj + generate_graph + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + graph_lib.lib;%(AdditionalDependencies) + ../Debug + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + ../../../include + + + Console + true + true + true + graph_lib.lib;%(AdditionalDependencies) + ../Release + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab3/src/Dijkstra.cpp b/VolkovAA/lab3/src/Dijkstra.cpp new file mode 100644 index 000000000..7a57dfe29 --- /dev/null +++ b/VolkovAA/lab3/src/Dijkstra.cpp @@ -0,0 +1,63 @@ +#include "Deijkstra.h" + +DataFloat::DataFloat(int v, float dist) +{ + this->v = v; + priorities = dist; +} + +void Dijkstra::dijkstra(Graph *&graph, int s, float *&distance, int *&up) +{ + int n = graph->getVerticesNum(); + int m = graph->getRealSize(); + if ((s < 0) || (s >= n)) + throw "Dijkstra: Invalid start vertex!"; + + Data** dist = new Data*[n]; + up = new int[n]; + + PriorityQueue *queue = QueueFactory::createQueue(static_cast(0)); + + for (int i = 0; i < n; i++) { + up[i] = i; + dist[i] = new DataFloat(i, FLT_MAX); + if (i == s) + dist[s]->priorities = 0; + queue->push(dist[i]); + } + + Edge** edges = graph->getEdgeSet(); + while (!queue->isEmpty()) + { + int vConsidered = ((DataFloat*)queue->pop())->v; + float delta; + + for (int i = 0; i < m; i++) + { + int vIncident = -1; + if (edges[i]->K == vConsidered) + vIncident = edges[i]->N; + if (edges[i]->N == vConsidered) + vIncident = edges[i]->K; + if (vIncident == -1) continue; + + float way = dist[vConsidered]->priorities + graph->getWeight(vConsidered, vIncident); + delta = dist[vIncident]->priorities - way; + if (delta > 0) + { + dist[vIncident]->priorities = way; //ïîñêîëüêó â î÷åðåäè ëåæàò óêàçàòåëè íà îáúåêòû dist[i], + up[vIncident] = vConsidered; //òî çíà÷åíèÿ â î÷åðåäè àâòîìàòè÷åñêè èçìåíÿòñÿ. + queue->refresh(); + } + } + } + + distance = new float[n]; + for (int i = 0; i < n; i++) + distance[i] = dist[i]->priorities; + + for (int i = 0; i < n; i++) + delete dist[i]; + delete[]dist; + delete queue; +} \ No newline at end of file diff --git a/VolkovAA/lab3/src/d-heap.cpp b/VolkovAA/lab3/src/d-heap.cpp new file mode 100644 index 000000000..d96749fa2 --- /dev/null +++ b/VolkovAA/lab3/src/d-heap.cpp @@ -0,0 +1,146 @@ +#include "d-heap.h" + +DAryHeap::DAryHeap(int d) +{ + if (d <= 0) + throw "DAryHeap: Invalid d"; + this->d = d; + lastIdx = -1; + keys = new Data*[maxSize]; +} + +DAryHeap::DAryHeap(const DAryHeap &heap) +{ + this->d = heap.d; + keys = new Data*[maxSize]; + for (int i = 0; i <= heap.lastIdx; i++) + keys[i] = heap.keys[i]; + this->lastIdx = heap.lastIdx; +} + +DAryHeap::~DAryHeap() +{ + delete[] keys; +} + +void DAryHeap::add(Data *&key) +{ + if (isFull()) + throw "DAryHeap: No memory"; + lastIdx++; + keys[lastIdx] = key; + surfacing(lastIdx); +} + +void DAryHeap::addSet(Data **key, int num) +{ + if (lastIdx + num >= maxSize) + throw "DAryHeap: Too large set!"; + for (int i = lastIdx + 1; i < lastIdx + num + 1; i++) + { + keys[i] = key[i - lastIdx - 1]; + } + lastIdx += num; + spudding(); +} + +Data* DAryHeap::erase() +{ + if (isEmpty()) + throw "DAryHeap: No data!"; + Data* key = keys[lastIdx]; + lastIdx--; + return key; +} + +Data* DAryHeap::erase(int i) +{ + if (isEmpty()) + throw "DAryHeap: No data!"; + if ((i < 0) || (i > lastIdx)) + throw "DAryHeap: Invalid index"; + + Data* key = keys[i]; + if (i == lastIdx) + { + lastIdx--; + return key; + } + transposition(i, lastIdx); + lastIdx--; + immersion(i); + return key; +} + +void DAryHeap::transposition(int i, int j) +{ + if ((i < 0) || (j < 0) || (i > lastIdx) || (j > lastIdx)) + throw "DAryHeap: Invalid indexes"; + Data* tmp = keys[i]; + keys[i] = keys[j]; + keys[j] = tmp; +} + +void DAryHeap::surfacing(int i) +{ + if ((i < 0) || (i > lastIdx)) + throw "DAryHeap: Invalid index"; + + int p = (i - 1) / d; + while (i > 0) { + if (keys[p]->priorities < keys[i]->priorities) + break; + transposition(p, i); + i = p; + p = (i - 1) / d; + } +} + +void DAryHeap::immersion(int i) +{ + if ((i < 0) || (i > lastIdx)) + throw "DAryHeap: Invalid index"; + + int c = minChild(i); + while ((c != -1) && (keys[c]->priorities < keys[i]->priorities)) { + transposition(i, c); + i = c; + c = minChild(i); + } +} + +void DAryHeap::spudding() +{ + for (int i = lastIdx; i >= 0; i--) + immersion(i); +} + +int DAryHeap::isFull() +{ + return lastIdx >= maxSize - 1; +} + +int DAryHeap::isEmpty() +{ + return lastIdx == -1; +} + +int DAryHeap::minChild(int i) +{ + int f = i*d + 1; + if (f > lastIdx) + return -1; + + int l = std::min(i*d + d, lastIdx); + int c; + + Data* minKey = keys[f]; + c = f; + for (int k = f + 1; k <= l; k++) { + if (minKey->priorities > keys[k]->priorities) { + minKey = keys[k]; + c = k; + } + } + return c; +} \ No newline at end of file diff --git a/VolkovAA/lab3/src/graph.cpp b/VolkovAA/lab3/src/graph.cpp new file mode 100644 index 000000000..ee47f810a --- /dev/null +++ b/VolkovAA/lab3/src/graph.cpp @@ -0,0 +1,175 @@ +#include "Graph.h" + +Edge::Edge(int N, int K, float weight) +{ + this->N = N; + this->K = K; + this->weight = weight; +} + +Graph::Graph(int n) +{ + if ((n < 0) || (n > maxVerticesSize)) + throw "Graph: Invalid namber of vertices"; + else + this->n = n; + this->m = n*(n - 1) / 2; + m_cur = 0; + vertices = new int[n]; + edges = new Edge*[m]; +} + +Graph::Graph(int n, int m) +{ + if ((n < 0) || (n > maxVerticesSize)) + throw "Graph: Invalid namber of vertices"; + else + this->n = n; + + if ((m < 0) || (m > n*(n - 1) / 2)) + throw "Graph: Invalid namber of edges"; + else + this->m = m; + + m_cur = 0; + + vertices = new int[n]; + edges = new Edge*[m]; +} + +Graph::~Graph() +{ + cleaner(); + delete[] edges; + delete[]vertices; +} + +int Graph::findEdge(int N, int K) +{ + for (int j = 0; j < m_cur; j++) + if ((edges[j]->K == K) && + (edges[j]->N == N) || + (edges[j]->N == K) && + (edges[j]->K == N)) + return j; + return -1; +} + +void Graph::generateVertices(int &N, int &K) +{ + do { + N = rand() % n; + K = rand() % n; + } while ((N == K) || (findEdge(N, K) != -1)); +} + +float Graph::generateWeight(float minRange, float maxRange) +{ + double d = minRange; + double c = (double)(maxRange - minRange) / RAND_MAX; + double result = c * rand() + d; + return result; +} + +void Graph::cleaner() +{ + for (int i = 0; i < m_cur; i++) + delete edges[i]; +} + +void Graph::generateGraph(float minRange, float maxRange) +{ + int N; + int K; + float weight; + + if (minRange > maxRange) + throw "Graph: Invalid ranges"; + + if (m_cur) { + cleaner(); + m_cur = 0; + } + + srand(time(NULL)); + for (int i = 0; i < m; i++) { + generateVertices(N, K); + weight = generateWeight(minRange, maxRange); + edges[i] = new Edge(N, K, weight); + m_cur++; + } +} + +void Graph::addEdge(int N, int K, float weight) +{ + if (m_cur == m) + throw "Graph: Graph is full"; + if (N == K) + throw "Graph: Loops are disabled"; + if (findEdge(N, K) != -1) + throw "Graph: multiple edges arent allowed!"; + edges[m_cur] = new Edge(N, K, weight); + m_cur++; +} + +void Graph::delEdge(int N, int K) +{ + int j = findEdge(N, K); + if (j == -1) + throw "Graph: try delete non-existent edge"; + delete edges[j]; + edges[j] = edges[m_cur - 1]; + m_cur--; +} + +int Graph::getVerticesNum() +{ + return n; +} + +int Graph::getEdgeSize() +{ + return m; +} + +int Graph::getRealSize() +{ + return m_cur; +} + +Edge** Graph::getEdgeSet() +{ + if (m_cur == 0) + return 0; + return edges; +} + +Edge* Graph::getEdge(int j) +{ + return edges[j]; +} + +float Graph::getWeight(int N, int K) +{ + int j = findEdge(N, K); + if (j == -1) + throw "Graph: Invalid edge!"; + return edges[j]->weight; +} + +void Graph::printList() +{ + using namespace std; + for (int i = 0; i < n; i++) + { + cout << i << " : "; + for (int j = 0; j < m_cur; j++) + { + if (edges[j]->N == i) + cout << edges[j]->K << '(' << edges[j]->weight << ')' << ", "; + if (edges[j]->K == i) + cout << edges[j]->N << '(' << edges[j]->weight << ')' << ", "; + } + cout << endl; + } +} \ No newline at end of file diff --git a/VolkovAA/lab3/src/priority_queue_heap.cpp b/VolkovAA/lab3/src/priority_queue_heap.cpp new file mode 100644 index 000000000..eb4185e03 --- /dev/null +++ b/VolkovAA/lab3/src/priority_queue_heap.cpp @@ -0,0 +1,47 @@ +#include "priority_queue.h" + +PriorityQueueHeap::PriorityQueueHeap(int d) +{ + heap = new DAryHeap(d); +} + +PriorityQueueHeap::PriorityQueueHeap(const PriorityQueueHeap &queue) +{ + this->heap = new DAryHeap(*(queue.heap)); +} + +PriorityQueueHeap::PriorityQueueHeap(Data **keys, int num, int d) +{ + heap = new DAryHeap(d); + heap->addSet(keys, num); +} + +PriorityQueueHeap::~PriorityQueueHeap() +{ + delete heap; +} + +void PriorityQueueHeap::push(Data *&key) +{ + heap->add(key); +} + +Data* PriorityQueueHeap::pop() +{ + return heap->erase(0); +} + +void PriorityQueueHeap::refresh() +{ + heap->spudding(); +} + +int PriorityQueueHeap::isFull() +{ + return heap->isFull(); +} + +int PriorityQueueHeap::isEmpty() +{ + return heap->isEmpty(); +} diff --git a/VolkovAA/lab4/DOC/OtchyotLR1.docx b/VolkovAA/lab4/DOC/OtchyotLR1.docx new file mode 100644 index 000000000..f3107b641 Binary files /dev/null and b/VolkovAA/lab4/DOC/OtchyotLR1.docx differ diff --git a/VolkovAA/lab4/build/Lab4/lab4_postfix.sln b/VolkovAA/lab4/build/Lab4/lab4_postfix.sln new file mode 100644 index 000000000..c406d130d --- /dev/null +++ b/VolkovAA/lab4/build/Lab4/lab4_postfix.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "postfix_lib", "lab4_postfix\lab4_postfix.vcxproj", "{7196E28F-DCEE-4542-B581-E4EA6CA7AE11}" + ProjectSection(ProjectDependencies) = postProject + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A} = {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample_postfix", "sample_postfix\sample_postfix.vcxproj", "{04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Debug|x64.ActiveCfg = Debug|x64 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Debug|x64.Build.0 = Debug|x64 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Debug|x86.ActiveCfg = Debug|Win32 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Debug|x86.Build.0 = Debug|Win32 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Release|x64.ActiveCfg = Release|x64 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Release|x64.Build.0 = Release|x64 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Release|x86.ActiveCfg = Release|Win32 + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11}.Release|x86.Build.0 = Release|Win32 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Debug|x64.ActiveCfg = Debug|x64 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Debug|x64.Build.0 = Debug|x64 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Debug|x86.ActiveCfg = Debug|Win32 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Debug|x86.Build.0 = Debug|Win32 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Release|x64.ActiveCfg = Release|x64 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Release|x64.Build.0 = Release|x64 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Release|x86.ActiveCfg = Release|Win32 + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj b/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj new file mode 100644 index 000000000..e0b7b13db --- /dev/null +++ b/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj @@ -0,0 +1,154 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {7196E28F-DCEE-4542-B581-E4EA6CA7AE11} + Win32Proj + lab4_postfix + 8.1 + postfix_lib + + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + StaticLibrary + true + v140 + Unicode + + + StaticLibrary + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Windows + + + + + + + Level3 + Disabled + _DEBUG;_LIB;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Windows + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Windows + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Windows + true + true + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj.filters b/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj.filters new file mode 100644 index 000000000..b4eca98dc --- /dev/null +++ b/VolkovAA/lab4/build/Lab4/lab4_postfix/lab4_postfix.vcxproj.filters @@ -0,0 +1,50 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + + + + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj b/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj new file mode 100644 index 000000000..f4429400d --- /dev/null +++ b/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj @@ -0,0 +1,162 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {04E8ACA0-AA0A-4312-9B29-1BDC7F3B534A} + Win32Proj + sample_postfix + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Console + true + postfix_lib.lib;%(AdditionalDependencies) + ..\Debug + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Console + true + ..\Debug + postfix_lib.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Console + true + true + true + postfix_lib.lib;%(AdditionalDependencies) + ..\Release + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + C:\tmp\mp2-practice\VolkovAA\lab4\include + + + Console + true + true + true + ..\Release + postfix_lib.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj.filters b/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj.filters new file mode 100644 index 000000000..4ae06ba2d --- /dev/null +++ b/VolkovAA/lab4/build/Lab4/sample_postfix/sample_postfix.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Source Files + + + \ No newline at end of file diff --git a/VolkovAA/lab4/include/fun.h b/VolkovAA/lab4/include/fun.h new file mode 100644 index 000000000..2d7eb7479 --- /dev/null +++ b/VolkovAA/lab4/include/fun.h @@ -0,0 +1,27 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +#include +#include + +#include "list.h" +#include "nods.h" +#include "Stack.h" +#include "val.h" + +using namespace std; + +class functions +{ +private: + static int Priority(char x); +public: + static NameValue* NVmas(string op); + static string postfix(string line); + static string operands(string line); + static float Result(string line, int sizeOfTotalExpression, NameValue* NV); + +}; + + +#endif \ No newline at end of file diff --git a/VolkovAA/lab4/include/list.h b/VolkovAA/lab4/include/list.h new file mode 100644 index 000000000..11e7c3ff6 --- /dev/null +++ b/VolkovAA/lab4/include/list.h @@ -0,0 +1,185 @@ +#ifndef LIST_H +#define LIST_H + +#include "nods.h" + +template +class List +{ +private: + Node *root; +public: + List(); + List(const List&); + ~List(); + + void Add(T); + void Remove(T); + void InsertBefore(T, T); + void InsertAfter(T, T); + void InsertEnd(T); + void print() const; + int Size() const; + T GetRoot()const; + Node *Search(T); +}; + +template +List::List() +{ + root = 0; +}; + +template +List::List(const List&L) +{ + L->pNext=this->root; + this->root = L; +}; + +template +List::~List() +{ + + while (root != 0) + { + Node* tmp = root; + root = root->pNext; + delete tmp; + } + delete root; +}; + +template +void List::Add(T key) +{ + Node * a = new Node; + a->key = key; + a->pNext = root; + root = a; +}; + +template +void List::Remove(T K) +{ + if (root == 0) + throw "List is empty"; + if (this->root->key == K) + { + Node*n = root; + root = root->pNext; + delete n; + return; + } + Node*tmp = root; + while (tmp -> pNext != 0 && tmp->pNext->key != K) + { + tmp = tmp->pNext; + } + if (tmp->pNext == 0) + throw "Key not found"; + Node*n = tmp->pNext; + tmp->pNext = n->pNext; +}; + +template +void List::InsertBefore(T K1, T K2) +{ + if (root == 0) throw "Is Empty"; + if (root->key == K1) + { + root = new Node; + root->key = K1; + + } +}; + +template +void List::InsertAfter(T K1, T K2) +{ + if (root == 0) throw "List is empty"; + while (root->pNext != 0 && root->key != K2) + { + root = root->pNext; + } + if (root->pNext == 0 && root->key != K2) + { + throw "Key not found"; + } + Node*a = new Node; + a->key = K1; + a->pNext = root -> pNext; + root->pNext = a; +}; + +template +void List::InsertEnd(T K) +{ + if (root == 0) + { + root = new Node; + root->key = K; + root->pNext = 0; + return; + } + while (root->pNext != 0) + { + root = root->pNext; + } + root->pNext = new Node; + root->pNext->key = K; + root->pNext -> pNext = 0; +}; + +template +int List::Size() const +{ + Node* a = root; + int i = 0; + while (a != 0) + { + i++; + a = a->pNext; + } + return i; +}; + +template +Node* List::Search(T K) +{ + Node* a = root; + while (a!= 0 && a->key!=K) + { + a = a->pNext; + } + if (a == 0) throw "Key not found"; + return a; +}; + +template +T List::GetRoot()const +{ + if (root!=0) + return root->key; + else + return '!'; + +} + +template +void List::print() const +{ + if (root == 0) + { + throw "Node is empty"; + } + Node* a = root; + while (a != 0) + { + cout << a->key << " "; + a = a->pNext; + } + cout << endl; +}; + +#endif \ No newline at end of file diff --git a/VolkovAA/lab4/include/nods.h b/VolkovAA/lab4/include/nods.h new file mode 100644 index 000000000..64eb3161c --- /dev/null +++ b/VolkovAA/lab4/include/nods.h @@ -0,0 +1,13 @@ +#ifndef NODS_H +#define NODS_H + +template +class Node +{ +public: + T key; + Node *pNext; + +}; + +#endif diff --git a/VolkovAA/lab4/include/stack.h b/VolkovAA/lab4/include/stack.h new file mode 100644 index 000000000..df7c9aaa3 --- /dev/null +++ b/VolkovAA/lab4/include/stack.h @@ -0,0 +1,89 @@ +#ifndef STACK_H +#define STACK_H +#include "list.h" + +template +class Stack +{ +private: + List* list; +public: + Stack(); + Stack(const Stack&); + ~Stack(); + + void Push(T); + T Pop(); + T Take() const; + bool IsEmpty() const; + bool IsFull() const; +}; + +template +Stack::Stack() +{ + list = new List; +}; + +template +Stack::Stack(const Stack&S) +{ + list = new List(S.list); +}; + +template +Stack::~Stack() +{ + delete list; +}; + +template +void Stack::Push(T key) +{ + if (IsFull()) throw "Is Full"; + list->Add(key); +}; + +template +T Stack::Take()const +{ + + return list->GetRoot(); +}; + +template +T Stack::Pop() +{ + if (IsEmpty()) throw "Is Empty"; + T key = list->GetRoot(); + try { + list->Remove(key); + } + catch (const char* ex) + { + cout << ex << endl; + } + return key; +}; + +template +bool Stack::IsEmpty() const +{ + return(list->Size() == 0); +}; +template +bool Stack::IsFull() const +{ + T key = -1; + try { + list -> Add(key); + list->Remove(key); + } + catch (const char* ex) + { + return true; + } + return false; +}; + +#endif \ No newline at end of file diff --git a/VolkovAA/lab4/include/val.h b/VolkovAA/lab4/include/val.h new file mode 100644 index 000000000..7002c2e76 --- /dev/null +++ b/VolkovAA/lab4/include/val.h @@ -0,0 +1,10 @@ +#ifndef NameVal_H +#define NameVal_H + +struct NameValue +{ + char Operand; + float Value; +}; + +#endif \ No newline at end of file diff --git a/VolkovAA/lab4/sample/Main.cpp b/VolkovAA/lab4/sample/Main.cpp new file mode 100644 index 000000000..fb6265a43 --- /dev/null +++ b/VolkovAA/lab4/sample/Main.cpp @@ -0,0 +1,51 @@ +#include +#include + +#include "list.h" +#include "nods.h" +#include "stack.h" +#include "val.h" +#include "fun.h" + +using namespace std; + +void main() +{ + cout <<"Enter an expression"<< endl; + string line; + cin >> line; + + string postfix; + try + { + postfix=functions::postfix(line); + } + catch( const char* ex) + { + cout< +#include + +#include "List.h" +#include "nods.h" +#include "stack.h" +#include "val.h" + +using namespace std; + +int functions::Priority(char sym) +{ + if ((sym == '*') || (sym == '/')) { + return 3; + } + if ((sym == '+') || (sym == '-')) { + return 2; + } + if (sym == ')') { + return 1; + } + if (sym == '(') { + return 0; + } + return -1; +} +string functions::postfix(string line) +{ + Stack S1; + Stack S2; + + for (int i = 0; i < (int)line.length(); i++) + { + if (((line[i] >= 'a') && (line[i] <= 'z')) || ((line[i] >= 'A') && (line[i] <= 'Z'))) + S1.Push(line[i]); + else if (line[i] == ')') + { + while ((!S2.IsEmpty()) && (S2.Take() != '(')) + { + S1.Push(S2.Pop()); + + } + if (!S2.IsEmpty() && (S2.Take() == '(')) + S2.Pop(); + else + throw "S2 is empty"; + } + else if (line[i] == '(') + S2.Push(line[i]); + else if ((line[i] == '+') || (line[i] == '-') || (line[i] == '/') || (line[i] == '*')) + { + if (Priority(S2.Take()) <= Priority(line[i]) || S2.Take() == '(') + S2.Push(line[i]); + else + { + while ((!S2.IsEmpty()) && (Priority(S2.Take()) >= Priority(line[i]))) + { + S1.Push(S2.Pop()); + } + S2.Push(line[i]); + } + } + } + while (!S2.IsEmpty()) + { + if((S2.Take()!=')')&&(S2.Take()!='(')) + S1.Push(S2.Pop()); + else + S2.Pop(); + } + + string postfix = ""; + while (!S1.IsEmpty()) + { + postfix = S1.Pop() + postfix; + } + return postfix; +} + +string functions::operands(string line) +{ + int n=0; + for(int i=0;i<(int)line.length();i++) + { + if(((line[i]>='A')&&(line[i]<='Z'))|| ((line[i] >= 'a') && (line[i] <= 'z'))) + n++; + } + char* arr=new char[n]; + int j=0; + for(int i=0;i<(int)line.length();i++) + { + if(((line[i]>='A')&&(line[i]<='Z'))|| ((line[i] >= 'a') && (line[i] <= 'z'))) + { + arr[j]=line[i]; + j++; + } + } + int k=1; + for( int i=1;i> NV[i].Value; + NV[i].Operand = op[i]; + cout << endl; + } + return NV; +} +float functions::Result(string line, int sizeOfTotalExpression, NameValue* NV) +{ + Stack S1; + + for (int i = 0; i < sizeOfTotalExpression; i++) + { + + int j = 0; + if (((line[i] >= 'A') && (line[i] <= 'Z')) || ((line[i] >= 'a') && (line[i] <= 'z'))) + { + while (NV[j].Operand != line[i]) + j++; + S1.Push(NV[j].Value); + } + float a, b; + if (line[i] == '+') + { + a = S1.Pop(); + b = S1.Pop(); + S1.Push(b + a); + } + if (line[i] == '-') + { + a = S1.Pop(); + b = S1.Pop(); + S1.Push(b - a); + } + if (line[i] == '*') + { + a = S1.Pop(); + b = S1.Pop(); + S1.Push(b * a); + } + if (line[i] == '/') + { + a = S1.Pop(); + b = S1.Pop(); + if(a!=0) + S1.Push(b / a); + else + throw "Error!\nYou can not divide by zero."; + } + + } + return S1.Pop(); +} \ No newline at end of file diff --git a/VolkovAA/lab4/src/val.cpp b/VolkovAA/lab4/src/val.cpp new file mode 100644 index 000000000..4c7ebfccd --- /dev/null +++ b/VolkovAA/lab4/src/val.cpp @@ -0,0 +1 @@ +#include "val.h" \ No newline at end of file