working 1number, not yet splitted

master
nihonium 2 years ago
parent ab6732eded
commit 7e1ad95143
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -1,5 +1,6 @@
#include <iostream>
#include <math.h>
#include <cstdlib>
#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;

@ -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="<<result.size<< std::endl;
#endif
return result;
}
void operator+=(const Number& a) {
@ -193,8 +211,7 @@ public:
#endif
if (carry[i + j - 1]) {
result.data[i + j - 1] = carry[i + j - 1];
result.size = i + j;
carry[i + j - 1] = 0;
result.size = i + j;
}
else {
result.size = i + j - 1;
@ -227,6 +244,78 @@ public:
*this = *this * a;
}
bool operator==(const Number& a) const {
if (size != a.size) {
return false;
}
for (int i = 0; i < size; ++i) {
if (data[i] != a.data[i]) {
return false;
}
}
return true;
}
bool operator!=(const Number& a) const {
return not (*this == a);
}
bool operator>(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<int>(data[i]) << ">" << static_cast<int>(a.data[i]) << std::endl;
#endif
}
if (data[i] < a.data[i]) {
#ifdef _DEBUG_COMP
std::cout << static_cast<int>(data[i]) << "<" << static_cast<int>(a.data[i]) <<std::endl;
#endif
return false;
}
}
#ifdef _DEBUG_COMP
std::cout << "using final false" << std::endl;
#endif
return false;
}
bool operator<(const Number& a) const {
return not (*this > 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;
}