C++大数类

来源:互联网 发布:数据采集与处理官网 编辑:程序博客网 时间:2024/05/23 13:29
//BigNum.h//作者:wzh190015//日期:2015/12/07#ifndef BIG_NUM_H#define BIG_NUM_H#include<deque>using namespace std;class BigNum//大数类{public://构造函数BigNum ();//默认构造函数BigNum ( int );//构造函数BigNum ( const BigNum& );//拷贝构造函数//重载运算符//重载输入输出运算符friend istream& operator>>( istream& , BigNum& );//重载输入运算符friend ostream& operator<<( ostream& , BigNum& );//重载输出运算符//重载算数运算符//重载加号及相关运算符BigNum operator+( BigNum& );BigNum operator+( int );friend BigNum operator+( int , BigNum& );BigNum operator+( );BigNum operator+=( BigNum& );BigNum operator+=( int );BigNum& operator++( );BigNum operator++( int );//重载减号及相关运算符BigNum operator-( BigNum& );BigNum operator-( int );friend BigNum operator-( int , BigNum& );BigNum operator-( );BigNum operator-=( BigNum& );BigNum operator-=( int );BigNum& operator--( );BigNum operator--( int );//重载乘号及相关运算符BigNum operator*( BigNum& );BigNum operator*( int );friend BigNum operator*( int , BigNum& );BigNum operator*=( BigNum& );BigNum operator*=( int );//重载除号及相关运算符BigNum operator/( BigNum& );BigNum operator/( int );friend BigNum operator/( int , BigNum& );BigNum operator/=( BigNum& );BigNum operator/=( int );//重载取模运算符BigNum operator%( BigNum& );BigNum operator%( int );friend BigNum operator%( int , BigNum& );//乘方运算friend BigNum power ( BigNum& , BigNum& );friend BigNum power ( BigNum& , int );friend BigNum power ( int , BigNum& );//重载逻辑运算符//重载小于及相关运算符bool operator<( BigNum& );bool operator<( int );friend bool operator<( int , BigNum& );bool operator<=( BigNum& );bool operator<=( int );friend bool operator<=( int , BigNum& );//重载大于及相关运算符bool operator>( BigNum& );bool operator>( int );friend bool operator>( int , BigNum& );bool operator>=( BigNum& );bool operator>=( int );friend bool operator>=( int , BigNum& );//重载等于及相关运算符bool operator==( BigNum& );bool operator==( int );friend bool operator==( int , BigNum& );bool operator!=( BigNum& );bool operator!=( int );friend bool operator!=( int , BigNum& );private:deque<int> num;//储存数字bool positive;//储存符号};#endif
<pre name="code" class="cpp">//BigNum.cpp//作者:wzh190015//日期:2015/12/07#include"BigNum.h"#include<iostream>#include<string>#include<vector>//构造函数//默认构造函数BigNum::BigNum (){num.push_back ( 0 );positive = true;}//构造函数BigNum::BigNum ( int n ){positive = true;char *cstr = new char [ 1000 ];sprintf_s ( cstr , 1000 , "%d" , n );string str ( cstr );for ( auto &i : str ){if ( i == '-' ){positive = false;continue;}if ( i == '+' ){positive = true;continue;}num.push_back ( i - '0' );}delete []cstr;}//拷贝构造函数BigNum::BigNum ( const BigNum& copy ){this->num = copy.num;this->positive = copy.positive;}//重载输入输出运算符//重载输入运算符istream& operator>>( istream &in , BigNum &bn ){char i;bn.num.clear ();bn.positive = true;while ( isdigit ( i = getchar () ) || i == '+' || i == '-' ){if ( i == '-' ){bn.positive = false;continue;}if ( i == '+' ){bn.positive = true;continue;}bn.num.push_back ( i - '0' );}return in;}//重载输出运算符ostream& operator<<( ostream& out , BigNum& bn ){if ( bn.positive == false ){out << "-";}for ( auto &i : bn.num ){out << i;}return out;}//重载算数运算符//重载加号及相关运算符BigNum BigNum::operator+( BigNum& n2 ){if ( positive == true && n2.positive == false ){return ( *this - ( -n2 ) );}else if ( positive == false && n2.positive == true ){return ( n2 - ( -*this ) );}else if ( positive == false && n2.positive == false ){return -( -n2 + ( -*this ) );}else{auto i1 = num.rbegin ();auto i2 = n2.num.rbegin ();BigNum sum;sum.num.clear ();int carry = 0;for ( ; i1 != num.rend () && i2 != n2.num.rend (); i1++ , i2++ ){sum.num.push_front ( ( ( *i1 + *i2 ) + carry ) % 10 );if ( *i1 + *i2 + carry >= 10 ){carry = 1;}else{carry = 0;}}if ( i1 == num.rend () ){for ( ; i2 != n2.num.rend (); i2++ ){sum.num.push_front ( ( *i2 + carry ) % 10 );if ( *i2 + carry >= 10 ){carry = 1;}else{carry = 0;}}}else if ( i2 == n2.num.rend () ){for ( ; i1 != num.rend (); i1++ ){sum.num.push_front ( ( *i1 + carry ) % 10 );if ( *i1 + carry >= 10 ){carry = 1;}else{carry = 0;}}}if ( carry == 1 ){sum.num.push_front ( 1 );}sum.positive = positive;return sum;}}BigNum BigNum::operator+( int n2 ){BigNum bn2 ( n2 );return ( *this + bn2 );}BigNum operator+( int n1 , BigNum& n2 ){return n2 + n1;}BigNum BigNum::operator+( ){BigNum bn ( *this );if ( this->positive == true ){bn.positive = true;}else{bn.positive = false;}return bn;}BigNum BigNum::operator+=( BigNum& n2 ){*this = *this + n2;return *this;}BigNum BigNum::operator+=( int n2 ){*this = *this + n2;return *this;}BigNum& BigNum::operator++( ){*this = *this + 1;return *this;}BigNum BigNum::operator++( int ){BigNum old = *this;++*this;return old;}//重载减号及相关运算符BigNum BigNum::operator-( BigNum& n2 ){if ( positive == false && n2.positive == true ){return -( n2 + ( -*this ) );}if ( positive == true && n2.positive == false ){return ( *this + ( -n2 ) );}if ( positive == false && n2.positive == false ){return ( ( -n2 ) - *this );}if ( n2 < *this ){auto i1 = num.rbegin ();auto i2 = n2.num.rbegin ();BigNum dif;dif.num.clear ();int borrow = 0;for ( ; i2 != n2.num.rend (); i1++ , i2++ ){dif.num.push_front ( ( ( *i1 - *i2 + borrow ) >= 0 ) ? ( *i1 - *i2 + borrow ) : ( *i1 - *i2 + borrow + 10 ) );if ( *i1 - *i2 + borrow < 0 ){borrow = -1;}else{borrow = 0;}}for ( ; i1 != num.rend (); i1++ ){dif.num.push_front ( ( ( *i1 + borrow ) >= 0 ) ? ( *i1 + borrow ) : ( *i1 + borrow + 10 ) );if ( *i1 + borrow < 0 ){borrow = -1;}else{borrow = 0;}}while ( dif.num [ 0 ] == 0 && dif.num.size () > 1 ){dif.num.pop_front ();}return dif;}else if ( n2 > *this ){return -( n2 - *this );}else{return 0;}}BigNum BigNum::operator-( int n2 ){BigNum bn2 ( n2 );return ( *this - bn2 );}BigNum operator-( int n1 , BigNum& n2 ){return n2 - n1;}BigNum BigNum::operator-( ){BigNum bn ( *this );if ( this->positive == true ){bn.positive = false;}else{bn.positive = true;}return bn;}BigNum BigNum::operator-=( BigNum& n2 ){*this = *this - n2;return *this;}BigNum BigNum::operator-=( int n2 ){*this = *this - n2;return *this;}BigNum& BigNum::operator--( ){*this = *this - 1;return *this;}BigNum BigNum::operator--( int ){BigNum old = *this;--*this;return old;}//重载乘号及相关运算符BigNum BigNum::operator*( BigNum& n2 ){if ( *this == 0 || n2 == 0 ){return 0;}if ( positive == false && n2.positive == true ){return -( ( -*this )*n2 );}if ( positive == true && n2.positive == false ){return -( *this*( -n2 ) );}if ( positive == false && n2.positive == false ){return ( -*this )*( -n2 );}BigNum product;//product.num.clear ();vector<BigNum> vec;auto i2 = n2.num.rbegin ();int carry = 0;int level = 0;for ( ; i2 != n2.num.rend (); i2++ , level++ ){carry = 0;product.num.clear ();auto i1 = num.rbegin ();for ( ; i1 != num.rend (); i1++ ){product.num.push_front ( ( ( *i1 )*( *i2 ) + carry ) % 10 );carry = ( ( ( ( *i1 )*( *i2 ) ) + carry ) / 10 );}product.num.push_front ( carry );for ( int i = 0; i < level; i++ ){product.num.push_back ( 0 );}vec.push_back ( product );}product.num.clear ();product.num.push_back ( 0 );for ( auto &i : vec ){product += i;}return product;}BigNum BigNum::operator*( int n2 ){BigNum bn2 ( n2 );return ( *this * bn2 );}BigNum operator*( int n1 , BigNum& n2 ){return n2 * n1;}BigNum BigNum::operator*=( BigNum& n2 ){*this = *this * n2;return *this;}BigNum BigNum::operator*=( int n2 ){*this = *this * n2;return *this;}//重载除号及相关运算符BigNum BigNum::operator/( BigNum& n2 ){if ( *this == 0 ){return 0;}if ( n2 == 0 ){std::cerr << "CANNOT DIVIDED BY ZERO!" << endl;return 0;}if ( positive == false && n2.positive == true ){return -( ( -*this ) / n2 );}if ( positive == true && n2.positive == false ){return -( *this / ( -n2 ) );}if ( positive == false && n2.positive == false ){return ( -*this ) / ( -n2 );}BigNum quotient;BigNum dividend = *this;for ( quotient = 0; dividend >= n2; ++quotient ){dividend -= n2;}return quotient;}BigNum BigNum::operator/( int n2 ){BigNum bn2 ( n2 );return ( *this / bn2 );}BigNum operator/( int n1 , BigNum& n2 ){return n2 / n1;}BigNum BigNum::operator/=( BigNum& n2 ){*this = *this / n2;return *this;}BigNum BigNum::operator/=( int n2 ){*this = *this / n2;return *this;}//重载取模运算符BigNum BigNum::operator%( BigNum& n2 ){if ( *this == 0 ){return 0;}if ( n2 == 0 ){return *this;}if ( *this == n2 ){return 0;}if ( positive == false && n2.positive == true ){return -( ( -*this ) % n2 );}if ( positive == true && n2.positive == false ){return -( *this % ( -n2 ) );}if ( positive == false && n2.positive == false ){return ( -*this ) % ( -n2 );}BigNum remainder = *this;while ( remainder >= n2 ){remainder -= n2;if ( remainder < n2 ){return remainder;}}return remainder;}BigNum BigNum::operator%( int n2 ){BigNum bn2 ( n2 );return ( *this % bn2 );}BigNum operator%( int n1 , BigNum& n2 ){return n2 % n1;}//重载乘方运算符BigNum power ( BigNum& n1 , BigNum& n2 ){BigNum power = 1;for ( BigNum i = 0; i < n2; i++ ){power *= n1;while ( power.num [ 0 ] == 0 ){power.num.pop_front ();}}return power;}BigNum power ( BigNum& n1 , int n2 ){BigNum bn2 ( n2 );return power ( n1 , bn2 );}BigNum power ( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return power ( bn1 , n2 );}//重载逻辑运算符//重载小于及相关运算符bool BigNum::operator<( BigNum& n2 ){if ( positive == false && n2.positive == true ){return true;}else if ( positive == true && n2.positive == false ){return false;}else if ( positive == false && n2.positive == false ){return ( -n2 < -*this );}if ( num.size () < n2.num.size () ){return true;}else if ( num.size ()>n2.num.size () ){return false;}auto i1 = num.begin ();auto i2 = n2.num.begin ();for ( ; i1 != num.end () && i2 != n2.num.end (); i1++ , i2++ ){if ( *i1 < *i2 ){return true;}if ( *i1>*i2 ){return false;}}return false;}bool BigNum::operator<( int n2 ){BigNum bn2 ( n2 );return *this < bn2;}bool operator<( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 < n2;}bool BigNum::operator<=( BigNum& n2 ){return ( ( *this < n2 ) || ( *this == n2 ) );}bool BigNum::operator<=( int n2 ){BigNum bn2 ( n2 );return *this <= bn2;}bool operator<=( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 <= n2;}//重载大于及相关运算符bool BigNum::operator>( BigNum& n2 ){if ( positive == false && n2.positive == true ){return false;}else if ( positive == true && n2.positive == false ){return true;}else if ( positive == false && n2.positive == false ){return ( -n2 > -*this );}if ( num.size () > n2.num.size () ){return true;}else if ( num.size () > n2.num.size () ){return false;}auto i1 = num.begin ();auto i2 = n2.num.begin ();for ( ; i1 != num.end () && i2 != n2.num.end (); i1++ , i2++ ){if ( *i1 > *i2 ){return true;}if ( *i1 < *i2 ){return false;}}return false;}bool BigNum::operator> ( int n2 ){BigNum bn2 ( n2 );return *this > bn2;}bool operator>( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 > n2;}bool BigNum::operator>=( BigNum& n2 ){return ( ( *this > n2 ) || ( *this == n2 ) );}bool BigNum::operator>=( int n2 ){BigNum bn2 ( n2 );return *this >= bn2;}bool operator>=( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 >= n2;}//重载等于及相关运算符bool BigNum::operator==( BigNum& n2 ){if ( positive == n2.positive && num == n2.num ){return true;}else{return false;}}bool BigNum::operator==( int n2 ){BigNum bn2 ( n2 );return *this == bn2;}bool operator==( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 == n2;}bool BigNum::operator!=( BigNum& n2 ){if ( *this == n2 ){return false;}else{return true;}}bool BigNum::operator!=( int n2 ){BigNum bn2 ( n2 );return *this != bn2;}bool operator!=( int n1 , BigNum& n2 ){BigNum bn1 ( n1 );return bn1 != n2;}



0 0
原创粉丝点击