brain:verylong.cpp
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
brain:verylong.cpp [2016/12/16 10:47] – created peter | brain:verylong.cpp [2020/07/15 09:30] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | verylong.h | + | verylong.cpp |
<code cpp> | <code cpp> | ||
- | #ifndef __SHAREWIZ_VERYLONG_H__ | + | /* |
- | #define __SHAREWIZ_VERYLONG_H__ | + | #include < |
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
- | //# | + | # |
- | // Very Long Integer | + | // Class Data |
+ | const Verylong Verylong:: | ||
+ | const Verylong Verylong:: | ||
+ | const Verylong Verylong:: | ||
- | class Verylong | + | // Constructors, |
+ | |||
+ | Verylong:: | ||
{ | { | ||
- | private: | + | std:: |
- | // Data Fields | + | |
- | std:: | + | |
- | int vlsign; | + | |
- | // Private member functions | + | vlsign = (s[0] == ' |
- | Verylong multdigit(int) const; | + | if (ispunct(s[0])) // if the first character |
- | Verylong mult10(int) const; | + | vlstr = s.substr(1, s.length() - 1); // is a punctuation mark. |
+ | else | ||
+ | vlstr = s; | ||
+ | } | ||
- | public: | ||
- | // Constructors and destructor | ||
- | Verylong(const std:: | ||
- | Verylong(int); | ||
- | Verylong(const Verylong &); | ||
- | ~Verylong(); | ||
- | // Conversion operators | + | Verylong:: |
- | operator int() const; | + | { |
- | operator double() const; | + | if (n < 0) // check for sign and convert the |
- | operator std:: | + | { // number to positive if it is negative |
+ | vlsign = 1; | ||
+ | n = (-n); | ||
+ | } | ||
+ | else | ||
+ | vlsign = 0; | ||
- | // Arithmetic operators and Relational operators | + | if (n > 0) |
- | const Verylong & operator = (const Verylong &); // assignment operator | + | while (n >= 1) |
- | Verylong operator - () const; | + | { |
- | Verylong operator ++ (); | + | vlstr = char(n % 10 + ' |
- | Verylong operator | + | n /= 10; |
- | Verylong operator -- (); // prefix | + | } |
- | Verylong operator -- (int); | + | else |
+ | vlstr = std::string(" | ||
+ | } | ||
- | Verylong operator += (const Verylong &); | ||
- | Verylong operator -= (const Verylong &); | ||
- | Verylong operator *= (const Verylong &); | ||
- | Verylong operator /= (const Verylong &); | ||
- | Verylong operator %= (const Verylong &); | ||
- | Verylong operator ^= (const Verylong &); | ||
- | friend | + | Verylong::Verylong(const Verylong &x) : vlstr(x.vlstr), vlsign(x.vlsign) |
- | friend | + | { |
- | friend Verylong operator * (const Verylong &, const Verylong &); | + | } |
- | friend Verylong operator / (const Verylong &, const Verylong &); | + | |
- | friend Verylong operator % (const Verylong &, const Verylong &); | + | |
- | friend Verylong operator ^ (const Verylong &, const Verylong &); | + | |
- | friend int operator == (const Verylong &, const Verylong &); | ||
- | friend int operator != (const Verylong &, const Verylong &); | ||
- | friend int operator < (const Verylong &, const Verylong &); | ||
- | friend int operator <= (const Verylong &, const Verylong &); | ||
- | friend int operator > (const Verylong &, const Verylong &); | ||
- | friend int operator >= (const Verylong &, const Verylong &); | ||
- | // Other functions | + | Verylong::~Verylong() |
- | friend | + | { |
- | friend Verylong sqrt(const Verylong &); | + | } |
- | friend Verylong pow(const Verylong &, const Verylong &); | + | |
- | friend double div(const Verylong &, const Verylong &); | + | |
- | // Class Data | ||
- | static const Verylong zero; | ||
- | static const Verylong one; | ||
- | static const Verylong two; | ||
- | // I/O stream functions | + | Verylong:: |
- | friend std::ostream & operator | + | { |
- | friend | + | int number, factor = 1; |
- | }; | + | static |
+ | static Verylong min0(INT_MIN + 1); | ||
+ | std::string::const_reverse_iterator j = vlstr.rbegin(); | ||
+ | if (*this > max0) | ||
+ | { | ||
+ | std::cerr << "Error : Conversion Verylong-> | ||
+ | return INT_MAX; | ||
+ | } | ||
+ | else | ||
+ | if (*this < min0) | ||
+ | { | ||
+ | std::cerr << "Error : Conversion Verylong-> | ||
+ | return INT_MIN; | ||
+ | } | ||
- | //template <> Verylong zero(Verylong) { return Verylong:: | + | number = *j - ' |
- | //template <> Verylong one(Verylong) { return Verylong:: | + | |
+ | for (j++; j != vlstr.rend(); | ||
+ | { | ||
+ | factor *= 10; | ||
+ | number += (*j - ' | ||
+ | } | ||
- | #endif | + | if (vlsign) |
+ | return -number; | ||
+ | return number; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong:: | ||
+ | { | ||
+ | double sum, factor = 1.0; | ||
+ | std:: | ||
+ | |||
+ | sum = double(*i) - ' | ||
+ | |||
+ | for (i++; i != vlstr.rend(); | ||
+ | { | ||
+ | factor *= 10.0; | ||
+ | sum += double(*i - ' | ||
+ | } | ||
+ | |||
+ | if (vlsign) | ||
+ | return -sum; | ||
+ | |||
+ | return sum; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong:: | ||
+ | { | ||
+ | if (vlstr.length() == 0) | ||
+ | return std:: | ||
+ | |||
+ | return vlstr; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Various member operators | ||
+ | |||
+ | const Verylong & Verylong:: | ||
+ | { | ||
+ | if (this == & | ||
+ | return *this; | ||
+ | |||
+ | vlstr = rhs.vlstr; | ||
+ | vlsign = rhs.vlsign; | ||
+ | |||
+ | return *this; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Unary - operator | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | Verylong temp(*this); | ||
+ | |||
+ | if (temp != zero) | ||
+ | temp.vlsign = !vlsign; | ||
+ | |||
+ | return temp; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Prefix increment operator | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this + one; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Postfix increment operator | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | Verylong result(*this); | ||
+ | |||
+ | *this = *this + one; | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Prefix decrement operator | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this - one; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Postfix decrement operator | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | Verylong result(*this); | ||
+ | |||
+ | *this = *this - one; | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this + v; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this - v; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this * v; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this / v; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | return *this = *this % v; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | Verylong N(degree); | ||
+ | Verylong Y(" | ||
+ | |||
+ | if (N == Verylong:: | ||
+ | return Verylong:: | ||
+ | |||
+ | if (N < Verylong:: | ||
+ | return Verylong:: | ||
+ | |||
+ | while (1) | ||
+ | { | ||
+ | if (N == Verylong:: | ||
+ | { | ||
+ | *this = Y; | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | Y = Y * *this; | ||
+ | N = N - Verylong:: | ||
+ | } | ||
+ | |||
+ | return *this; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | // Various friendship operators and functions. | ||
+ | |||
+ | Verylong operator + (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | char digitsum, d1, d2, carry = 0; | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | if (u.vlsign ^ v.vlsign) | ||
+ | { | ||
+ | if (u.vlsign == 0) | ||
+ | return u - abs(v); | ||
+ | else | ||
+ | return v - abs(u); | ||
+ | } | ||
+ | |||
+ | for (j = u.vlstr.rbegin(), | ||
+ | j != u.vlstr.rend() || k != v.vlstr.rend(); | ||
+ | { | ||
+ | d1 = (j == u.vlstr.rend()) ? 0 : *(j++) - ' | ||
+ | d2 = (k == v.vlstr.rend()) ? 0 : *(k++) - ' | ||
+ | digitsum = d1 + d2 + carry; | ||
+ | |||
+ | carry = (digitsum >= 10) ? 1 : 0; | ||
+ | digitsum -= 10 * carry; | ||
+ | |||
+ | temp = char(digitsum + ' | ||
+ | } | ||
+ | |||
+ | if (carry) // if carry at end, last digit is 1 | ||
+ | temp = ' | ||
+ | |||
+ | if (u.vlsign) | ||
+ | temp = ' | ||
+ | |||
+ | return Verylong(temp); | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong operator - (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | char d, d1, d2, borrow = 0; | ||
+ | int negative; | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | if (u.vlsign ^ v.vlsign) | ||
+ | { | ||
+ | if (u.vlsign == 0) | ||
+ | return u + abs(v); | ||
+ | else | ||
+ | return -(v + abs(u)); | ||
+ | } | ||
+ | |||
+ | Verylong w, y; | ||
+ | |||
+ | if (u.vlsign == 0) // both u,v are positive | ||
+ | if (u< | ||
+ | { | ||
+ | w = v; | ||
+ | y = u; | ||
+ | negative = 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | w = u; | ||
+ | y = v; | ||
+ | negative = 0; | ||
+ | } | ||
+ | else | ||
+ | if (u< | ||
+ | { | ||
+ | w = u; | ||
+ | y = v; | ||
+ | negative = 1; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | w = v; | ||
+ | y = u; | ||
+ | negative = 0; | ||
+ | } | ||
+ | |||
+ | for (i = w.vlstr.rbegin(), | ||
+ | i != w.vlstr.rend() || j != y.vlstr.rend(); | ||
+ | { | ||
+ | d1 = (i == w.vlstr.rend()) ? 0 : *(i++) - ' | ||
+ | d2 = (j == y.vlstr.rend()) ? 0 : *(j++) - ' | ||
+ | |||
+ | d = d1 - d2 - borrow; | ||
+ | borrow = (d < 0) ? 1 : 0; | ||
+ | d += 10 * borrow; | ||
+ | |||
+ | temp = char(d + ' | ||
+ | } | ||
+ | |||
+ | while (temp[0] == ' | ||
+ | temp = temp.substr(1); | ||
+ | |||
+ | if (negative) | ||
+ | temp = ' | ||
+ | |||
+ | return Verylong(temp); | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong operator * (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | Verylong pprod(" | ||
+ | std:: | ||
+ | |||
+ | for (int j = 0; r != v.vlstr.rend(); | ||
+ | { | ||
+ | int digit = *r - ' | ||
+ | |||
+ | pprod = u.multdigit(digit); | ||
+ | pprod = pprod.mult10(j); | ||
+ | tempsum = tempsum + pprod; | ||
+ | } | ||
+ | |||
+ | tempsum.vlsign = u.vlsign^v.vlsign; | ||
+ | return tempsum; | ||
+ | } | ||
+ | |||
+ | |||
+ | // This algorithm is the long division algorithm. | ||
+ | Verylong operator / (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | int len = u.vlstr.length() - v.vlstr.length(); | ||
+ | std:: | ||
+ | Verylong w, y, b, c, d, quotient = Verylong:: | ||
+ | |||
+ | if (v == Verylong:: | ||
+ | { | ||
+ | std::cerr << "Error : division by zero" << std::endl; | ||
+ | return Verylong:: | ||
+ | } | ||
+ | |||
+ | w = abs(u); | ||
+ | y = abs(v); | ||
+ | |||
+ | if (w < y) | ||
+ | return Verylong:: | ||
+ | |||
+ | c = Verylong(w.vlstr.substr(0, | ||
+ | |||
+ | for (int i = 0; i <= len; i++) | ||
+ | { | ||
+ | quotient = quotient.mult10(1); | ||
+ | |||
+ | b = d = Verylong:: | ||
+ | |||
+ | while (b < c) | ||
+ | { | ||
+ | b = b + y; d = d + Verylong:: | ||
+ | } | ||
+ | |||
+ | if (c < b) // if b>c, then | ||
+ | { // we have added one count too many | ||
+ | b = b - y; | ||
+ | d = d - Verylong:: | ||
+ | } | ||
+ | |||
+ | quotient = quotient + d; // add to the quotient | ||
+ | |||
+ | if (i < len) | ||
+ | { | ||
+ | // partial remainder * 10 and add to next digit | ||
+ | c = (c - b).mult10(1); | ||
+ | c += Verylong(w.vlstr[w.vlstr.length() - len + i] - ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | quotient.vlsign = u.vlsign^v.vlsign; | ||
+ | |||
+ | return quotient; | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong operator % (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return (u - v*(u / v)); | ||
+ | } | ||
+ | |||
+ | |||
+ | Verylong operator ^ (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | //return (u - v*(u / v)); | ||
+ | |||
+ | Verylong temp(u); | ||
+ | |||
+ | return temp ^= v; | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | int operator == (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return (u.vlsign == v.vlsign && u.vlstr == v.vlstr); | ||
+ | } | ||
+ | |||
+ | |||
+ | int operator != (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return !(u == v); | ||
+ | } | ||
+ | |||
+ | |||
+ | int operator < (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | if (u.vlsign < v.vlsign) | ||
+ | return 0; | ||
+ | else | ||
+ | if (u.vlsign > v.vlsign) | ||
+ | return 1; | ||
+ | |||
+ | // exclusive or (^) to determine sign | ||
+ | if (u.vlstr.length() < v.vlstr.length()) | ||
+ | return (1 ^ u.vlsign); | ||
+ | else | ||
+ | if (u.vlstr.length() > v.vlstr.length()) | ||
+ | return (0 ^ u.vlsign); | ||
+ | |||
+ | return (u.vlstr < v.vlstr && !u.vlsign) || | ||
+ | (u.vlstr > v.vlstr && u.vlsign); | ||
+ | } | ||
+ | |||
+ | |||
+ | int operator <= (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return (u<v || u == v); | ||
+ | } | ||
+ | |||
+ | |||
+ | int operator >(const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return (!(u<v) && u != v); | ||
+ | } | ||
+ | |||
+ | |||
+ | int operator >= (const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | return (u>v || u == v); | ||
+ | } | ||
+ | |||
+ | |||
+ | // Calculate the absolute value of a number | ||
+ | Verylong abs(const Verylong &v) | ||
+ | { | ||
+ | Verylong u(v); | ||
+ | |||
+ | if (u.vlsign) | ||
+ | u.vlsign = 0; | ||
+ | |||
+ | return u; | ||
+ | } | ||
+ | |||
+ | // Calculate the integer square root of a number | ||
+ | // based on the formula (a+b)^2 = a^2 + 2ab + b^2 | ||
+ | Verylong sqrt(const Verylong &v) | ||
+ | { | ||
+ | // if v is negative, error is reported | ||
+ | if (v.vlsign) | ||
+ | { | ||
+ | std::cerr << " | ||
+ | return Verylong:: | ||
+ | } | ||
+ | |||
+ | int j, k = v.vlstr.length() + 1, num = k >> 1; | ||
+ | Verylong y, z, sum, tempsum, digitsum; | ||
+ | |||
+ | std:: | ||
+ | |||
+ | k = 0; | ||
+ | j = 1; | ||
+ | |||
+ | // segment the number 2 digits by 2 digits | ||
+ | if (v.vlstr.length() % 2) | ||
+ | digitsum = Verylong(w[k++] - ' | ||
+ | else | ||
+ | { | ||
+ | digitsum = Verylong((w[k] - ' | ||
+ | k += 2; | ||
+ | } | ||
+ | |||
+ | // find the first digit of the integer square root | ||
+ | sum = z = Verylong(int(sqrt(double(digitsum)))); | ||
+ | |||
+ | // store partial result | ||
+ | temp = char(int(z) + ' | ||
+ | digitsum = digitsum - z*z; | ||
+ | |||
+ | for (; j<num; j++) | ||
+ | { | ||
+ | // get next digit from the number | ||
+ | digitsum = digitsum.mult10(1) + Verylong(w[k++] - ' | ||
+ | y = z + z; // 2*a | ||
+ | z = digitsum / y; | ||
+ | tempsum = digitsum.mult10(1) + Verylong(w[k++] - ' | ||
+ | digitsum = -y*z.mult10(1) + tempsum - z*z; | ||
+ | |||
+ | // decrease z by 1 and re-calculate when it is over-estimated. | ||
+ | while (digitsum < Verylong:: | ||
+ | { | ||
+ | --z; | ||
+ | digitsum = -y*z.mult10(1) + tempsum - z*z; | ||
+ | } | ||
+ | |||
+ | temp = temp + char(int(z) + ' | ||
+ | z = sum = sum.mult10(1) + z; // update value of the partial result | ||
+ | } | ||
+ | |||
+ | Verylong result(temp); | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Raise a number X to a power of degree | ||
+ | Verylong pow(const Verylong &X, const Verylong & | ||
+ | { | ||
+ | Verylong N(degree), Y(" | ||
+ | |||
+ | if (N == Verylong:: | ||
+ | return Verylong:: | ||
+ | |||
+ | if (N < Verylong:: | ||
+ | return Verylong:: | ||
+ | |||
+ | while (1) | ||
+ | { | ||
+ | if (N%Verylong:: | ||
+ | { | ||
+ | Y = Y * x; | ||
+ | N = N / Verylong:: | ||
+ | if (N == Verylong:: | ||
+ | return Y; | ||
+ | } | ||
+ | else | ||
+ | N = N / Verylong:: | ||
+ | |||
+ | x = x * x; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | // Double division function | ||
+ | double div(const Verylong &u, const Verylong &v) | ||
+ | { | ||
+ | double qq = 0.0, qqscale = 1.0; | ||
+ | Verylong w, y, b, c; | ||
+ | int d, count, | ||
+ | decno = std:: | ||
+ | |||
+ | if (v == Verylong:: | ||
+ | { | ||
+ | std::cerr << "ERROR : Division by zero" << std::endl; | ||
+ | return 0.0; | ||
+ | } | ||
+ | |||
+ | if (u == Verylong:: | ||
+ | return 0.0; | ||
+ | |||
+ | w = abs(u); | ||
+ | y = abs(v); | ||
+ | |||
+ | while (w< | ||
+ | { | ||
+ | w = w.mult10(1); | ||
+ | qqscale *= 0.1; | ||
+ | } | ||
+ | |||
+ | int len = w.vlstr.length() - y.vlstr.length(); | ||
+ | std:: | ||
+ | |||
+ | c = Verylong(temp); | ||
+ | |||
+ | for (int i = 0; i <= len; i++) | ||
+ | { | ||
+ | qq *= 10.0; | ||
+ | |||
+ | b = Verylong:: | ||
+ | |||
+ | while (b < c) | ||
+ | { | ||
+ | b += y; d += 1; | ||
+ | } | ||
+ | |||
+ | if (c < b) // if b>c, then | ||
+ | { // we have added one count too many | ||
+ | b -= y; | ||
+ | d -= 1; | ||
+ | } | ||
+ | |||
+ | qq += double(d); | ||
+ | |||
+ | c = (c - b).mult10(1); | ||
+ | |||
+ | if (i < len) // and add to next digit | ||
+ | c += Verylong(w.vlstr[w.vlstr.length() - len + i] - ' | ||
+ | } | ||
+ | |||
+ | qq *= qqscale; count = 0; | ||
+ | |||
+ | while (c != Verylong:: | ||
+ | { | ||
+ | qqscale *= 0.1; | ||
+ | |||
+ | b = Verylong:: | ||
+ | |||
+ | while (b < c) | ||
+ | { | ||
+ | b += y; d += 1; | ||
+ | } | ||
+ | |||
+ | if (c < b) // if b>c, then | ||
+ | { // we have added one count too many | ||
+ | b -= y; d -= 1; | ||
+ | } | ||
+ | |||
+ | qq += double(d)*qqscale; | ||
+ | |||
+ | c = (c - b).mult10(1); | ||
+ | count++; | ||
+ | } | ||
+ | |||
+ | if (u.vlsign^v.vlsign) | ||
+ | qq *= (-1.0); | ||
+ | |||
+ | return qq; | ||
+ | } | ||
+ | |||
+ | |||
+ | std:: | ||
+ | { | ||
+ | if (v.vlstr.length() > 0) | ||
+ | { | ||
+ | if (v.vlsign) s << " | ||
+ | s << v.vlstr; | ||
+ | } | ||
+ | else | ||
+ | s << " | ||
+ | |||
+ | return s; | ||
+ | } | ||
+ | |||
+ | |||
+ | std:: | ||
+ | { | ||
+ | std:: | ||
+ | |||
+ | s >> temp; | ||
+ | v = Verylong(temp); | ||
+ | |||
+ | return s; | ||
+ | } | ||
+ | |||
+ | |||
+ | // | ||
+ | // Private member functions: multdigit(), | ||
+ | // | ||
+ | |||
+ | // Multiply this Verylong number by num | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | int carry = 0; | ||
+ | std:: | ||
+ | |||
+ | if (num) | ||
+ | { | ||
+ | std:: | ||
+ | |||
+ | for (r = vlstr.rbegin(); | ||
+ | { | ||
+ | int d1 = *r - ' | ||
+ | digitprod = d1*num + carry; | ||
+ | |||
+ | if (digitprod >= 10) // if there' | ||
+ | { | ||
+ | carry = digitprod / 10; // carry is high digit | ||
+ | digitprod -= carry * 10; // result is low digit | ||
+ | } | ||
+ | else | ||
+ | carry = 0; // otherwise carry is 0 | ||
+ | |||
+ | temp = char(digitprod + ' | ||
+ | } | ||
+ | |||
+ | if (carry) //if carry at end, | ||
+ | temp = char(carry + ' | ||
+ | |||
+ | Verylong result(temp); | ||
+ | return result; | ||
+ | } | ||
+ | else | ||
+ | return zero; | ||
+ | } | ||
+ | |||
+ | |||
+ | // Multiply this Verylong number by 10*num | ||
+ | Verylong Verylong:: | ||
+ | { | ||
+ | int j; | ||
+ | |||
+ | if (*this != zero) | ||
+ | { | ||
+ | std:: | ||
+ | |||
+ | for (j = 0; j<num; j++) | ||
+ | temp = temp + ' | ||
+ | |||
+ | Verylong result(vlstr + temp); | ||
+ | |||
+ | if (vlsign) | ||
+ | result = -result; | ||
+ | |||
+ | return result; | ||
+ | } | ||
+ | else | ||
+ | return zero; | ||
+ | } | ||
+ | |||
+ | |||
+ | //template <> Verylong zero(Verylong) { return Verylong:: | ||
+ | //template <> Verylong one(Verylong) { return Verylong:: | ||
</ | </ | ||
+ | |||
+ |
brain/verylong.cpp.1481885277.txt.gz · Last modified: 2020/07/15 09:30 (external edit)