From 8912e381845eab7c855edcecfbd6b71f8ef6d1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A3=D1=81=D0=BE=D0=B2=D0=B0=20=D0=9C=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B5=D0=B2=D0=BD?= =?UTF-8?q?=D0=B0?= Date: Mon, 27 Mar 2017 20:34:29 +0400 Subject: [PATCH 1/2] Realisation --- app/main.cpp | 4 +-- include/add.h | 6 ---- include/min_way.h | 7 +++++ src/add.cpp | 3 -- src/min_way.cpp | 64 +++++++++++++++++++++++++++++++++++++++++++ test/test_add.cpp | 7 ----- test/test_min_way.cpp | 6 ++++ 7 files changed, 79 insertions(+), 18 deletions(-) delete mode 100644 include/add.h create mode 100644 include/min_way.h delete mode 100644 src/add.cpp create mode 100644 src/min_way.cpp delete mode 100644 test/test_add.cpp create mode 100644 test/test_min_way.cpp diff --git a/app/main.cpp b/app/main.cpp index 4485790..798865a 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,6 +1,6 @@ -#include "add.h" +#include "min_way.h" #include int main() { - std::cout << "2 + 2 = " << add(2, 2) << std::endl; } + diff --git a/include/add.h b/include/add.h deleted file mode 100644 index ebb1c94..0000000 --- a/include/add.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef INCLUDE_ADD_H_ -#define INCLUDE_ADD_H_ - -int add(int x, int y); - -#endif // INCLUDE_ADD_H_ diff --git a/include/min_way.h b/include/min_way.h new file mode 100644 index 0000000..9ded219 --- /dev/null +++ b/include/min_way.h @@ -0,0 +1,7 @@ +#ifndef INCLUDE_MIN_WAY_H_ +#define INCLUDE_MIN_WAY_H_ + +char* way(int x, int y); +bool test (); + +#endif // INCLUDE_MIN_WAY_H_ diff --git a/src/add.cpp b/src/add.cpp deleted file mode 100644 index 35bf82f..0000000 --- a/src/add.cpp +++ /dev/null @@ -1,3 +0,0 @@ -int add(int x, int y) { - return x + y; -} diff --git a/src/min_way.cpp b/src/min_way.cpp new file mode 100644 index 0000000..6a6d3ae --- /dev/null +++ b/src/min_way.cpp @@ -0,0 +1,64 @@ +#include "min_way.h" +#include +#include +#include + +char* way(int x, int y) { + char* a = new char [100]; + int x0 = 0, y0 = 0; + int step = 1; + if ((x == 0) && (y == 0)) { + throw std::logic_error("Error description\n"); + } + while (x0 <= x) { + x += step; + a[step] = 'E'; + step++; + y += step; + a[step] = 'N'; + step++; + } + while (x0 != x) { + if (x < x0) { + x += step; + a[step] = 'E'; + step++; + } + if (x > x0) { + x -= step; + a[step] = 'W'; + step++; + } + } + while (y0 != y) { + if (y < y0) { + y += step; + a[step] = 'N'; + step++; + } + if (y > x0) { + y -= step; + a[step] = 'S'; + step++; + } + } +} + +bool test () { + int x, y; + int i = 5; + char* a; + while (i < 50) { + x = i; + y = i--; + a = way(x, y); + x = -i; + y = ++i; + a = way(x, y); + x = ++i; + y = -i; + a = way(x, y); + } + return true; +} + \ No newline at end of file diff --git a/test/test_add.cpp b/test/test_add.cpp deleted file mode 100644 index 66c2df3..0000000 --- a/test/test_add.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "add.h" - -TEST(Addition, CanAddTwoNumbers) { - EXPECT_EQ(add(2, 2), 4); - EXPECT_EQ(add(-2, 2), 0); -} diff --git a/test/test_min_way.cpp b/test/test_min_way.cpp new file mode 100644 index 0000000..06a11d9 --- /dev/null +++ b/test/test_min_way.cpp @@ -0,0 +1,6 @@ +#include +#include "min_way.h" + +TEST(Min_Way, can_work) { + EXPECT_EQ(test(), true); +} From 23aee08d147753eea51b3f3f6a82afb90b69b0b3 Mon Sep 17 00:00:00 2001 From: Usova Marina A Date: Sun, 2 Apr 2017 13:15:55 +0300 Subject: [PATCH 2/2] Changed function, tests and added the application --- app/main.cpp | 21 ++++++++- include/min_way.h | 1 - src/min_way.cpp | 106 +++++++++++++++++++++++------------------- test/test_min_way.cpp | 28 ++++++++++- 4 files changed, 105 insertions(+), 51 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 798865a..25ec04c 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,6 +1,25 @@ #include "min_way.h" #include +using std::cout; +using std::endl; int main() { + cout << "Find way from (0,0) to (4,5): "; + char* result_way = way(4, 5); + cout << result_way << endl << endl; + cout << "Find way from (0,0) to (-4,5): "; + result_way = way(-4, 5); + cout << result_way << endl << endl; + cout << "Find way from (0,0) to (4,-5): "; + result_way = way(4, -5); + cout << result_way << endl << endl; + cout << "Find way from (0,0) to (-4,-5): "; + result_way = way(-4, -5); + cout << result_way << endl << endl; + cout << "Find way from (0,0) to (0,0): "; + result_way = way(0, 0); + cout << result_way << endl << endl; + cout << "Find way from (0,0) to (4,-55): "; + result_way = way(4, -55); + cout << result_way << endl << endl; } - diff --git a/include/min_way.h b/include/min_way.h index 9ded219..3327ba7 100644 --- a/include/min_way.h +++ b/include/min_way.h @@ -2,6 +2,5 @@ #define INCLUDE_MIN_WAY_H_ char* way(int x, int y); -bool test (); #endif // INCLUDE_MIN_WAY_H_ diff --git a/src/min_way.cpp b/src/min_way.cpp index 6a6d3ae..f2a1fae 100644 --- a/src/min_way.cpp +++ b/src/min_way.cpp @@ -4,61 +4,73 @@ #include char* way(int x, int y) { - char* a = new char [100]; + char* a = new char[100]; int x0 = 0, y0 = 0; int step = 1; + if ((x == 0) && (y == 0)) { - throw std::logic_error("Error description\n"); - } - while (x0 <= x) { - x += step; - a[step] = 'E'; - step++; - y += step; - a[step] = 'N'; - step++; + std::cout << "You are already in finish position"; } - while (x0 != x) { - if (x < x0) { - x += step; - a[step] = 'E'; - step++; - } - if (x > x0) { - x -= step; - a[step] = 'W'; - step++; + + if (x > 0) { + while (x0 + step < x) { + x0 += step; + a[step - 1] = 'E'; + ++step; } - } - while (y0 != y) { - if (y < y0) { - y += step; - a[step] = 'N'; - step++; + while (x0 != x) { + x0 -= step; + a[step - 1] = 'W'; + ++step; + x0 += step; + a[step - 1] = 'E'; + ++step; } - if (y > x0) { - y -= step; - a[step] = 'S'; - step++; + } else { + while (x0 - step > x) { + x0 -= step; + a[step - 1] = 'W'; + ++step; + } + while (x0 != x) { + x0 += step; + a[step - 1] = 'E'; + ++step; + x0 -= step; + a[step - 1] = 'W'; + ++step; } } -} -bool test () { - int x, y; - int i = 5; - char* a; - while (i < 50) { - x = i; - y = i--; - a = way(x, y); - x = -i; - y = ++i; - a = way(x, y); - x = ++i; - y = -i; - a = way(x, y); + if (y > 0) { + while (y0 + step < y) { + y0 += step; + a[step - 1] = 'N'; + ++step; + } + while (y0 != y) { + y0 -= step; + a[step - 1] = 'S'; + ++step; + y0 += step; + a[step - 1] = 'N'; + step++; + } + } else { + while (y0 - step > y) { + y0 -= step; + a[step - 1] = 'S'; + ++step; + } + while (y0 != y) { + y0 += step; + a[step - 1] = 'N'; + ++step; + y0 -= step; + a[step - 1] = 'S'; + ++step; + } } - return true; + a[step - 1] = '\0'; + return a; } - \ No newline at end of file diff --git a/test/test_min_way.cpp b/test/test_min_way.cpp index 06a11d9..723ecdc 100644 --- a/test/test_min_way.cpp +++ b/test/test_min_way.cpp @@ -1,6 +1,30 @@ #include #include "min_way.h" -TEST(Min_Way, can_work) { - EXPECT_EQ(test(), true); +bool test_1(int x, int y) { + int x0 = 0, y0 = 0; + int step = 1; + char* ch = way(x, y); + for (int i = 0; ch[i] != '\0'; ++i) { + if (ch[i] == 'N') + y0 += step; + if (ch[i] == 'S') + y0 -= step; + if (ch[i] == 'W') + x0 -= step; + if (ch[i] == 'E') + x0 += step; + step++; + } + return ((x == x0) && (y == y0)); +} + +TEST(Find_Way, can_find) { + for (int x = 1; x < 50; ++x) + for (int y = 1; y < 50; ++y) { + EXPECT_TRUE(test_1(x, y)); + EXPECT_TRUE(test_1(-x, y)); + EXPECT_TRUE(test_1(x, -y)); + EXPECT_TRUE(test_1(-x, -y)); + } }