From a9d169808d2b3caf64998e11b403c3c667da14c8 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 02:16:38 +0300 Subject: [PATCH 01/11] added stack --- hw6/postfix expression/postfix expression.sln | 31 +++++ .../postfix expression/Element.cpp | 13 ++ .../postfix expression/Element.h | 9 ++ .../postfix expression/Postfix.cpp | 0 .../postfix expression/Postfix.h | 1 + .../postfix expression/Stack.cpp | 65 +++++++++ .../postfix expression/Stack.h | 28 ++++ .../postfix expression/main.cpp | 10 ++ .../postfix expression.vcxproj | 129 ++++++++++++++++++ .../postfix expression.vcxproj.filters | 36 +++++ 10 files changed, 322 insertions(+) create mode 100644 hw6/postfix expression/postfix expression.sln create mode 100644 hw6/postfix expression/postfix expression/Element.cpp create mode 100644 hw6/postfix expression/postfix expression/Element.h create mode 100644 hw6/postfix expression/postfix expression/Postfix.cpp create mode 100644 hw6/postfix expression/postfix expression/Postfix.h create mode 100644 hw6/postfix expression/postfix expression/Stack.cpp create mode 100644 hw6/postfix expression/postfix expression/Stack.h create mode 100644 hw6/postfix expression/postfix expression/main.cpp create mode 100644 hw6/postfix expression/postfix expression/postfix expression.vcxproj create mode 100644 hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters diff --git a/hw6/postfix expression/postfix expression.sln b/hw6/postfix expression/postfix expression.sln new file mode 100644 index 0000000..ed8b52c --- /dev/null +++ b/hw6/postfix expression/postfix expression.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}") = "postfix expression", "postfix expression\postfix expression.vcxproj", "{71E3947C-0B74-4DAA-9648-0CF33E3270DD}" +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 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Debug|x64.ActiveCfg = Debug|x64 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Debug|x64.Build.0 = Debug|x64 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Debug|x86.ActiveCfg = Debug|Win32 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Debug|x86.Build.0 = Debug|Win32 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Release|x64.ActiveCfg = Release|x64 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Release|x64.Build.0 = Release|x64 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Release|x86.ActiveCfg = Release|Win32 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {FA9D9EDC-47CF-4A60-8B4C-25B0FAC8F792} + EndGlobalSection +EndGlobal diff --git a/hw6/postfix expression/postfix expression/Element.cpp b/hw6/postfix expression/postfix expression/Element.cpp new file mode 100644 index 0000000..8bbeb35 --- /dev/null +++ b/hw6/postfix expression/postfix expression/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/postfix expression/postfix expression/Element.h b/hw6/postfix expression/postfix expression/Element.h new file mode 100644 index 0000000..f15288d --- /dev/null +++ b/hw6/postfix expression/postfix expression/Element.h @@ -0,0 +1,9 @@ +#pragma once + +struct Element +{ + Element(char data); + ~Element(); + char data; + Element* next; +}; \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp new file mode 100644 index 0000000..e69de29 diff --git a/hw6/postfix expression/postfix expression/Postfix.h b/hw6/postfix expression/postfix expression/Postfix.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/hw6/postfix expression/postfix expression/Postfix.h @@ -0,0 +1 @@ +#pragma once diff --git a/hw6/postfix expression/postfix expression/Stack.cpp b/hw6/postfix expression/postfix expression/Stack.cpp new file mode 100644 index 0000000..000c1f2 --- /dev/null +++ b/hw6/postfix expression/postfix expression/Stack.cpp @@ -0,0 +1,65 @@ +#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"; + return '\0'; + } +} + +bool Stack::isEmpty() +{ + return head == nullptr; +} \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Stack.h b/hw6/postfix expression/postfix expression/Stack.h new file mode 100644 index 0000000..9a208c5 --- /dev/null +++ b/hw6/postfix expression/postfix expression/Stack.h @@ -0,0 +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/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp new file mode 100644 index 0000000..8672997 --- /dev/null +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -0,0 +1,10 @@ +#include + +using namespace std; + +int main() +{ + + system("pause"); + return 0; +} \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/postfix expression.vcxproj b/hw6/postfix expression/postfix expression/postfix expression.vcxproj new file mode 100644 index 0000000..325b884 --- /dev/null +++ b/hw6/postfix expression/postfix expression/postfix expression.vcxproj @@ -0,0 +1,129 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {71E3947C-0B74-4DAA-9648-0CF33E3270DD} + postfixexpression + 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/postfix expression/postfix expression/postfix expression.vcxproj.filters b/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters new file mode 100644 index 0000000..266570e --- /dev/null +++ b/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {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 From ee776307c4a907c7fa8b79ff0c3b60f0ef887d9d Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 03:11:56 +0300 Subject: [PATCH 02/11] some adds --- .../postfix expression/Postfix.cpp | 54 +++++++++++++++++++ .../postfix expression/Postfix.h | 6 +++ .../postfix expression/main.cpp | 3 ++ .../postfix expression.vcxproj | 2 + .../postfix expression.vcxproj.filters | 6 +++ 5 files changed, 71 insertions(+) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index e69de29..ad02907 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -0,0 +1,54 @@ +#include "Postfix.h" +#include "Stack.h" + +bool isOperator(char symbol); +int performOperation(char symbol, int firstNum, int secondNum); + +int fromPostfixToResult(string const & str) +{ + Stack s; + for (int i = 0; i < str.size(); ++i) + { + if (!isOperator(str[i])) + { + s.push(str[i]); + } + else + { + auto temp2 = s.peek(); + int num2 = atoi(&temp2); + s.pop(); + auto temp1 = s.peek(); + int num1 = atoi(&temp1); + s.pop(); + performOperation(str[i], num1, num2); + } + } + return 0; +} + +bool isOperator(char symbol) +{ + return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'); +} + +int performOperation(char symbol, int firstNum, int secondNum) +{ + int result = 0; + switch (symbol) + { + case '+': + result = firstNum + secondNum; + break; + case '-': + result = firstNum - secondNum; + break; + case '*': + result = firstNum * secondNum; + break; + case '/': + result = firstNum / secondNum; + break; + } + return result; +} \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Postfix.h b/hw6/postfix expression/postfix expression/Postfix.h index 6f70f09..58d16f1 100644 --- a/hw6/postfix expression/postfix expression/Postfix.h +++ b/hw6/postfix expression/postfix expression/Postfix.h @@ -1 +1,7 @@ #pragma once +#include +#include + +using namespace std; + +int fromPostfixToResult(string const & str); \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index 8672997..6228439 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -4,6 +4,9 @@ using namespace std; int main() { + char num = '9'; + int res = atoi(&num); + cout << res << endl; system("pause"); return 0; diff --git a/hw6/postfix expression/postfix expression/postfix expression.vcxproj b/hw6/postfix expression/postfix expression/postfix expression.vcxproj index 325b884..4b1ce2c 100644 --- a/hw6/postfix expression/postfix expression/postfix expression.vcxproj +++ b/hw6/postfix expression/postfix expression/postfix expression.vcxproj @@ -117,10 +117,12 @@ + + diff --git a/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters b/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters index 266570e..2dd9c80 100644 --- a/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters +++ b/hw6/postfix expression/postfix expression/postfix expression.vcxproj.filters @@ -24,6 +24,9 @@ Исходные файлы + + Исходные файлы + @@ -32,5 +35,8 @@ Файлы заголовков + + Файлы заголовков + \ No newline at end of file From ded9b07de37fc2256215ee20ef49dbc8ae2ff478 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 03:17:19 +0300 Subject: [PATCH 03/11] trying to fix bug --- hw6/postfix expression/postfix expression/Postfix.cpp | 5 +++-- hw6/postfix expression/postfix expression/main.cpp | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index ad02907..44e290c 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -21,10 +21,11 @@ int fromPostfixToResult(string const & str) auto temp1 = s.peek(); int num1 = atoi(&temp1); s.pop(); - performOperation(str[i], num1, num2); + int resultOfOperation = performOperation(str[i], num1, num2); + s.push(resultOfOperation); } } - return 0; + return str[str.size() - 1]; } bool isOperator(char symbol) diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index 6228439..6a419f0 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -1,12 +1,16 @@ #include +#include +#include "Postfix.h" using namespace std; int main() { - char num = '9'; - int res = atoi(&num); - cout << res << endl; + string str; + cout << "Input string" << endl; + cin >> str; + cout << "res is " << fromPostfixToResult(str) << endl; + system("pause"); return 0; From 53c580f04a06e16e1da9cc3f47bbf268fe752960 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 03:27:34 +0300 Subject: [PATCH 04/11] function seems ready --- hw6/postfix expression/postfix expression/Postfix.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index 44e290c..a7eb2c0 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -7,7 +7,8 @@ int performOperation(char symbol, int firstNum, int secondNum); int fromPostfixToResult(string const & str) { Stack s; - for (int i = 0; i < str.size(); ++i) + int sizeOfStr = (int)str.size(); + for (int i = 0; i < sizeOfStr; ++i) { if (!isOperator(str[i])) { @@ -22,10 +23,13 @@ int fromPostfixToResult(string const & str) int num1 = atoi(&temp1); s.pop(); int resultOfOperation = performOperation(str[i], num1, num2); - s.push(resultOfOperation); + char resOperation = resultOfOperation + '0'; + s.push(resOperation); } } - return str[str.size() - 1]; + auto tempRes = s.peek(); + int result = atoi(&tempRes); + return result; } bool isOperator(char symbol) From a5b64dcc1b726b756f53c18afbe0baab50dd414e Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 03:42:25 +0300 Subject: [PATCH 05/11] finished --- hw6/postfix expression/postfix expression/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index 6a419f0..cf394ff 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -9,7 +9,7 @@ int main() string str; cout << "Input string" << endl; cin >> str; - cout << "res is " << fromPostfixToResult(str) << endl; + cout << "result is " << fromPostfixToResult(str) << endl; system("pause"); From c897a3c4d7b7955fab50237f135662c5529d9123 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 27 Oct 2018 19:33:49 +0300 Subject: [PATCH 06/11] fixed bug --- .../postfix expression/Element.cpp | 4 ++-- .../postfix expression/Element.h | 4 ++-- .../postfix expression/Postfix.cpp | 16 +++++++--------- .../postfix expression/Stack.cpp | 6 +++--- .../postfix expression/Stack.h | 4 ++-- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Element.cpp b/hw6/postfix expression/postfix expression/Element.cpp index 8bbeb35..9980f55 100644 --- a/hw6/postfix expression/postfix expression/Element.cpp +++ b/hw6/postfix expression/postfix expression/Element.cpp @@ -1,6 +1,6 @@ #include "Element.h" -Element::Element(char data = '\0') +Element::Element(int data = 0) { this->data = data; this->next = nullptr; @@ -8,6 +8,6 @@ Element::Element(char data = '\0') Element::~Element() { - this->data = '\0'; + this->data = 0; this->next = nullptr; } \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Element.h b/hw6/postfix expression/postfix expression/Element.h index f15288d..b03da05 100644 --- a/hw6/postfix expression/postfix expression/Element.h +++ b/hw6/postfix expression/postfix expression/Element.h @@ -2,8 +2,8 @@ struct Element { - Element(char data); + Element(int data); ~Element(); - char data; + int data; Element* next; }; \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index a7eb2c0..bb4d067 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -12,23 +12,21 @@ int fromPostfixToResult(string const & str) { if (!isOperator(str[i])) { - s.push(str[i]); + char number = str[i]; + int numberInInt = atoi(&number); + s.push(numberInInt); } else { - auto temp2 = s.peek(); - int num2 = atoi(&temp2); + int num2 = s.peek(); s.pop(); - auto temp1 = s.peek(); - int num1 = atoi(&temp1); + int num1 = s.peek(); s.pop(); int resultOfOperation = performOperation(str[i], num1, num2); - char resOperation = resultOfOperation + '0'; - s.push(resOperation); + s.push(resultOfOperation); } } - auto tempRes = s.peek(); - int result = atoi(&tempRes); + int result = s.peek(); return result; } diff --git a/hw6/postfix expression/postfix expression/Stack.cpp b/hw6/postfix expression/postfix expression/Stack.cpp index 000c1f2..b27e80e 100644 --- a/hw6/postfix expression/postfix expression/Stack.cpp +++ b/hw6/postfix expression/postfix expression/Stack.cpp @@ -19,7 +19,7 @@ Stack::~Stack() } } -void Stack::push(char data) +void Stack::push(int data) { auto* newElement = new Element(data); if (!isEmpty()) @@ -46,7 +46,7 @@ int Stack::pop() } } -char Stack::peek() +int Stack::peek() { if (!isEmpty()) { @@ -55,7 +55,7 @@ char Stack::peek() else { cout << "Stack is empty\n"; - return '\0'; + return -1; } } diff --git a/hw6/postfix expression/postfix expression/Stack.h b/hw6/postfix expression/postfix expression/Stack.h index 9a208c5..3e9290a 100644 --- a/hw6/postfix expression/postfix expression/Stack.h +++ b/hw6/postfix expression/postfix expression/Stack.h @@ -16,11 +16,11 @@ struct Stack // // - void push(char data); + void push(int data); // int pop(); // - char peek(); + int peek(); // bool isEmpty(); private: From 3faef6e4e2494cde8f8d7136532aec193a290e97 Mon Sep 17 00:00:00 2001 From: Victoria Date: Mon, 12 Nov 2018 00:42:23 +0300 Subject: [PATCH 07/11] fixed all remarks --- .../postfix expression/Element.cpp | 6 ---- .../postfix expression/Element.h | 3 +- .../postfix expression/Postfix.cpp | 31 ++++++++++++++++--- .../postfix expression/Postfix.h | 5 +-- .../postfix expression/Stack.cpp | 31 +++++++++---------- .../postfix expression/Stack.h | 16 +++++----- .../postfix expression/main.cpp | 2 -- .../postfix expression.vcxproj | 3 ++ 8 files changed, 52 insertions(+), 45 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Element.cpp b/hw6/postfix expression/postfix expression/Element.cpp index 9980f55..48bbe6e 100644 --- a/hw6/postfix expression/postfix expression/Element.cpp +++ b/hw6/postfix expression/postfix expression/Element.cpp @@ -4,10 +4,4 @@ Element::Element(int 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/postfix expression/postfix expression/Element.h b/hw6/postfix expression/postfix expression/Element.h index b03da05..a045f9c 100644 --- a/hw6/postfix expression/postfix expression/Element.h +++ b/hw6/postfix expression/postfix expression/Element.h @@ -2,8 +2,7 @@ struct Element { - Element(int data); - ~Element(); + explicit Element(int data); int data; Element* next; }; \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index bb4d067..72bb55d 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -1,14 +1,18 @@ #include "Postfix.h" #include "Stack.h" +#include + +using namespace std; bool isOperator(char symbol); + int performOperation(char symbol, int firstNum, int secondNum); int fromPostfixToResult(string const & str) { Stack s; - int sizeOfStr = (int)str.size(); - for (int i = 0; i < sizeOfStr; ++i) + size_t sizeOfStr = str.size(); + for (size_t i = 0; i < sizeOfStr; ++i) { if (!isOperator(str[i])) { @@ -16,18 +20,32 @@ int fromPostfixToResult(string const & str) int numberInInt = atoi(&number); s.push(numberInInt); } - else + else if (!isOperator(str[i])) { + if (s.peek() == -1) + { + cout << "Incorrect expression" << endl; + return -1; + } int num2 = s.peek(); s.pop(); + if (s.peek() == -1) + { + cout << "Incorrect expression" << endl; + return -1; + } int num1 = s.peek(); s.pop(); int resultOfOperation = performOperation(str[i], num1, num2); s.push(resultOfOperation); } + else + { + cout << "Incorrect expression" << endl; + return -1; + } } - int result = s.peek(); - return result; + return s.peek(); } bool isOperator(char symbol) @@ -52,6 +70,9 @@ int performOperation(char symbol, int firstNum, int secondNum) case '/': result = firstNum / secondNum; break; + default: + cout << "Incorrect expression" << endl; + return -1; } return result; } \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Postfix.h b/hw6/postfix expression/postfix expression/Postfix.h index 58d16f1..09a5915 100644 --- a/hw6/postfix expression/postfix expression/Postfix.h +++ b/hw6/postfix expression/postfix expression/Postfix.h @@ -1,7 +1,4 @@ #pragma once -#include #include -using namespace std; - -int fromPostfixToResult(string const & str); \ No newline at end of file +int fromPostfixToResult(std::string const & str); \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Stack.cpp b/hw6/postfix expression/postfix expression/Stack.cpp index b27e80e..330a42e 100644 --- a/hw6/postfix expression/postfix expression/Stack.cpp +++ b/hw6/postfix expression/postfix expression/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; } } @@ -29,24 +30,20 @@ void Stack::push(int data) head = newElement; } -int Stack::pop() +bool Stack::pop() { - if (!isEmpty()) - { - auto* temp = head->next; - head->next = nullptr; - delete head; - head = nullptr; - head = temp; - return 0; - } - else + if (isEmpty()) { - return -1; + return false; } + auto* temp = head->next; + head->next = nullptr; + delete head; + head = temp; + return true; } -int Stack::peek() +int Stack::peek() const { if (!isEmpty()) { @@ -59,7 +56,7 @@ int Stack::peek() } } -bool Stack::isEmpty() +bool Stack::isEmpty() const { return head == nullptr; } \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Stack.h b/hw6/postfix expression/postfix expression/Stack.h index 3e9290a..a2deb73 100644 --- a/hw6/postfix expression/postfix expression/Stack.h +++ b/hw6/postfix expression/postfix expression/Stack.h @@ -1,7 +1,4 @@ #pragma once -#include - -using namespace std; struct Element; @@ -9,20 +6,21 @@ struct Stack { // Stack(); + // ~Stack(); - // - - // // void push(int data); + // - int pop(); + bool pop(); + // - int peek(); + int peek() const; + // - bool isEmpty(); + bool isEmpty() const; private: Element * head; }; \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index cf394ff..df0bd4b 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -11,7 +11,5 @@ int main() cin >> str; cout << "result is " << fromPostfixToResult(str) << endl; - - system("pause"); return 0; } \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/postfix expression.vcxproj b/hw6/postfix expression/postfix expression/postfix expression.vcxproj index 4b1ce2c..544b7e1 100644 --- a/hw6/postfix expression/postfix expression/postfix expression.vcxproj +++ b/hw6/postfix expression/postfix expression/postfix expression.vcxproj @@ -77,6 +77,9 @@ true true + + Console + From cc357d22f620a80e92ccf9f0f28edbde218e2cf3 Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 24 Nov 2018 02:24:36 +0300 Subject: [PATCH 08/11] fixed bug and all remarks --- .../postfix expression/Postfix.cpp | 30 ++++++++++++------- .../postfix expression/Postfix.h | 3 +- .../postfix expression/Stack.cpp | 14 +++++---- .../postfix expression/Stack.h | 3 +- .../postfix expression/main.cpp | 11 ++++++- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index 72bb55d..8f467db 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -8,10 +8,10 @@ bool isOperator(char symbol); int performOperation(char symbol, int firstNum, int secondNum); -int fromPostfixToResult(string const & str) +pair & fromPostfixToResult(string const & str, pair & p) { Stack s; - size_t sizeOfStr = str.size(); + size_t const sizeOfStr = str.size(); for (size_t i = 0; i < sizeOfStr; ++i) { if (!isOperator(str[i])) @@ -20,32 +20,40 @@ int fromPostfixToResult(string const & str) int numberInInt = atoi(&number); s.push(numberInInt); } - else if (!isOperator(str[i])) + else if (isOperator(str[i])) { - if (s.peek() == -1) + if (!s.peek(p).first) { + p.first = false; cout << "Incorrect expression" << endl; - return -1; + p.second = -1; + return p; } - int num2 = s.peek(); + int num2 = s.peek(p).second; s.pop(); - if (s.peek() == -1) + if (!s.peek(p).first) { + p.first = false; cout << "Incorrect expression" << endl; - return -1; + p.second = -1; + return p; } - int num1 = s.peek(); + int num1 = s.peek(p).second; s.pop(); int resultOfOperation = performOperation(str[i], num1, num2); s.push(resultOfOperation); } else { + p.first = false; cout << "Incorrect expression" << endl; - return -1; + p.second = -1; + return p; } } - return s.peek(); + p.first = s.peek(p).first; + p.second = s.peek(p).second; + return p; } bool isOperator(char symbol) diff --git a/hw6/postfix expression/postfix expression/Postfix.h b/hw6/postfix expression/postfix expression/Postfix.h index 09a5915..c091ae4 100644 --- a/hw6/postfix expression/postfix expression/Postfix.h +++ b/hw6/postfix expression/postfix expression/Postfix.h @@ -1,4 +1,5 @@ #pragma once #include +#include -int fromPostfixToResult(std::string const & str); \ No newline at end of file +std::pair & fromPostfixToResult(std::string const & str, std::pair & p); \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/Stack.cpp b/hw6/postfix expression/postfix expression/Stack.cpp index 330a42e..5fab723 100644 --- a/hw6/postfix expression/postfix expression/Stack.cpp +++ b/hw6/postfix expression/postfix expression/Stack.cpp @@ -43,17 +43,21 @@ bool Stack::pop() return true; } -int Stack::peek() const +pair & Stack::peek(pair & p) const { - if (!isEmpty()) + p.first = head->data; + if (isEmpty()) { - return head->data; + p.first = false; + cout << "Stack is empty" << endl; + p.second = -1; } else { - cout << "Stack is empty\n"; - return -1; + p.first = true; + p.second = head->data; } + return p; } bool Stack::isEmpty() const diff --git a/hw6/postfix expression/postfix expression/Stack.h b/hw6/postfix expression/postfix expression/Stack.h index a2deb73..ea5254e 100644 --- a/hw6/postfix expression/postfix expression/Stack.h +++ b/hw6/postfix expression/postfix expression/Stack.h @@ -1,4 +1,5 @@ #pragma once +#include struct Element; @@ -17,7 +18,7 @@ struct Stack bool pop(); // - int peek() const; + std::pair & peek(std::pair & p) const; // bool isEmpty() const; diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index df0bd4b..61411ae 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -1,15 +1,24 @@ #include #include #include "Postfix.h" +#include using namespace std; int main() { string str; + pair p; cout << "Input string" << endl; cin >> str; - cout << "result is " << fromPostfixToResult(str) << endl; + if (!fromPostfixToResult(str, p).first) + { + cout << "Incorrect expression" << endl; + } + else + { + cout << "result is " << fromPostfixToResult(str, p).second << endl; + } return 0; } \ No newline at end of file From cbe7d2a827ab88e5505aea7c42a7abad34fd281c Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 24 Nov 2018 03:00:10 +0300 Subject: [PATCH 09/11] deleted copypaste --- .../postfix expression/Postfix.cpp | 25 ++++++++++--------- .../postfix expression/Stack.cpp | 3 +-- .../postfix expression/main.cpp | 9 +------ 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index 8f467db..2516d34 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -8,6 +8,8 @@ bool isOperator(char symbol); int performOperation(char symbol, int firstNum, int secondNum); +pair & error(pair & p); + pair & fromPostfixToResult(string const & str, pair & p) { Stack s; @@ -24,19 +26,13 @@ pair & fromPostfixToResult(string const & str, pair & p) { if (!s.peek(p).first) { - p.first = false; - cout << "Incorrect expression" << endl; - p.second = -1; - return p; + return error(p); } int num2 = s.peek(p).second; s.pop(); if (!s.peek(p).first) { - p.first = false; - cout << "Incorrect expression" << endl; - p.second = -1; - return p; + return error(p); } int num1 = s.peek(p).second; s.pop(); @@ -45,10 +41,7 @@ pair & fromPostfixToResult(string const & str, pair & p) } else { - p.first = false; - cout << "Incorrect expression" << endl; - p.second = -1; - return p; + return error(p); } } p.first = s.peek(p).first; @@ -56,6 +49,14 @@ pair & fromPostfixToResult(string const & str, pair & p) return p; } +pair & error(pair & p) +{ + p.first = false; + cout << "Incorrect expression" << endl; + p.second = -1; + return p; +} + bool isOperator(char symbol) { return (symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/'); diff --git a/hw6/postfix expression/postfix expression/Stack.cpp b/hw6/postfix expression/postfix expression/Stack.cpp index 5fab723..72bab60 100644 --- a/hw6/postfix expression/postfix expression/Stack.cpp +++ b/hw6/postfix expression/postfix expression/Stack.cpp @@ -43,9 +43,8 @@ bool Stack::pop() return true; } -pair & Stack::peek(pair & p) const +pair Stack::peek(pair & p) const { - p.first = head->data; if (isEmpty()) { p.first = false; diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index 61411ae..9e6661c 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -11,14 +11,7 @@ int main() pair p; cout << "Input string" << endl; cin >> str; - if (!fromPostfixToResult(str, p).first) - { - cout << "Incorrect expression" << endl; - } - else - { - cout << "result is " << fromPostfixToResult(str, p).second << endl; - } + cout << "result is " << fromPostfixToResult(str, p).second << endl; return 0; } \ No newline at end of file From f45049280a21073003a4caa4beb2c117d9fc911b Mon Sep 17 00:00:00 2001 From: Victoria Date: Sat, 24 Nov 2018 03:00:40 +0300 Subject: [PATCH 10/11] deleted & --- hw6/postfix expression/postfix expression/Stack.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw6/postfix expression/postfix expression/Stack.h b/hw6/postfix expression/postfix expression/Stack.h index ea5254e..bfe2fec 100644 --- a/hw6/postfix expression/postfix expression/Stack.h +++ b/hw6/postfix expression/postfix expression/Stack.h @@ -18,7 +18,7 @@ struct Stack bool pop(); // - std::pair & peek(std::pair & p) const; + std::pair peek(std::pair & p) const; // bool isEmpty() const; From 4ebb22bc7d1fb1a3e68298c6b27200ec46db115d Mon Sep 17 00:00:00 2001 From: Victoria Date: Tue, 18 Dec 2018 16:23:26 +0300 Subject: [PATCH 11/11] adds --- hw6/postfix expression/postfix expression/Postfix.cpp | 7 +++++++ hw6/postfix expression/postfix expression/Postfix.h | 2 +- hw6/postfix expression/postfix expression/main.cpp | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hw6/postfix expression/postfix expression/Postfix.cpp b/hw6/postfix expression/postfix expression/Postfix.cpp index 2516d34..d0a34c0 100644 --- a/hw6/postfix expression/postfix expression/Postfix.cpp +++ b/hw6/postfix expression/postfix expression/Postfix.cpp @@ -18,6 +18,13 @@ pair & fromPostfixToResult(string const & str, pair & p) { if (!isOperator(str[i])) { + if (!isdigit(str[i])) + { + cout << "Incorrect input" << endl; + p.first = false; + p.second = -1; + return p; + } char number = str[i]; int numberInInt = atoi(&number); s.push(numberInInt); diff --git a/hw6/postfix expression/postfix expression/Postfix.h b/hw6/postfix expression/postfix expression/Postfix.h index c091ae4..9eca970 100644 --- a/hw6/postfix expression/postfix expression/Postfix.h +++ b/hw6/postfix expression/postfix expression/Postfix.h @@ -2,4 +2,4 @@ #include #include -std::pair & fromPostfixToResult(std::string const & str, std::pair & p); \ No newline at end of file +std::pair & fromPostfixToResult(std::string const & str, std::pair & p); \ No newline at end of file diff --git a/hw6/postfix expression/postfix expression/main.cpp b/hw6/postfix expression/postfix expression/main.cpp index 9e6661c..b96fa6e 100644 --- a/hw6/postfix expression/postfix expression/main.cpp +++ b/hw6/postfix expression/postfix expression/main.cpp @@ -8,7 +8,7 @@ using namespace std; int main() { string str; - pair p; + pair p; cout << "Input string" << endl; cin >> str; cout << "result is " << fromPostfixToResult(str, p).second << endl;