From 7e1ad9514340ee01d0cb753fbd5bfd7707c5f208 Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 15 Sep 2022 08:21:25 +0300 Subject: [PATCH] working 1number, not yet splitted --- .../homework/code/0circle/circle.cpp | 3 +- .../code/1number/{number.cpp => main.cpp} | 156 ++++++++++++++++-- 2 files changed, 140 insertions(+), 19 deletions(-) rename seminar02_encapsulation/homework/code/1number/{number.cpp => main.cpp} (70%) diff --git a/seminar02_encapsulation/homework/code/0circle/circle.cpp b/seminar02_encapsulation/homework/code/0circle/circle.cpp index 4d123fe..89b4e78 100644 --- a/seminar02_encapsulation/homework/code/0circle/circle.cpp +++ b/seminar02_encapsulation/homework/code/0circle/circle.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "circle.h" #include "point.h" @@ -25,7 +26,7 @@ void Circle::setRadius(float radius) { mRadius = radius > 0 ? radius : 0; } float Circle::getArea() const { - return mRadius * mRadius * M_PI; + return abs(mRadius * mRadius * M_PI); } float Circle::getDistance(const Point& p) { return mCenter.distance(p) - mRadius; diff --git a/seminar02_encapsulation/homework/code/1number/number.cpp b/seminar02_encapsulation/homework/code/1number/main.cpp similarity index 70% rename from seminar02_encapsulation/homework/code/1number/number.cpp rename to seminar02_encapsulation/homework/code/1number/main.cpp index 55cfbcd..d4a4158 100644 --- a/seminar02_encapsulation/homework/code/1number/number.cpp +++ b/seminar02_encapsulation/homework/code/1number/main.cpp @@ -118,27 +118,45 @@ public: return *this; } - Number operator+(const Number& a) { + Number operator+(Number a) { +#ifdef _DEBUG_ADD + std::cout << "arg1=" << a << "capacity=" << a.capacity << ",size="<< a.size<< std::endl; + std::cout << "arg2=" << *this << "capacity=" << this->capacity << ",size="<< this->size<< std::endl; + +#endif Number result; + Number temp; int i; - char carry = 0; - int max_size = size > a.size ? size : a.size; - - result.capacity = max_size + 1; - result.data = new char[capacity]; - - for (i = 0; i < max_size; ++i) { + int carry = 0; + if (size < a.size) { + temp = *this; + *this = a; + a = temp; + } + result.capacity = size + 1; + //result.data = new char[capacity]; + result.data = (char*)calloc(result.capacity, sizeof(char)); + for (i = 0; i < a.size; ++i) { result.data[i] = (data[i] + a.data[i] + carry) % base; carry = (data[i] + a.data[i] + carry) / base; } - + for (; i < size; ++i) { + result.data[i] = (data[i] + carry) % base; + carry = (data[i] + carry) / base; + } if (carry) { +#ifdef _DEBUG_ADD + std::cout << "applied carry" << std::endl; +#endif result.data[i] = carry; - result.size = max_size + 1; + result.size = size + 1; } else { - result.size = max_size; + result.size = size; } +#ifdef _DEBUG_ADD + std::cout << result << " capacity=" << result.capacity << ",size="<(const Number& a) const { +#ifdef _DEBUG_COMP + std::cout << "comp " << *this << "(size=" << size << ") and " << a << "(size=" << a.size << ")" << std::endl; +#endif + if (size > a.size) { +#ifdef _DEBUG_COMP + std::cout << "size > a.size => true" << std::endl; +#endif + return true; + } + if (size < a.size) { +#ifdef _DEBUG_COMP + std::cout << "size < a.size => false" << std::endl; +#endif + return false; + } + for (int i = size - 1; i >= 0; --i) { + if (data[i] > a.data[i]) { + return true; +#ifdef _DEBUG_COMP + std::cout << static_cast(data[i]) << ">" << static_cast(a.data[i]) << std::endl; +#endif + } + if (data[i] < a.data[i]) { +#ifdef _DEBUG_COMP + std::cout << static_cast(data[i]) << "<" << static_cast(a.data[i]) < a) and (*this != a); + } + void div2() { +#ifdef _DEBUG_DIV2 + std::cout << "n = " << *this << std::endl; +#endif + int carry = 0; + int temp; + for (int i = size - 1; i >= 0; --i) { + temp = data[i] + carry * base; + data[i] = temp / 2; + carry = temp % 2; + } + if (data[size-1] == 0) { + --size; + } +#ifdef _DEBUG_DIV2 + std::cout << "unstripped result "<< *this << std::endl; +#endif + } + friend std::ostream& operator<<(std::ostream& stream, const Number& right); friend int main(); friend Number factorial(int n); @@ -279,11 +368,39 @@ Number factorial(int n) { return result; } +void grad(Number n) { + + std::cout << "n = " << n; + Number max = n; + unsigned long long int steps = 0; + while (n != Number(1)) { + //std::cout << steps << ":" << n << std::endl; + if (n > max) { +#ifdef _DEBUG_COMP + std::cout << n << " is greater than " << max << std::endl; +#endif + max = n; + } + if (n.isEven()) { + n.div2(); + } + else { + n = Number(3) * n + Number(1); + } + //if(steps > 100) { + // std::cout << "break" << std::endl; + // break; + //} + ++steps; + } + std::cout << " steps = " << steps << " max = " << max << std::endl; +} + int main() { - Number x = Number("25852016738884976640000"); - Number y = Number("24"); - + Number x = Number("12"); + Number y = Number("122"); + //y.div2(); //char s[3]; //Number result = "1"; //for (int i = 1; i < 26; ++i) { @@ -298,10 +415,13 @@ int main() //y = factorial(5); //std::cout << x << " " << x.capacity << " " << x.size << std::endl; //std::cout << y << " "<< y.capacity << " " << y.size << std::endl; - // 90405070506200618121707-18-13-05-18-08 //std::cout << "===" << std::endl << Number(2) * Number(3) << " "<< Number(3) * Number(2) << std::endl; //std::cout << "5! = " << Number(2) * Number(3) * Number(4) * Number(5) << std::endl; - std::cout << factorial(1000) << std::endl; + //std::cout << factorial(1000) << std::endl; //std::cout << Number("620448401733239439360000") * Number(25) << std::endl; + //std::cout << (y < x) << std::endl; + grad(Number("4761963248413673697")); + //grad(Number("256")); + //std::cout << Number(128) * Number(3) + Number(1) + Number(2) + Number(3) + Number(4) << std::endl; }