From 94609dcd1435dff35d5dfd5b6ec322a3d4d2f948 Mon Sep 17 00:00:00 2001 From: Victoria Date: Fri, 26 Oct 2018 22:24:51 +0300 Subject: [PATCH 01/12] some changes --- hw6/bracket sequence/bracket sequence.sln | 31 +++++ .../bracket sequence/Element.cpp | 0 .../bracket sequence/Element.h | 1 + .../bracket sequence/Stack.cpp | 0 hw6/bracket sequence/bracket sequence/Stack.h | 1 + .../bracket sequence/bracket sequence.vcxproj | 127 ++++++++++++++++++ .../bracket sequence.vcxproj.filters | 30 +++++ .../bracket sequence/main.cpp | 0 8 files changed, 190 insertions(+) create mode 100644 hw6/bracket sequence/bracket sequence.sln create mode 100644 hw6/bracket sequence/bracket sequence/Element.cpp create mode 100644 hw6/bracket sequence/bracket sequence/Element.h create mode 100644 hw6/bracket sequence/bracket sequence/Stack.cpp create mode 100644 hw6/bracket sequence/bracket sequence/Stack.h create mode 100644 hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj create mode 100644 hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters create mode 100644 hw6/bracket sequence/bracket sequence/main.cpp diff --git a/hw6/bracket sequence/bracket sequence.sln b/hw6/bracket sequence/bracket sequence.sln new file mode 100644 index 0000000..59d60cd --- /dev/null +++ b/hw6/bracket sequence/bracket sequence.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bracket sequence", "bracket sequence\bracket sequence.vcxproj", "{E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}" +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 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x64.ActiveCfg = Debug|x64 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x64.Build.0 = Debug|x64 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x86.ActiveCfg = Debug|Win32 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Debug|x86.Build.0 = Debug|Win32 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x64.ActiveCfg = Release|x64 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x64.Build.0 = Release|x64 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x86.ActiveCfg = Release|Win32 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A3CDAC60-BE3A-42CD-AD96-73C4C1E3F3A4} + EndGlobalSection +EndGlobal diff --git a/hw6/bracket sequence/bracket sequence/Element.cpp b/hw6/bracket sequence/bracket sequence/Element.cpp new file mode 100644 index 0000000..e69de29 diff --git a/hw6/bracket sequence/bracket sequence/Element.h b/hw6/bracket sequence/bracket sequence/Element.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/Element.h @@ -0,0 +1 @@ +#pragma once diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp new file mode 100644 index 0000000..e69de29 diff --git a/hw6/bracket sequence/bracket sequence/Stack.h b/hw6/bracket sequence/bracket sequence/Stack.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/Stack.h @@ -0,0 +1 @@ +#pragma once diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj new file mode 100644 index 0000000..60a42f9 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -0,0 +1,127 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {E0E7D27A-9576-458F-B076-FBF3A2DEB9C8} + bracketsequence + 10.0.17134.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + Level3 + Disabled + true + true + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters new file mode 100644 index 0000000..dcfde47 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {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 + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp new file mode 100644 index 0000000..e69de29 From efc204167be2c5bd7f28113bf25014d52f99bf2c Mon Sep 17 00:00:00 2001 From: Victoria Date: Fri, 26 Oct 2018 23:41:43 +0300 Subject: [PATCH 02/12] made structure stack --- .../bracket sequence/Element.cpp | 13 ++++ .../bracket sequence/Element.h | 8 +++ .../bracket sequence/Stack.cpp | 64 +++++++++++++++++++ hw6/bracket sequence/bracket sequence/Stack.h | 27 ++++++++ .../bracket sequence/bracket sequence.vcxproj | 6 +- .../bracket sequence.vcxproj.filters | 12 +++- .../bracket sequence/main.cpp | 16 +++++ 7 files changed, 141 insertions(+), 5 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/Element.cpp b/hw6/bracket sequence/bracket sequence/Element.cpp index e69de29..8bbeb35 100644 --- a/hw6/bracket sequence/bracket sequence/Element.cpp +++ b/hw6/bracket sequence/bracket sequence/Element.cpp @@ -0,0 +1,13 @@ +#include "Element.h" + +Element::Element(char data = '\0') +{ + this->data = data; + this->next = nullptr; +} + +Element::~Element() +{ + this->data = '\0'; + this->next = nullptr; +} \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Element.h b/hw6/bracket sequence/bracket sequence/Element.h index 6f70f09..f15288d 100644 --- a/hw6/bracket sequence/bracket sequence/Element.h +++ b/hw6/bracket sequence/bracket sequence/Element.h @@ -1 +1,9 @@ #pragma once + +struct Element +{ + Element(char data); + ~Element(); + char data; + Element* next; +}; \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp index e69de29..5d5cd52 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.cpp +++ b/hw6/bracket sequence/bracket sequence/Stack.cpp @@ -0,0 +1,64 @@ +#include "Stack.h" +#include "Element.h" + +Stack::Stack() +{ + head = nullptr; +} + +Stack::~Stack() +{ + auto* temp = head; + while (!isEmpty()) + { + temp = head; + head = temp->next; + temp->next = nullptr; + delete temp; + temp = nullptr; + } +} + +void Stack::push(char data) +{ + auto* newElement = new Element(data); + if (!isEmpty()) + { + newElement->next = head; + } + head = newElement; +} + +int Stack::pop() +{ + if (!isEmpty()) + { + auto* temp = head->next; + head->next = nullptr; + delete head; + head = nullptr; + head = temp; + return 0; + } + else + { + return -1; + } +} + +char Stack::peek() +{ + if (!isEmpty()) + { + return head->data; + } + else + { + cout << "Stack is empty\n"; + } +} + +bool Stack::isEmpty() +{ + return head == nullptr; +} \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Stack.h b/hw6/bracket sequence/bracket sequence/Stack.h index 6f70f09..6b95aff 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.h +++ b/hw6/bracket sequence/bracket sequence/Stack.h @@ -1 +1,28 @@ #pragma once +#include + +using namespace std; + +struct Element; + +struct Stack +{ + // + Stack(); + // + ~Stack(); + // + + // + + // + void push(char data); + // + int pop(); + // + char peek(); + // + bool isEmpty(); +private: + Element* head; +}; \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj index 60a42f9..8fac5ac 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -115,11 +115,13 @@ + - + - + + diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters index dcfde47..8f9717b 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters @@ -15,15 +15,21 @@ - + Исходные файлы - + + Исходные файлы + + Исходные файлы - + + Файлы заголовков + + Файлы заголовков diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index e69de29..540e55f 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -0,0 +1,16 @@ +#include "Stack.h" +#include +#include + +using namespace std; + +// , +// : (), [], {}. ({ ) } , ({}) - . + +int main() +{ + + + system("pause"); + return 0; +} \ No newline at end of file From 4d07785b5e1a03e1cfe1d8a9bcb715a0b1555fdf Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 00:24:54 +0300 Subject: [PATCH 03/12] trying to make function that checks if bracket sequence is correct --- .../bracket sequence/Stack.cpp | 1 + .../bracket sequence/bracket sequence.vcxproj | 2 + .../bracket sequence.vcxproj.filters | 6 ++ .../bracket sequence/ifBracketSequence.cpp | 70 +++++++++++++++++++ .../bracket sequence/ifBracketSequence.h | 5 ++ .../bracket sequence/main.cpp | 6 +- 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp create mode 100644 hw6/bracket sequence/bracket sequence/ifBracketSequence.h diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp index 5d5cd52..000c1f2 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.cpp +++ b/hw6/bracket sequence/bracket sequence/Stack.cpp @@ -55,6 +55,7 @@ char Stack::peek() else { cout << "Stack is empty\n"; + return '\0'; } } diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj index 8fac5ac..e8b6741 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -116,11 +116,13 @@ + + diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters index 8f9717b..9490658 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters @@ -24,6 +24,9 @@ Исходные файлы + + Исходные файлы + @@ -32,5 +35,8 @@ Файлы заголовков + + Файлы заголовков + \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp b/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp new file mode 100644 index 0000000..6a5e92c --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp @@ -0,0 +1,70 @@ +#include "ifBracketSequence.h" + +bool ifBracketSequenceIsCorrect(string const & str) +{ + Stack s; + bool ifSequenceIsCorrect = true; + for (int i = 0; i < str.size(); ++i) + { + if (isBracket(s, str[i])) + { + if (s.isEmpty()) + { + ifSequenceIsCorrect = ifNotClosingBracketToEmptyStack(s, str[i]); + if (!ifBracketSequenceIsCorrect) + { + return ifSequenceIsCorrect; + } + } + else + { + if (str[i] == ')' || str[i] == ']' || str[i] == '}') + { + if (s.peek() == ')' || s.peek() == ']' || s.peek() == '}') + { + ifSequenceIsCorrect = false; + return ifBracketSequenceIsCorrect; + } + // , , , + } + else + { + // - , , + } + } + } + } +} + +bool isBracket(Stack const & s, char const symbol) +{ + if (symbol == '(' || symbol == ')' || symbol == '[' || symbol == ']' || + symbol == '{' || symbol == '}') + { + return true; + } + else + { + return false; + } +} + +bool ifClosingBracket(Stack const & s, char const symbol) +{ + +} + +bool ifNotClosingBracketToEmptyStack(Stack & s, char const symbol) +{ + if (symbol == ')' || symbol == ']' || symbol == '}') + { + return false; + } + else + { + s.push(symbol); + return true; + } +} + +bool ifSameTypeOfBracket \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/ifBracketSequence.h b/hw6/bracket sequence/bracket sequence/ifBracketSequence.h new file mode 100644 index 0000000..94bea4d --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/ifBracketSequence.h @@ -0,0 +1,5 @@ +#pragma once +#include "Stack.h" +#include + +bool ifBracketSequenceIsCorrect(string const & str); \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index 540e55f..7e1d8f7 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -2,11 +2,13 @@ #include #include -using namespace std; - // , // : (), [], {}. ({ ) } , ({}) - . +using namespace std; + +bool ifBracketSequenceIsCorrect(string const & str); + int main() { From 2bb51d9b3dd56961341dcbbca3afe9787465b00c Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 01:16:57 +0300 Subject: [PATCH 04/12] function that checks seems ready --- .../bracket sequence/bracket sequence.cpp | 91 +++++++++++++++++++ ...ifBracketSequence.h => bracket sequence.h} | 0 .../bracket sequence/bracket sequence.vcxproj | 4 +- .../bracket sequence.vcxproj.filters | 4 +- .../bracket sequence/ifBracketSequence.cpp | 70 -------------- .../bracket sequence/main.cpp | 9 +- 6 files changed, 102 insertions(+), 76 deletions(-) create mode 100644 hw6/bracket sequence/bracket sequence/bracket sequence.cpp rename hw6/bracket sequence/bracket sequence/{ifBracketSequence.h => bracket sequence.h} (100%) delete mode 100644 hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp new file mode 100644 index 0000000..db48603 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp @@ -0,0 +1,91 @@ +#include "bracket sequence.h" + +bool isBracket(char const symbol); +bool ifClosingBracket(char const symbol); +bool ifOpeningBracket(Stack & s, char const symbol); +bool ifPairOfBrackets(Stack & s, char const closingBracket); + +bool ifBracketSequenceIsCorrect(string const & str) +{ + Stack s; + for (int i = 0; i < str.size(); ++i) + { + if (isBracket(str[i])) + { + if (s.isEmpty()) + { + if (!ifOpeningBracket(s, str[i])) + { + return false; + } + } + else + { + if (ifClosingBracket(str[i])) + { + if (ifClosingBracket(s.peek())) + { + return false; + } + if (!ifPairOfBrackets(s, str[i])) + { + return false; + } + } + else + { + s.push(str[i]); + } + } + } + } + if (!s.isEmpty()) + { + return false; + } + return true; +} + +bool isBracket(char const symbol) +{ + return (symbol == '(' || symbol == ')' || symbol == '[' || symbol == ']' || + symbol == '{' || symbol == '}'); +} + +bool ifClosingBracket(char const symbol) +{ + return (symbol == ')' || symbol == ']' || symbol == '}'); +} + +bool ifOpeningBracket(Stack & s, char const symbol) +{ + if (symbol == '(' || symbol == '[' || symbol == '{') + { + s.push(symbol); + return true; + } + else + { + return false; + } +} + +bool ifPairOfBrackets(Stack & s, char const closingBracket) +{ + if (s.peek() == '(' && closingBracket == ')') + { + s.pop(); + return true; + } + if (s.peek() == '[' && closingBracket == ']') + { + s.pop(); + return true; + } + if (s.peek() == '{' && closingBracket == '}') + { + s.pop(); + return true; + } + return false; +} \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/ifBracketSequence.h b/hw6/bracket sequence/bracket sequence/bracket sequence.h similarity index 100% rename from hw6/bracket sequence/bracket sequence/ifBracketSequence.h rename to hw6/bracket sequence/bracket sequence/bracket sequence.h diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj index e8b6741..36d750f 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -116,13 +116,13 @@ - + - + diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters index 9490658..d9f5171 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters @@ -24,7 +24,7 @@ Исходные файлы - + Исходные файлы @@ -35,7 +35,7 @@ Файлы заголовков - + Файлы заголовков diff --git a/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp b/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp deleted file mode 100644 index 6a5e92c..0000000 --- a/hw6/bracket sequence/bracket sequence/ifBracketSequence.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "ifBracketSequence.h" - -bool ifBracketSequenceIsCorrect(string const & str) -{ - Stack s; - bool ifSequenceIsCorrect = true; - for (int i = 0; i < str.size(); ++i) - { - if (isBracket(s, str[i])) - { - if (s.isEmpty()) - { - ifSequenceIsCorrect = ifNotClosingBracketToEmptyStack(s, str[i]); - if (!ifBracketSequenceIsCorrect) - { - return ifSequenceIsCorrect; - } - } - else - { - if (str[i] == ')' || str[i] == ']' || str[i] == '}') - { - if (s.peek() == ')' || s.peek() == ']' || s.peek() == '}') - { - ifSequenceIsCorrect = false; - return ifBracketSequenceIsCorrect; - } - // , , , - } - else - { - // - , , - } - } - } - } -} - -bool isBracket(Stack const & s, char const symbol) -{ - if (symbol == '(' || symbol == ')' || symbol == '[' || symbol == ']' || - symbol == '{' || symbol == '}') - { - return true; - } - else - { - return false; - } -} - -bool ifClosingBracket(Stack const & s, char const symbol) -{ - -} - -bool ifNotClosingBracketToEmptyStack(Stack & s, char const symbol) -{ - if (symbol == ')' || symbol == ']' || symbol == '}') - { - return false; - } - else - { - s.push(symbol); - return true; - } -} - -bool ifSameTypeOfBracket \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index 7e1d8f7..1a9b5f9 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -1,4 +1,5 @@ #include "Stack.h" +#include "bracket sequence.h" #include #include @@ -7,11 +8,15 @@ using namespace std; -bool ifBracketSequenceIsCorrect(string const & str); +// bool ifBracketSequenceIsCorrect(string const & str); int main() { - + string s; + cout << "Input string" << endl; + cin >> s; + bool result = ifBracketSequenceIsCorrect(s); + cout << "res is " << result << endl; system("pause"); return 0; From 60fbd8193e329aa9bde1c4aae3649a54f5ebde06 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 01:28:39 +0300 Subject: [PATCH 05/12] changed design in functions for brackets --- .../bracket sequence/bracket sequence.cpp | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp index db48603..81e44a8 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp @@ -2,8 +2,8 @@ bool isBracket(char const symbol); bool ifClosingBracket(char const symbol); -bool ifOpeningBracket(Stack & s, char const symbol); -bool ifPairOfBrackets(Stack & s, char const closingBracket); +bool ifOpeningBracket(char const symbol); +bool ifPairOfBrackets(char const openingBracket, char const closingBracket); bool ifBracketSequenceIsCorrect(string const & str) { @@ -14,7 +14,11 @@ bool ifBracketSequenceIsCorrect(string const & str) { if (s.isEmpty()) { - if (!ifOpeningBracket(s, str[i])) + if (ifOpeningBracket(str[i])) + { + s.push(str[i]); + } + else { return false; } @@ -27,7 +31,11 @@ bool ifBracketSequenceIsCorrect(string const & str) { return false; } - if (!ifPairOfBrackets(s, str[i])) + if (ifPairOfBrackets(s.peek(), str[i])) + { + s.pop(); + } + else { return false; } @@ -57,11 +65,10 @@ bool ifClosingBracket(char const symbol) return (symbol == ')' || symbol == ']' || symbol == '}'); } -bool ifOpeningBracket(Stack & s, char const symbol) +bool ifOpeningBracket(char const symbol) { if (symbol == '(' || symbol == '[' || symbol == '{') { - s.push(symbol); return true; } else @@ -70,21 +77,18 @@ bool ifOpeningBracket(Stack & s, char const symbol) } } -bool ifPairOfBrackets(Stack & s, char const closingBracket) +bool ifPairOfBrackets(char const openingBracket, char const closingBracket) { - if (s.peek() == '(' && closingBracket == ')') + if (openingBracket == '(' && closingBracket == ')') { - s.pop(); return true; } - if (s.peek() == '[' && closingBracket == ']') + if (openingBracket == '[' && closingBracket == ']') { - s.pop(); return true; } - if (s.peek() == '{' && closingBracket == '}') + if (openingBracket == '{' && closingBracket == '}') { - s.pop(); return true; } return false; From 54c7a52898f8d1731ec5637774f2c56a5f06ea0b Mon Sep 17 00:00:00 2001 From: Victoria Date: Thu, 15 Nov 2018 17:25:47 +0300 Subject: [PATCH 06/12] Deleted useless destructor in Element. Added const to methods in Stack --- .../bracket sequence/Element.cpp | 6 ----- .../bracket sequence/Element.h | 3 +-- .../bracket sequence/Stack.cpp | 25 ++++++++----------- hw6/bracket sequence/bracket sequence/Stack.h | 6 ++--- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/Element.cpp b/hw6/bracket sequence/bracket sequence/Element.cpp index 8bbeb35..cbe3050 100644 --- a/hw6/bracket sequence/bracket sequence/Element.cpp +++ b/hw6/bracket sequence/bracket sequence/Element.cpp @@ -4,10 +4,4 @@ Element::Element(char data = '\0') { this->data = data; this->next = nullptr; -} - -Element::~Element() -{ - this->data = '\0'; - this->next = nullptr; } \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Element.h b/hw6/bracket sequence/bracket sequence/Element.h index f15288d..a17c752 100644 --- a/hw6/bracket sequence/bracket sequence/Element.h +++ b/hw6/bracket sequence/bracket sequence/Element.h @@ -2,8 +2,7 @@ struct Element { - Element(char data); - ~Element(); + explicit Element(char data); char data; Element* next; }; \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp index 000c1f2..5296f4b 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.cpp +++ b/hw6/bracket sequence/bracket sequence/Stack.cpp @@ -29,24 +29,21 @@ void Stack::push(char data) head = newElement; } -int Stack::pop() +bool Stack::pop() { - if (!isEmpty()) + if (isEmpty()) { - auto* temp = head->next; - head->next = nullptr; - delete head; - head = nullptr; - head = temp; - return 0; - } - else - { - return -1; + return false; } + auto* temp = head->next; + head->next = nullptr; + delete head; + head = nullptr; + head = temp; + return true; } -char Stack::peek() +char Stack::peek() const { if (!isEmpty()) { @@ -59,7 +56,7 @@ char Stack::peek() } } -bool Stack::isEmpty() +bool Stack::isEmpty() const { return head == nullptr; } \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Stack.h b/hw6/bracket sequence/bracket sequence/Stack.h index 6b95aff..f7ecaec 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.h +++ b/hw6/bracket sequence/bracket sequence/Stack.h @@ -18,11 +18,11 @@ struct Stack // void push(char data); // - int pop(); + bool pop(); // - char peek(); + char peek() const; // - bool isEmpty(); + bool isEmpty() const; private: Element* head; }; \ No newline at end of file From 9533022af95bfd43e53f0ea4bd361e03ec33eae0 Mon Sep 17 00:00:00 2001 From: Victoria Date: Thu, 15 Nov 2018 20:42:02 +0300 Subject: [PATCH 07/12] fixed some remarks. Made definitions in some functions smaller. Deleted system(pause). Changes definition in method peek a little bit (stack) --- .../bracket sequence/Stack.cpp | 7 ++--- .../bracket sequence/bracket sequence.cpp | 27 ++++--------------- .../bracket sequence/bracket sequence.vcxproj | 3 +++ .../bracket sequence/main.cpp | 1 - 4 files changed, 10 insertions(+), 28 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp index 5296f4b..89a9bd9 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.cpp +++ b/hw6/bracket sequence/bracket sequence/Stack.cpp @@ -49,11 +49,8 @@ char Stack::peek() const { return head->data; } - else - { - cout << "Stack is empty\n"; - return '\0'; - } + cout << "Stack is empty\n"; + return '\0'; } bool Stack::isEmpty() const diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp index 81e44a8..272d405 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp @@ -14,31 +14,21 @@ bool ifBracketSequenceIsCorrect(string const & str) { if (s.isEmpty()) { - if (ifOpeningBracket(str[i])) - { - s.push(str[i]); - } - else + if (!ifOpeningBracket(str[i])) { return false; } + s.push(str[i]); } else { if (ifClosingBracket(str[i])) { - if (ifClosingBracket(s.peek())) - { - return false; - } - if (ifPairOfBrackets(s.peek(), str[i])) - { - s.pop(); - } - else + if (!ifPairOfBrackets(s.peek(), str[i])) { return false; } + s.pop(); } else { @@ -67,14 +57,7 @@ bool ifClosingBracket(char const symbol) bool ifOpeningBracket(char const symbol) { - if (symbol == '(' || symbol == '[' || symbol == '{') - { - return true; - } - else - { - return false; - } + return (symbol == '(' || symbol == '[' || symbol == '{'); } bool ifPairOfBrackets(char const openingBracket, char const closingBracket) diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj index 36d750f..7bbd539 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -77,6 +77,9 @@ true true + + Console + diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index 1a9b5f9..45c6209 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -18,6 +18,5 @@ int main() bool result = ifBracketSequenceIsCorrect(s); cout << "res is " << result << endl; - system("pause"); return 0; } \ No newline at end of file From 41f713d939a4010129f75a68eea0c7814443cdce Mon Sep 17 00:00:00 2001 From: Victoria Date: Thu, 15 Nov 2018 20:53:00 +0300 Subject: [PATCH 08/12] Deted using namespace std in h files. Fixed small remark in ~Stack() --- hw6/bracket sequence/bracket sequence/Element.h | 1 + hw6/bracket sequence/bracket sequence/Stack.cpp | 8 ++++---- hw6/bracket sequence/bracket sequence/Stack.h | 10 ++++------ .../bracket sequence/bracket sequence.h | 3 +-- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/Element.h b/hw6/bracket sequence/bracket sequence/Element.h index a17c752..280ab03 100644 --- a/hw6/bracket sequence/bracket sequence/Element.h +++ b/hw6/bracket sequence/bracket sequence/Element.h @@ -3,6 +3,7 @@ struct Element { explicit Element(char data); + char data; Element* next; }; \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/Stack.cpp b/hw6/bracket sequence/bracket sequence/Stack.cpp index 89a9bd9..1581c4f 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.cpp +++ b/hw6/bracket sequence/bracket sequence/Stack.cpp @@ -1,5 +1,8 @@ #include "Stack.h" #include "Element.h" +#include + +using namespace std; Stack::Stack() { @@ -8,14 +11,12 @@ Stack::Stack() Stack::~Stack() { - auto* temp = head; while (!isEmpty()) { - temp = head; + auto* temp = head; head = temp->next; temp->next = nullptr; delete temp; - temp = nullptr; } } @@ -38,7 +39,6 @@ bool Stack::pop() auto* temp = head->next; head->next = nullptr; delete head; - head = nullptr; head = temp; return true; } diff --git a/hw6/bracket sequence/bracket sequence/Stack.h b/hw6/bracket sequence/bracket sequence/Stack.h index f7ecaec..d2c626d 100644 --- a/hw6/bracket sequence/bracket sequence/Stack.h +++ b/hw6/bracket sequence/bracket sequence/Stack.h @@ -1,7 +1,4 @@ #pragma once -#include - -using namespace std; struct Element; @@ -9,18 +6,19 @@ struct Stack { // Stack(); + // ~Stack(); - // - - // // void push(char data); + // bool pop(); + // char peek() const; + // bool isEmpty() const; private: diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.h b/hw6/bracket sequence/bracket sequence/bracket sequence.h index 94bea4d..1948994 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.h +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.h @@ -1,5 +1,4 @@ #pragma once -#include "Stack.h" #include -bool ifBracketSequenceIsCorrect(string const & str); \ No newline at end of file +bool ifBracketSequenceIsCorrect(std::string const & str); \ No newline at end of file From 4146470ab2d7570f20a5136d281ea855ea7d6078 Mon Sep 17 00:00:00 2001 From: Victoria Date: Thu, 15 Nov 2018 21:12:19 +0300 Subject: [PATCH 09/12] Fixed small bug: added std in Stack.cpp --- hw6/bracket sequence/bracket sequence/bracket sequence.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp index 272d405..9a75c20 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp @@ -1,11 +1,12 @@ #include "bracket sequence.h" +#include "Stack.h" bool isBracket(char const symbol); bool ifClosingBracket(char const symbol); bool ifOpeningBracket(char const symbol); bool ifPairOfBrackets(char const openingBracket, char const closingBracket); -bool ifBracketSequenceIsCorrect(string const & str) +bool ifBracketSequenceIsCorrect(std::string const & str) { Stack s; for (int i = 0; i < str.size(); ++i) From 927b90aca08d969d06221ec5645b5bdee1baa530 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 24 Nov 2018 03:05:18 +0300 Subject: [PATCH 10/12] fixed remark --- hw6/bracket sequence/bracket sequence/bracket sequence.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp index 9a75c20..d3a4286 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.cpp +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.cpp @@ -38,11 +38,7 @@ bool ifBracketSequenceIsCorrect(std::string const & str) } } } - if (!s.isEmpty()) - { - return false; - } - return true; + return s.isEmpty(); } bool isBracket(char const symbol) From 9e723131e744e973a9e8761a184c934e8b2508f8 Mon Sep 17 00:00:00 2001 From: Victoria Date: Tue, 18 Dec 2018 16:29:33 +0300 Subject: [PATCH 11/12] deleted commented code --- hw6/bracket sequence/bracket sequence/main.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index 45c6209..a68b479 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -8,8 +8,6 @@ using namespace std; -// bool ifBracketSequenceIsCorrect(string const & str); - int main() { string s; From 52d86bf7d48356ab2be51beaec3589b51e4109d5 Mon Sep 17 00:00:00 2001 From: Victoria Date: Tue, 18 Dec 2018 16:59:52 +0300 Subject: [PATCH 12/12] added tests --- .../bracket sequence/bracket sequence.vcxproj | 2 + .../bracket sequence.vcxproj.filters | 6 +++ .../bracket sequence/main.cpp | 2 + .../bracket sequence/tests.cpp | 54 +++++++++++++++++++ hw6/bracket sequence/bracket sequence/tests.h | 3 ++ 5 files changed, 67 insertions(+) create mode 100644 hw6/bracket sequence/bracket sequence/tests.cpp create mode 100644 hw6/bracket sequence/bracket sequence/tests.h diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj index 7bbd539..0bf0d59 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj @@ -122,11 +122,13 @@ + + diff --git a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters index d9f5171..38d4aa2 100644 --- a/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters +++ b/hw6/bracket sequence/bracket sequence/bracket sequence.vcxproj.filters @@ -27,6 +27,9 @@ Исходные файлы + + Исходные файлы + @@ -38,5 +41,8 @@ Файлы заголовков + + Файлы заголовков + \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/main.cpp b/hw6/bracket sequence/bracket sequence/main.cpp index a68b479..584fa92 100644 --- a/hw6/bracket sequence/bracket sequence/main.cpp +++ b/hw6/bracket sequence/bracket sequence/main.cpp @@ -2,6 +2,7 @@ #include "bracket sequence.h" #include #include +#include "tests.h" // , // : (), [], {}. ({ ) } , ({}) - . @@ -10,6 +11,7 @@ using namespace std; int main() { + tests(); string s; cout << "Input string" << endl; cin >> s; diff --git a/hw6/bracket sequence/bracket sequence/tests.cpp b/hw6/bracket sequence/bracket sequence/tests.cpp new file mode 100644 index 0000000..1377093 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/tests.cpp @@ -0,0 +1,54 @@ +#include "tests.h" +#include +#include "bracket sequence.h" + +using namespace std; + +bool test1(); + +bool test2(); + +bool test3(); + +void tests() +{ + if (test1()) + { + cout << "Test 1 passed" << endl; + } + else + { + cout << "Test 1 FAILED" << endl; + } + if (test2()) + { + cout << "Test 2 passed" << endl; + } + else + { + cout << "Test 2 FAILED" << endl; + } + if (test3()) + { + cout << "Test 3 passed" << endl; + } + else + { + cout << "Test 3 FAILED" << endl; + } +} + +bool test1() +{ + return !ifBracketSequenceIsCorrect("([])[(])"); +} + +bool test2() +{ + return ifBracketSequenceIsCorrect("()()()[][({})]"); +} + +bool test3() +{ + return !ifBracketSequenceIsCorrect(")()()"); +} \ No newline at end of file diff --git a/hw6/bracket sequence/bracket sequence/tests.h b/hw6/bracket sequence/bracket sequence/tests.h new file mode 100644 index 0000000..19d7933 --- /dev/null +++ b/hw6/bracket sequence/bracket sequence/tests.h @@ -0,0 +1,3 @@ +#pragma once + +void tests(); \ No newline at end of file