diff --git a/KaganovDA/lab1/build/lab1_postfix/lab1_postfix.sln b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix.sln new file mode 100644 index 000000000..54752393f --- /dev/null +++ b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix.sln @@ -0,0 +1,44 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab1_postfix", "lab1_postfix\lab1_postfix.vcxproj", "{95F7E418-95C2-4635-BC46-D40F28AD12E9}" + ProjectSection(ProjectDependencies) = postProject + {B5706D27-C05E-4ACB-97DD-E035B8100D27} = {B5706D27-C05E-4ACB-97DD-E035B8100D27} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "postfix_lib", "postfix_lib\postfix_lib.vcxproj", "{B5706D27-C05E-4ACB-97DD-E035B8100D27}" +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 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Debug|x64.ActiveCfg = Debug|x64 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Debug|x64.Build.0 = Debug|x64 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Debug|x86.ActiveCfg = Debug|Win32 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Debug|x86.Build.0 = Debug|Win32 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Release|x64.ActiveCfg = Release|x64 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Release|x64.Build.0 = Release|x64 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Release|x86.ActiveCfg = Release|Win32 + {95F7E418-95C2-4635-BC46-D40F28AD12E9}.Release|x86.Build.0 = Release|Win32 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Debug|x64.ActiveCfg = Debug|x64 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Debug|x64.Build.0 = Debug|x64 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Debug|x86.ActiveCfg = Debug|Win32 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Debug|x86.Build.0 = Debug|Win32 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Release|x64.ActiveCfg = Release|x64 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Release|x64.Build.0 = Release|x64 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Release|x86.ActiveCfg = Release|Win32 + {B5706D27-C05E-4ACB-97DD-E035B8100D27}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F7CEAC78-A731-4BDD-9E9D-DAAB4F19BE3D} + EndGlobalSection +EndGlobal diff --git a/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj new file mode 100644 index 000000000..b7d277aa4 --- /dev/null +++ b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj @@ -0,0 +1,161 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 15.0 + {95F7E418-95C2-4635-BC46-D40F28AD12E9} + Win32Proj + lab1postfix + 10.0.16299.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Console + true + postfix_lib.lib;%(AdditionalDependencies) + ..\Debug + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Console + true + true + true + postfix_lib.lib;%(AdditionalDependencies) + ..\Release + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj.filters b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj.filters new file mode 100644 index 000000000..e85f300f4 --- /dev/null +++ b/KaganovDA/lab1/build/lab1_postfix/lab1_postfix/lab1_postfix.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_lib.vcxproj b/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_lib.vcxproj new file mode 100644 index 000000000..82515f860 --- /dev/null +++ b/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_lib.vcxproj @@ -0,0 +1,168 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + + 15.0 + {B5706D27-C05E-4ACB-97DD-E035B8100D27} + Win32Proj + postfixlib + 10.0.16299.0 + + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + + Windows + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + + Windows + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + \ No newline at end of file diff --git a/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_lib.vcxproj.filters b/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_lib.vcxproj.filters new file mode 100644 index 000000000..30a2eb777 --- /dev/null +++ b/KaganovDA/lab1/build/lab1_postfix/postfix_lib/postfix_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;ipp;xsd + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/KaganovDA/lab1/doc/Postfix_one.pdf b/KaganovDA/lab1/doc/Postfix_one.pdf new file mode 100644 index 000000000..a2b4efc95 Binary files /dev/null and b/KaganovDA/lab1/doc/Postfix_one.pdf differ diff --git a/KaganovDA/lab1/include/List.h b/KaganovDA/lab1/include/List.h new file mode 100644 index 000000000..1730435ea --- /dev/null +++ b/KaganovDA/lab1/include/List.h @@ -0,0 +1,154 @@ +#ifndef POLISH_LIST_H +#define POLISH_LIST_H + +#include "Node.h" + +template + +class List { +private: + Node* root; +public: + List(); + List(const List&); + ~List(); + void Add(TKey); // + void Remove(TKey); // + void InsertBefore(TKey, TKey); // + void InsertAfter(TKey, TKey); // + void InsertEnd(TKey); // + void Print()const; // + int Size()const; // + TKey GetRoot()const; + Node* Search(TKey); // +}; + +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(TKey key) { + Node* a = new Node; + a->key = key; + a->pNext = root; + root = a; +}; + +template +void List::Remove(TKey key) { + if (root == 0) + throw "List Is Empty"; + if (this->root->key == key) { + Node* a = root; + root = root->pNext; + delete a; + return; + } + Node* tmp = root; + while (tmp->pNext != 0 && tmp->pNext->key != key) + tmp = tmp->pNext; + if (tmp->pNext == 0) + throw "Key Not Found"; + Node* a = tmp->pNext; + tmp->pNext = a->pNext; +}; + +template +void List::InsertBefore(TKey key, TKey KeyNext) { + if (root == 0) + throw "List Is Empty"; + if (root->key == key) { + root = new Node; + root->key = key; + } +}; + +template +void List::InsertAfter(TKey key, TKey KeyPrev) { + if (root == 0) + throw "List Is Empty"; + while (root->pNext != 0 && root->key != KeyPrev) + root = root->pNext; + if (root->pNext == 0 && root->key != KeyPrev) + throw "Key Not Found"; + Node* a = new Node; + a->key = key; + a->pNext = root->pNext; + root->pNext = a; +}; + +template +void List::InsertEnd(TKey key) { + if (root == 0) { + root = new Node; + root->key = key; + root->pNext = 0; + return; + } + while (root->pNext != 0) + root = root->pNext; + root->pNext = new Node; + root->pNext->key = key; + root->pNext->pNext = 0; +}; + +template +void List::Print()const { + if (root == 0) + throw "Stack Is Empty"; + Node* a = root; + while (a != 0) { + cout << a->key << " "; + a = a->pNext; + } + cout << endl; +}; + +template +int List::Size()const { + Node* a = root; + int i = 0; + while (a != 0) { + i++; + a = a->pNext; + } + return i; +}; + +template +TKey List::GetRoot()const { + if (root != 0) + return root->key; + else + return 1; +} + +template +Node* List::Search(TKey key) { + Node* a = root; + while (a != 0 && a->key != key) + a = a->pNext; + if (a == 0) + throw "Key Not Found"; + return a; +}; + +#endif \ No newline at end of file diff --git a/KaganovDA/lab1/include/Node.h b/KaganovDA/lab1/include/Node.h new file mode 100644 index 000000000..08262851a --- /dev/null +++ b/KaganovDA/lab1/include/Node.h @@ -0,0 +1,11 @@ +#ifndef POLISH_NODE_H +#define POLISH_NODE_H + +template + +struct Node { + TKey key; + Node* pNext; +}; + +#endif \ No newline at end of file diff --git a/KaganovDA/lab1/include/Stack.h b/KaganovDA/lab1/include/Stack.h new file mode 100644 index 000000000..c07c2f6f7 --- /dev/null +++ b/KaganovDA/lab1/include/Stack.h @@ -0,0 +1,85 @@ +#ifndef POLISH_STACK_H +#define POLISH_STACK_H + +#include "List.h" +#include +using namespace std; + +template + +class Stack { +private: + List* list; +public: + Stack(); + Stack(const Stack&); + ~Stack(); + void Push(TKey); // + TKey Pop(); // + TKey Take()const; // + bool IsEmpty()const; // + bool IsFull()const; // +}; + +template +Stack::Stack() { + list = new List; +}; + +template +Stack::~Stack() { + delete list; +}; + +template +Stack::Stack(const Stack& S) { + list = new List(S.list); +}; + +template +void Stack::Push(TKey key) { + if (IsFull()) + throw "Stack Is Full"; + list->Add(key); +}; + +template +TKey Stack::Pop() { + if (IsEmpty()) + throw "Stack Is Empty"; + TKey key = list->GetRoot(); + try { + list->Remove(key); + } + catch (const char* error) { + cout << error << endl; + } + return key; +}; + +template +TKey Stack::Take()const { + if (IsEmpty()) + throw "Stack Is Empty"; + return List.GetRoot(); +}; + +template +bool Stack::IsEmpty()const { + return(list->Size() == 0); +}; + +template +bool Stack::IsFull()const { + TKey key = -1; + try { + list->Add(key); + list->Remove(key); + } + catch (...) { + return true; + } + return false; +}; + +#endif \ No newline at end of file diff --git a/KaganovDA/lab1/include/postfix.h b/KaganovDA/lab1/include/postfix.h new file mode 100644 index 000000000..57777adfb --- /dev/null +++ b/KaganovDA/lab1/include/postfix.h @@ -0,0 +1,25 @@ +#ifndef POLISH_POSTFIX_H +#define POLISH_POSTFIX_H + +#include +#include +#include + +#include "Stack.h" +using namespace std; + +class postfix +{ +private: + static int GetOperationPtr(char op); // + static int IsOperation(char op); // + static bool Operand(const char Exp); + static float calc_op(float one, float two, char op); +public: + /* static bool Operand(const char Exp); + static float calc_op(float one, float two, char op); */ + static char* ConvertToPolish(char *Exp, int len); // + static float ConvertToInfix(char* Exp, int len); // +}; + +#endif \ No newline at end of file diff --git a/KaganovDA/lab1/sample/main.cpp b/KaganovDA/lab1/sample/main.cpp new file mode 100644 index 000000000..853a73a1f Binary files /dev/null and b/KaganovDA/lab1/sample/main.cpp differ diff --git a/KaganovDA/lab1/src/List.cpp b/KaganovDA/lab1/src/List.cpp new file mode 100644 index 000000000..924e8fdc1 --- /dev/null +++ b/KaganovDA/lab1/src/List.cpp @@ -0,0 +1 @@ +#include "List.h" \ No newline at end of file diff --git a/KaganovDA/lab1/src/Node.cpp b/KaganovDA/lab1/src/Node.cpp new file mode 100644 index 000000000..37456e587 --- /dev/null +++ b/KaganovDA/lab1/src/Node.cpp @@ -0,0 +1 @@ +#include "Node.h" \ No newline at end of file diff --git a/KaganovDA/lab1/src/Stack.cpp b/KaganovDA/lab1/src/Stack.cpp new file mode 100644 index 000000000..7e686a02c --- /dev/null +++ b/KaganovDA/lab1/src/Stack.cpp @@ -0,0 +1 @@ +#include "Stack.h" \ No newline at end of file diff --git a/KaganovDA/lab1/src/postfix.cpp b/KaganovDA/lab1/src/postfix.cpp new file mode 100644 index 000000000..2eb204cf3 --- /dev/null +++ b/KaganovDA/lab1/src/postfix.cpp @@ -0,0 +1,123 @@ +#include "postfix.h" +#include "Stack.h" +#include +#include + +using namespace std; + +int postfix::GetOperationPtr(char op) { + int Ptr; + switch (op) { + case '*': + case '/': Ptr = 3; break; + case '+': + case '-': Ptr = 2; break; + case '(': Ptr = 1; break; + case '=': Ptr = 0; break; + default: Ptr = -1; + } + return Ptr; +} + +int postfix::IsOperation(char op) { + if (op == '+' || op == '-' || op == '*' || op == '/' || op == '=') return 1; + else return 0; +} + +bool postfix::Operand(const char Exp) { + if ((Exp >= 65) && (Exp <= 90)) { + return true; + } + else if (((Exp >= 40) && (Exp <= 43)) || (Exp == 45) || (Exp == 47) || (Exp == 61)) { + return false; + } + throw "Wrong operation " + Exp; +} + +float postfix::calc_op(float one, float two, char op) { + switch (op) { + case '+': + return (one + two); + case '-': + return (one - two); + case '*': + return (one * two); + case '/': + if (two == 0) + { + throw "! !\n"; + } + else return (one / two); + default: + return -1; + } +} + +char* postfix::ConvertToPolish(char *InfixExp, int len) { + char ch, t, *PolishExp = new char[strlen(InfixExp) + 1]; + int pos = 0; // + Stack PolishStack, OperationStack; + bool key; + do { + ch = InfixExp[pos++]; + if (isalpha(ch)) PolishStack.Push(ch); + else if (ch == '(') OperationStack.Push(ch); + else if (ch == ')') { + while (1) { + t = OperationStack.Pop(); + if (t == '(') break; + PolishStack.Push(t); + } + } + else if (IsOperation(ch)) { + while (!OperationStack.IsEmpty()) { + t = OperationStack.Pop(); + if (GetOperationPtr(ch) <= GetOperationPtr(t)) PolishStack.Push(t); + else { OperationStack.Push(t); break; } + } + OperationStack.Push(ch); + } + } while ((ch != '=') && (pos < len)); + pos = 0; + for (int i = 0; i < len; i++) + if ((InfixExp[i] != '(') && (InfixExp[i] != ')')) pos++; + PolishExp[pos] = '\0'; + PolishExp[--pos] = '='; + while (!PolishStack.IsEmpty()) PolishExp[--pos] = PolishStack.Pop(); + return PolishExp; +} + +float postfix::ConvertToInfix(char *Exp, int l) { + Exp[l - 1] = '\0'; + l--; + map nums; + for (size_t i = 0; i < l; i++) { + if (postfix::Operand(Exp[i]) && nums.count(Exp[i]) == 0) { + float var; + cout << " : " << Exp[i] << " = "; + cin >> var; + nums.insert(pair(Exp[i], var)); + } + + } + Stack end_list; + for (int i = 0; i < l; i++) { + char element = Exp[i]; + if (postfix::Operand(element)) { + end_list.Push(nums[element]); + } + else { + double two = end_list.Pop(); + double one = end_list.Pop(); + /* try { + //float a = calc_op(one, two, element); + end_list.Push(calc_op(one, two, element)); + } + catch(const char* error) { + cout << error << endl; + } */ + end_list.Push(calc_op(one, two, element)); + } + } + return end_list.Pop(); +} diff --git a/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_lib.vcxproj b/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_lib.vcxproj new file mode 100644 index 000000000..d0339a48d --- /dev/null +++ b/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_lib.vcxproj @@ -0,0 +1,166 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + + 15.0 + {B8ED159E-FD18-4393-9706-90D89CE18FD3} + Win32Proj + dejkstralib + 10.0.16299.0 + + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + StaticLibrary + true + v141 + Unicode + + + StaticLibrary + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Windows + true + + + + + Use + Level3 + Disabled + true + _DEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Windows + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_LIB;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + \ No newline at end of file diff --git a/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_lib.vcxproj.filters b/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_lib.vcxproj.filters new file mode 100644 index 000000000..927703624 --- /dev/null +++ b/KaganovDA/lab2/build/lab2_dejkstra/dejkstra_lib/dejkstra_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;ipp;xsd + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + \ No newline at end of file diff --git a/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra.sln b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra.sln new file mode 100644 index 000000000..0c8b5a315 --- /dev/null +++ b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra.sln @@ -0,0 +1,41 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27428.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lab2_dejkstra", "lab2_dejkstra\lab2_dejkstra.vcxproj", "{69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dejkstra_lib", "dejkstra_lib\dejkstra_lib.vcxproj", "{B8ED159E-FD18-4393-9706-90D89CE18FD3}" +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 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Debug|x64.ActiveCfg = Debug|x64 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Debug|x64.Build.0 = Debug|x64 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Debug|x86.ActiveCfg = Debug|Win32 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Debug|x86.Build.0 = Debug|Win32 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Release|x64.ActiveCfg = Release|x64 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Release|x64.Build.0 = Release|x64 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Release|x86.ActiveCfg = Release|Win32 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA}.Release|x86.Build.0 = Release|Win32 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Debug|x64.ActiveCfg = Debug|x64 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Debug|x64.Build.0 = Debug|x64 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Debug|x86.ActiveCfg = Debug|Win32 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Debug|x86.Build.0 = Debug|Win32 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Release|x64.ActiveCfg = Release|x64 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Release|x64.Build.0 = Release|x64 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Release|x86.ActiveCfg = Release|Win32 + {B8ED159E-FD18-4393-9706-90D89CE18FD3}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {259E4AF7-2893-4BD1-A786-AC3FC9295A43} + EndGlobalSection +EndGlobal diff --git a/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj new file mode 100644 index 000000000..6df668086 --- /dev/null +++ b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj @@ -0,0 +1,161 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 15.0 + {69C89B8E-CFC5-467E-8DC7-8BE8FF3560CA} + Win32Proj + lab2dejkstra + 10.0.16299.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Console + true + dejkstra_lib.lib;%(AdditionalDependencies) + ..\Debug + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ..\..\..\include + + + Console + true + true + true + dejkstra_lib.lib;%(AdditionalDependencies) + ..\Release + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj.filters b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj.filters new file mode 100644 index 000000000..6c319c22f --- /dev/null +++ b/KaganovDA/lab2/build/lab2_dejkstra/lab2_dejkstra/lab2_dejkstra.vcxproj.filters @@ -0,0 +1,14 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + + + Исходные файлы + + + \ No newline at end of file diff --git a/KaganovDA/lab2/doc/Dejkstra_one.pdf b/KaganovDA/lab2/doc/Dejkstra_one.pdf new file mode 100644 index 000000000..e8f602250 Binary files /dev/null and b/KaganovDA/lab2/doc/Dejkstra_one.pdf differ diff --git a/KaganovDA/lab2/include/DHeap.h b/KaganovDA/lab2/include/DHeap.h new file mode 100644 index 000000000..c5b079eda --- /dev/null +++ b/KaganovDA/lab2/include/DHeap.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include "Graph.h" + +#define maxSize 1000 +typedef int dataType; + +class Data { +public: + float priorities; +}; + +class DHeap { +protected: + Data **keys; + int d; + int idx; +public: + DHeap(int d); + DHeap(const DHeap &heap); + ~DHeap(); + + void Add(Data *&key); + void AddSet(Data **key, int num); + Data* Delete(); + Data* Remove(int i); // i + + void Transpose(int i, int j); // + void Surfacing(int i); // + void Sinking(int i); // + void Hilling(); // + int IsFull()const; + int IsEmpty()const; + +private: + int MinChild(int i); // i +}; diff --git a/KaganovDA/lab2/include/Dejkstra.h b/KaganovDA/lab2/include/Dejkstra.h new file mode 100644 index 000000000..c0d09a621 --- /dev/null +++ b/KaganovDA/lab2/include/Dejkstra.h @@ -0,0 +1,16 @@ +#pragma once +#include "Graph.h" +#include "DHeap.h" +#include "PriorityQueue.h" +#include + +class DataFloat : public Data { +public: + DataFloat(int s, float dist); + int s; // +}; + +class Dejkstra { +public: + static void dejkstra(Graph *&graph, int s, float *&distance, int *&up); +}; diff --git a/KaganovDA/lab2/include/Graph.h b/KaganovDA/lab2/include/Graph.h new file mode 100644 index 000000000..f5222e7c2 --- /dev/null +++ b/KaganovDA/lab2/include/Graph.h @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include + +#define maxVerticesSize 1000 + +class Edge { +public: + int Ne; // + int Ke; // + float W; // + + Edge(int Ne, int Ke, float W); +}; + +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 Generate(float minRange, float maxRange); // + void AddEdge(int Ne, int Ke, float weight); + void DelEdge(int Ne, int Ke); + int GetVerticesNum(); + int GetEdgeSize(); + int GetRealSize(); + Edge** GetEdgeSet(); + Edge* GetEdge(int i); + float GetWeight(int Ne, int Ke); + void PrintList(); +private: + void GenerateVertices(int &Ne, int &Ke); + float GenerateWeight(float minRange, float maxRange); + void Clean(); + int FindEdge(int Ne, int Ke); +}; diff --git a/KaganovDA/lab2/include/PriorityQueue.h b/KaganovDA/lab2/include/PriorityQueue.h new file mode 100644 index 000000000..eead22068 --- /dev/null +++ b/KaganovDA/lab2/include/PriorityQueue.h @@ -0,0 +1,44 @@ +#pragma once +#include "DHeap.h" + +enum QueueID { + HEAP = 0, +}; + +class PriorityQueue { +public: + PriorityQueue() {}; + + virtual void Push(Data *&key) = 0; + virtual Data* Pop() = 0; + + + virtual int IsFull()const = 0; + virtual int IsEmpty()const = 0; +}; + +class DHeapBasedPriorityQueue : public PriorityQueue { +protected: + DHeap* heap; +public: + DHeapBasedPriorityQueue(int d = 4); + DHeapBasedPriorityQueue(const DHeapBasedPriorityQueue &queue); + DHeapBasedPriorityQueue(Data **keys, int num, int d = 4); + ~DHeapBasedPriorityQueue(); + + virtual void Push(Data *&key); + virtual Data* Pop(); + + virtual int IsFull()const; + virtual int IsEmpty()const; +}; + +class QueueFactory { +public: + static PriorityQueue* createQueue(QueueID Qid) + { + PriorityQueue *queue; + queue = new DHeapBasedPriorityQueue(); + return queue; + } +}; diff --git a/KaganovDA/lab2/sample/GenerateGraph.cpp b/KaganovDA/lab2/sample/GenerateGraph.cpp new file mode 100644 index 000000000..a218236cb --- /dev/null +++ b/KaganovDA/lab2/sample/GenerateGraph.cpp @@ -0,0 +1,136 @@ +#include "Graph.h" +#include "Dejkstra.h" +#include +#include +#include +#include +#include +using namespace std; + +int main(int argc, char **argv) +{ + setlocale(LC_ALL, "Russian"); + Graph* graph; + int n; // + int m; // + int s; // + + + int typeGenerate; + cout << " - : "; + cin >> n; + cout << " - : "; + cin >> m; + try + { + graph = new Graph(n, m); + } + catch (const char ex) + { + cout << ex; + return -1; + } + + cout << " :" << endl; + cout << "1. : " << endl; + cout << "2. : " << endl; + + + cin >> typeGenerate; + switch (typeGenerate) + { + case 1: { + try + { + int minRange; + int maxRange; + cout << " : "; + cin >> minRange; + cout << " : "; + cin >> maxRange; + graph->Generate(minRange, maxRange); + } + catch (const char ex) + { + cout << ex; + return -1; + } + break; + } + case 2: { + int _nac_ver, _kon_ver; + float _weight; + + try + { + for (int i = 0; i < m; i++) { + cout << " : " << endl; + cin >> _nac_ver; + cout << " : " << endl; + cin >> _kon_ver; + cout << " : " << endl; + cin >> _weight; + graph->AddEdge(_nac_ver, _kon_ver, _weight); + } + } + catch (const char ex) + { + cout << ex; + return -1; + } + break; + } + } + cout << "Write start vertices for Dijkstra" << endl; + cin >> s; + + graph->PrintList(); + cout << endl; + + float *dist; + int *up; + try { + Dejkstra::dejkstra(graph, s, dist, up); + } + catch (const char ex) + { + cout << ex; + return -2; + } + + cout << n << ' ' << m << endl; + cout << "start vertices = " << s << endl; + + m = graph->GetRealSize(); + Edge* edge; + cout << " Matrix edges " << endl; + for (int j = 0; j < m; j++) { + edge = graph->GetEdge(j); + cout << edge->Ne << ' ' << edge->Ke << ' ' << edge->W << endl; + } + + cout << " Distances " << endl; + for (int i = 0; i < n; i++) + if (dist[i] != FLT_MAX) + cout << dist[i] << ' '; + else + cout << "inf" << ' '; + + cout << endl << " Ways " << endl; + for (int i = 0; i < n; i++) { + cout << endl; + cout << i; + cout << " distance " << dist[i]; + cout << " up " << up[i]; + } + + + delete graph; + delete[]dist; + delete[]up; + + + + cout << endl; + return 0; +} diff --git a/KaganovDA/lab2/src/DHeap.cpp b/KaganovDA/lab2/src/DHeap.cpp new file mode 100644 index 000000000..c129023b9 --- /dev/null +++ b/KaganovDA/lab2/src/DHeap.cpp @@ -0,0 +1,145 @@ +#include "DHeap.h" + + +DHeap::DHeap(int d) +{ + if (d <= 0) + throw "DHeap: Invalid element"; + this->d = d; + idx = -1; + keys = new Data*[maxSize]; +} + +DHeap::DHeap(const DHeap &heap) +{ + this->d = heap.d; + keys = new Data*[maxSize]; + for (int i = 0; i <= heap.idx; i++) + keys[i] = heap.keys[i]; + this->idx = heap.idx; +} + +DHeap::~DHeap() +{ + delete[] keys; +} + +void DHeap::Add(Data *&key) +{ + if (IsFull()) + throw "DHeap: No Memory"; + idx++; + keys[idx] = key; + Surfacing(idx); +} + +void DHeap::AddSet(Data **key, int num) +{ + if (idx + num >= maxSize) + throw "DAryHeap: Too large set!"; + for (int i = idx + 1; i < idx + num + 1; i++) + { + keys[i] = key[i - idx - 1]; + } + idx += num; + Hilling(); +} + +Data* DHeap::Delete() +{ + if (IsEmpty()) + throw "DHeap: No data"; + Data* key = keys[idx]; + idx--; + return key; +} + +Data* DHeap::Remove(int i) +{ + if (IsEmpty()) + throw "DHeap: No data"; + if ((i < 0) || (i > idx)) + throw "DHeap: Invalid index"; + + Data* key = keys[i]; + if (i == idx) + { + idx--; + return key; + } + Transpose(i, idx); + idx--; + Sinking(i); + return key; +} + +void DHeap::Transpose(int i, int j) +{ + if ((i < 0) || (j < 0) || (i > idx) || (j > idx)) + throw "DHeap: Invalid indexes"; + Data* tmp = keys[i]; + keys[i] = keys[j]; + keys[j] = tmp; +} + +void DHeap::Surfacing(int i) +{ + if ((i < 0) || (i > idx)) + throw "DHeap: Invalid index"; + + int p = (i - 1) / d; + while (p > 0 && keys[p]->priorities > keys[i]->priorities) { + Transpose(i, p); + i = p; + p = (i - 1) / d; + } +} + +void DHeap::Sinking(int i) +{ + if ((i < 0) || (i > idx)) + throw "DHeap: Invalid index"; + + int c = MinChild(i); + while ((c != -1) && (keys[c]->priorities < keys[i]->priorities)) { + Transpose(i, c); + i = c; + c = MinChild(i); + } +} + +void DHeap::Hilling() +{ + for (int i = idx; i >= 0; i--) + Sinking(i); +} + +const int DHeap::IsFull() +{ + return idx >= maxSize - 1; +} + +const int DHeap::IsEmpty() +{ + return idx == -1; +} + +int DHeap::MinChild(int i) +{ + int f = i * d + 1; + if (f > idx) + return -1; + + int l = std::min(i*d + d, idx); + 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/KaganovDA/lab2/src/Dejkstra.cpp b/KaganovDA/lab2/src/Dejkstra.cpp new file mode 100644 index 000000000..9af46d13d --- /dev/null +++ b/KaganovDA/lab2/src/Dejkstra.cpp @@ -0,0 +1,63 @@ +#include "Dejkstra.h" + + +DataFloat::DataFloat(int s, float dist) +{ + this->s = s; + priorities = dist; +} + +void Dejkstra::dejkstra(Graph *&graph, int s, float *&distance, int *&up) +{ + int n = graph->GetVerticesNum(); + int m = graph->GetRealSize(); + if ((s < 0) || (s >= n)) + throw "Dejkstra: 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())->s; + float delta; + + for (int i = 0; i < m; i++) + { + int vIncident = -1; + if (edges[i]->Ke == vConsidered) + vIncident = edges[i]->Ne; + if (edges[i]->Ne == vConsidered) + vIncident = edges[i]->Ke; + 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; // + } + } + } + + 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; +} diff --git a/KaganovDA/lab2/src/Graph.cpp b/KaganovDA/lab2/src/Graph.cpp new file mode 100644 index 000000000..55b25d0ce --- /dev/null +++ b/KaganovDA/lab2/src/Graph.cpp @@ -0,0 +1,176 @@ +#include "Graph.h" + + +Edge::Edge(int Ne, int Ke, float W) +{ + this->Ne = Ne; + this->Ke = Ke; + this->W = W; +} + +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() +{ + Clean(); + delete[] edges; + delete[] vertices; +} + +void Graph::Generate(float minRange, float maxRange) +{ + int Ne; + int Ke; + float W; + + if (minRange > maxRange) + throw "Graph: Invalid ranges"; + + if (m_cur) { + Clean(); + m_cur = 0; + } + + srand(time(NULL)); + for (int i = 0; i < m; i++) { + GenerateVertices(Ne, Ke); + W = GenerateWeight(minRange, maxRange); + edges[i] = new Edge(Ne, Ke, W); + m_cur++; + } +} + +void Graph::AddEdge(int Ne, int Ke, float W) +{ + if (m_cur == m) + throw "Graph: Graph is full"; + if (Ne == Ke) + throw "Graph: Loops are disabled"; + if (FindEdge(Ne, Ke) != -1) + throw "Graph: multiple edges arent allowed!"; + edges[m_cur] = new Edge(Ne, Ke, W); + m_cur++; +} + +void Graph::DelEdge(int Ne, int Ke) +{ + int i = FindEdge(Ne, Ke); + if (i == -1) + throw "Graph: try delete non-existent edge"; + delete edges[i]; + edges[i] = 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 i) +{ + return edges[i]; +} + +float Graph::GetWeight(int Ne, int Ke) +{ + int i = FindEdge(Ne, Ke); + if (i == -1) + throw "Graph: Invalid edge!"; + return edges[i]->W; +} + +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]->Ne == i) + cout << edges[j]->Ke << '(' << edges[j]->W << ')' << ", "; + if (edges[j]->Ke == i) + cout << edges[j]->Ne << '(' << edges[j]->W << ')' << ", "; + } + cout << endl; + } +} + +void Graph::GenerateVertices(int &Ne, int &Ke) +{ + do { + Ne = rand() % n; + Ke = rand() % n; + } while ((Ne == Ke) || (FindEdge(Ne, Ke) != -1)); +} + +float Graph::GenerateWeight(float minRange, float maxRange) +{ + double a = minRange; + double b = (double)(maxRange - minRange) / RAND_MAX; + double result = b * rand() + a; + return result; +} + +void Graph::Clean() +{ + for (int i = 0; i < m_cur; i++) + delete edges[i]; +} + +int Graph::FindEdge(int Ne, int Ke) +{ + for (int i = 0; i < m_cur; i++) + if ((edges[i]->Ke == Ke) && + (edges[i]->Ne == Ne) || + (edges[i]->Ne == Ke) && + (edges[i]->Ke == Ne)) + return i; + return -1; +} diff --git a/KaganovDA/lab2/src/PriorityQueue.cpp b/KaganovDA/lab2/src/PriorityQueue.cpp new file mode 100644 index 000000000..f98f52b5d --- /dev/null +++ b/KaganovDA/lab2/src/PriorityQueue.cpp @@ -0,0 +1,43 @@ +#include "PriorityQueue.h" + + +DHeapBasedPriorityQueue::DHeapBasedPriorityQueue(int d) +{ + heap = new DHeap(d); +} + +DHeapBasedPriorityQueue::DHeapBasedPriorityQueue(const DHeapBasedPriorityQueue &queue) +{ + this->heap = new DHeap(*(queue.heap)); +} + +DHeapBasedPriorityQueue::DHeapBasedPriorityQueue(Data **keys, int num, int d) +{ + heap = new DHeap(d); + heap->AddSet(keys, num); +} + +DHeapBasedPriorityQueue::~DHeapBasedPriorityQueue() +{ + delete heap; +} + +void DHeapBasedPriorityQueue::Push(Data *&key) +{ + heap->Add(key); +} + +Data* DHeapBasedPriorityQueue::Pop() +{ + return heap->Remove(0); +} + +int DHeapBasedPriorityQueue::IsFull()const +{ + return heap->IsFull(); +} + +int DHeapBasedPriorityQueue::IsEmpty()const +{ + return heap->IsEmpty(); +}