diff --git a/app/main.cpp b/app/main.cpp index 4485790..25ec04c 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -1,6 +1,25 @@ -#include "add.h" +#include "min_way.h" #include +using std::cout; +using std::endl; int main() { - std::cout << "2 + 2 = " << add(2, 2) << std::endl; + 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/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..3327ba7 --- /dev/null +++ b/include/min_way.h @@ -0,0 +1,6 @@ +#ifndef INCLUDE_MIN_WAY_H_ +#define INCLUDE_MIN_WAY_H_ + +char* way(int x, int y); + +#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..f2a1fae --- /dev/null +++ b/src/min_way.cpp @@ -0,0 +1,76 @@ +#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)) { + std::cout << "You are already in finish position"; + } + + if (x > 0) { + while (x0 + step < x) { + x0 += step; + a[step - 1] = 'E'; + ++step; + } + while (x0 != x) { + x0 -= step; + a[step - 1] = 'W'; + ++step; + x0 += step; + a[step - 1] = 'E'; + ++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; + } + } + + 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; + } + } + a[step - 1] = '\0'; + return a; +} 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..723ecdc --- /dev/null +++ b/test/test_min_way.cpp @@ -0,0 +1,30 @@ +#include +#include "min_way.h" + +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)); + } +}