diff --git a/GoogleTest/test-calc.cpp b/GoogleTest/test-calc.cpp new file mode 100644 index 0000000..321cac1 --- /dev/null +++ b/GoogleTest/test-calc.cpp @@ -0,0 +1,199 @@ +#include "gtest.h" +#include "..\mp2-lab3-stack\TCalc.h" +#include + +TEST(TCalc, check_brackets_1_true) +{ + TCalc tmp; + tmp.SetInfix("()"); + EXPECT_EQ(true, tmp.CheckBrackets()); +} + +TEST(TCalc, check_brackets_2_true) +{ + TCalc tmp; + tmp.SetInfix("(())"); + EXPECT_EQ(true, tmp.CheckBrackets()); +} + +TEST(TCalc, check_brackets_3_true) +{ + TCalc tmp; + tmp.SetInfix("()()"); + EXPECT_EQ(true, tmp.CheckBrackets()); +} + +TEST(TCalc, check_brackets_4_true) +{ + TCalc tmp; + tmp.SetInfix("(()())"); + EXPECT_EQ(true, tmp.CheckBrackets()); +} + +TEST(TCalc, check_brakets_1_false) +{ + TCalc tmp; + tmp.SetInfix("("); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalc, check_brakets_2_false) +{ + TCalc tmp; + tmp.SetInfix(")"); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalc, check_brakets_3_false) +{ + TCalc tmp; + tmp.SetInfix("(()"); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalc, check_brakets_4_false) +{ + TCalc tmp; + tmp.SetInfix("())"); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalc, check_brakets_5_false) +{ + TCalc tmp; + tmp.SetInfix("()("); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalc, check_brakets_6_false) +{ + TCalc tmp; + tmp.SetInfix("((()"); + EXPECT_EQ(false, tmp.CheckBrackets()); +} +TEST(TCalculator, check_brakets_7_false) +{ + TCalc tmp; + tmp.SetInfix("(()))"); + EXPECT_EQ(false, tmp.CheckBrackets()); +} + +TEST(TCalc, check_empty_string) +{ + TCalc tmp; + EXPECT_ANY_THROW(tmp.CheckBrackets()); +} +TEST(TCalc, simple_example_plus) +{ + TCalc tmp; + tmp.SetInfix("2+2"); + tmp.ToPostFix(); + EXPECT_EQ("2 2+", tmp.GetPostfix()); + EXPECT_EQ(4, tmp.Calc()); +} + +TEST(TCalc, simple_example_minus) +{ + TCalc tmp; + tmp.SetInfix("2-2"); + tmp.ToPostFix(); + EXPECT_EQ("2 2-", tmp.GetPostfix()); + EXPECT_EQ(0, tmp.Calc()); +} +TEST(TCalc, simple_example_multiplication) +{ + TCalc tmp; + tmp.SetInfix("2*3"); + tmp.ToPostFix(); + EXPECT_EQ("2 3*", tmp.GetPostfix()); + EXPECT_EQ(6, tmp.Calc()); +} +TEST(TCalc, simple_example_division) +{ + TCalc tmp; + tmp.SetInfix("2/4"); + tmp.ToPostFix(); + EXPECT_EQ("2 4/", tmp.GetPostfix()); + EXPECT_EQ(0.5, tmp.Calc()); + +} +TEST(TCalc, simple_example_degree) +{ + TCalc tmp; + tmp.SetInfix("2^3"); + tmp.ToPostFix(); + EXPECT_EQ("2 3^", tmp.GetPostfix()); + EXPECT_EQ(8, tmp.Calc()); +} +TEST(TCalc, example_1) +{ + TCalc tmp; + tmp.SetInfix("(2+2)*3"); + tmp.ToPostFix(); + EXPECT_EQ("2 2+ 3*", tmp.GetPostfix()); + EXPECT_EQ(12, tmp.Calc()); +} + +TEST(TCalc, example_2) +{ + TCalc tmp; + tmp.SetInfix("2/(2+2)+2"); + tmp.ToPostFix(); + EXPECT_EQ("2 2 2+ /2+", tmp.GetPostfix()); + EXPECT_EQ(2.5, tmp.Calc()); +} +TEST(TCalc, example_3) +{ + TCalc tmp; + tmp.SetInfix("1/(1+1)^3+2"); + tmp.ToPostFix(); + EXPECT_EQ("1 1 1+ 3 ^/2+", tmp.GetPostfix()); + EXPECT_EQ(2.125, tmp.Calc()); +} + +TEST(TCalc, example_4) +{ + TCalc tmp; + tmp.SetInfix("(5+7)/40*2^5"); + tmp.ToPostFix(); + EXPECT_EQ("5 7+ 40 /2 5^*", tmp.GetPostfix()); + EXPECT_EQ(9.6, tmp.Calc()); +} +TEST(TCalc, example_5) +{ + TCalc tmp; + tmp.SetInfix("256-12^2/2"); + tmp.ToPostFix(); + EXPECT_EQ("256 12 2 ^2/-", tmp.GetPostfix()); + EXPECT_EQ(184, tmp.Calc()); +} +TEST(TCalc, test_sin) +{ + TCalc tmp; + bool check; + tmp.SetInfix("sin(30)"); + tmp.ToPostFix(); + if (abs(0.5 - tmp.Calc()) < pow(10, -10)) + check = true; + else + check = false; + EXPECT_EQ("30s", tmp.GetPostfix()); + EXPECT_EQ(true, check); +} + +TEST(TCalcr, test_cos) +{ + TCalc tmp; + bool check; + tmp.SetInfix("cos(60)"); + tmp.ToPostFix(); + if (abs(0.5 - tmp.Calc()) < pow(10, -10)) + check = true; + else + check = false; + EXPECT_EQ("60c", tmp.GetPostfix()); + EXPECT_EQ(true, check); +} + +TEST(TCalc, error_symbol) +{ + TCalc tmp; + tmp.SetInfix("son(35)"); + EXPECT_ANY_THROW(tmp.ToPostFix()); + +} \ No newline at end of file diff --git a/GoogleTest/test_tstack.cpp b/GoogleTest/test_tstack.cpp index 63254fe..9f60240 100644 --- a/GoogleTest/test_tstack.cpp +++ b/GoogleTest/test_tstack.cpp @@ -81,4 +81,5 @@ TEST(TStack, can_clear_stack) TS.clear(); ASSERT_TRUE(TS.IsEmpty()); + system("pause"); } \ No newline at end of file diff --git a/Graphics/Project3/MyForm.h b/Graphics/Project3/MyForm.h index d39d54b..28d4f77 100644 --- a/Graphics/Project3/MyForm.h +++ b/Graphics/Project3/MyForm.h @@ -82,6 +82,8 @@ namespace Project3 { // // textBox1 // + this->textBox1->Font = (gcnew System::Drawing::Font(L"MV Boli", 12, static_cast((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), + System::Drawing::GraphicsUnit::Point, static_cast(0))); this->textBox1->Location = System::Drawing::Point(12, 28); this->textBox1->Multiline = true; this->textBox1->Name = L"textBox1"; @@ -92,9 +94,11 @@ namespace Project3 { // label1 // this->label1->AutoSize = true; + this->label1->Font = (gcnew System::Drawing::Font(L"MV Boli", 12, static_cast((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)), + System::Drawing::GraphicsUnit::Point, static_cast(0))); this->label1->Location = System::Drawing::Point(443, 36); this->label1->Name = L"label1"; - this->label1->Size = System::Drawing::Size(34, 13); + this->label1->Size = System::Drawing::Size(53, 21); this->label1->TabIndex = 4; this->label1->Text = L"Solve"; this->label1->Click += gcnew System::EventHandler(this, &MyForm::label1_Click); @@ -103,7 +107,7 @@ namespace Project3 { // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; - this->ClientSize = System::Drawing::Size(596, 192); + this->ClientSize = System::Drawing::Size(569, 128); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Controls->Add(this->button1); @@ -124,7 +128,7 @@ namespace Project3 { calc.SetInfix(infix); calc.ToPostFix(); double res = calc.Calc(); - textBox1->Text = Convert::ToString(res); + label1->Text = Convert::ToString(res); } private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) { } diff --git a/Graphics/Project3/MyForm.resx b/Graphics/Project3/MyForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Graphics/Project3/MyForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/mp2-lab3-stack/TCalc.cpp b/mp2-lab3-stack/TCalc.cpp index 3784d74..4637248 100644 --- a/mp2-lab3-stack/TCalc.cpp +++ b/mp2-lab3-stack/TCalc.cpp @@ -1,6 +1,9 @@ #include "TCalc.h" #include #include +#define _USE_MATH_DEFINES +#include + using namespace std; int TCalc::GetPriority(char m) { @@ -34,6 +37,19 @@ void TCalc::ToPostFix() { while (GetPriority(tmp[i]) <= GetPriority(stop.Top()))postfix += stop.Pop(); stop.Push(tmp[i]); } + if (tmp[i] == 's' || tmp[i] == 'c' || tmp[i] == 't') + { + if (i == tmp.find("sin") || i == tmp.find("cos") || i == tmp.find("tg")) + { + stop.Push(tmp[i]); + if (tmp[i] == 's' || tmp[i] == 'c') + i += 2; + else + i++; + } + else + throw "Error symbol"; + } } } @@ -46,6 +62,30 @@ string TCalc::GetPostfix() { return postfix; } +bool TCalc::CheckBrackets() +{ + TStack bracket(infix.length()); + if (infix == "") + throw 'a'; + else + { + for (int i = 0; i < infix.length(); i++) + if (infix[i] == '(') + bracket.Push(infix[i]); + else if (infix[i] == ')') + { + if (bracket.IsEmpty()) + return false; + else + bracket.Pop(); + } + if (bracket.IsEmpty()) + return true; + else + return false; + } +} + double TCalc::Calc() { double num1, num2, res; stnum.clear(); @@ -76,6 +116,24 @@ double TCalc::Calc() { int l = p - &postfix[i]; i += l - 1; } + if (postfix[i] == 's' || postfix[i] == 'c' || postfix[i] == 't') + { + switch (postfix[i]) + { + case 's': + res = sin(stnum.Pop()*M_PI / 180); + stnum.Push(res); + break; + case 'c': + res = cos(stnum.Pop()*M_PI / 180); + stnum.Push(res); + break; + case 't': + res = tan(stnum.Pop()*M_PI / 180); + stnum.Push(res); + break; + } + } } TStack tmp(stnum); tmp.Pop(); diff --git a/mp2-lab3-stack/TCalc.h b/mp2-lab3-stack/TCalc.h index 4e9e9d1..3a3ffba 100644 --- a/mp2-lab3-stack/TCalc.h +++ b/mp2-lab3-stack/TCalc.h @@ -1,6 +1,8 @@ #pragma once #include "TStack.h" #include +#include +#define _USE_MATH_DEFINES class TCalc { std::string infix; @@ -13,4 +15,5 @@ class TCalc { void SetInfix(std::string inf); std::string GetPostfix(); double Calc(); + bool CheckBrackets(); }; \ No newline at end of file