问题描述:
设计一个实现任意长的整数进行加法运算的演示程序.
----------------------------------------------------------------------------------
基本要求:
利用双向循环链表实现长整数的存储,每个结点含一个整型变量。任何整形变量的范围是
-( 2^15 - 1 ) ~~~ ( 2^15 - 1 )。
输入和输出形式:按中国对于长整数的表示习惯,每四位一组,组间用逗号隔开
----------------------------------------------------------------------------------
测试数据:
1) 0;0;应输出"0".
2) -2345,6789;-7654,3211;应输出"-1,0000,0000".
3) -9999,9999;1,0000,0000,0000;应输出"9999,0000,0001".
4) 1,0001,0001;-1,0001,0001;应输出"0".
----------------------------------------------------------------------------------
实现提示:
------------------------------------------------------- ----------------------------
选作内容:
===================================================================================
*/
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
/异常
class InvalidOfLong
{
public:
InvalidOfLong(int line):_line(line){}
int _line;
};
class Long
{
friend std::ostream& operator << ( std::ostream &os, const Long& data );
public:
Long( const std::string &str, const char delim );
Long( const Long& data );
const Long& operator = ( const Long& data );
public:
const Long operator + ( const Long& data )const;
const Long operator - ( const Long& data )const;
const Long operator - ( void ) const;
const Long operator * ( const Long& data )const;
const Long operator / ( const Long& data )const;
private:
const Long& operator <<( const int count );//左移count*_limit位
const int vectorComp ( const std::vector<int>& lhs, const std::vector<int>& rhs )const;
private:
std::vector<int> * _data;
int _sign; //符号位 -1=='-', 0=='+'
static char _delim;//分隔符
const static int _limit=4;
const static int _system;//进制 pow(10,Long::_limit)
};
char Long::_delim(0);
const int Long::_system=static_cast<int>( pow(10,Long::_limit) ) ;
///
inline const Long& Long::operator <<( const int count )
{
//左移count*_limit位
for( int i(0); i<count; ++i )
{
this->_data->push_back( 0 );
}
return *this;
}
///
inline const Long Long::operator - ( void )const
{
Long temp( *this );
temp._sign = ( ( temp._sign == -1 )?(0):(-1) );
return temp;
}
///
inline Long::Long(const std::string &str, const char delim=',' )
:_data(new std::vector<int>)
{
_delim=delim;//分隔符
// str "1234567890" -----------> Long 12,3456,7890
std::string::size_type pos(0);
int number(0),flag(0);
if( str[pos] >= '0' && str[pos] <= '9' )
_sign = 0;
else
{
switch( str[pos] )
{
case '-':
_sign = -1; //purposive :)
case '+':
++pos;
flag = 1;
break;
default:
throw InvalidOfLong(__LINE__);
}
}
for(int beginlimit(static_cast<int>((str.size()-flag)%Long::_limit)); beginlimit; --beginlimit,++pos )
{
const int tn( str[pos]-'0' );
if( tn >= 0 && tn <= 9 )
number += tn * static_cast<int>( pow(10,beginlimit-1) );
else
throw InvalidOfLong(__LINE__);
}
if( number )
_data->push_back( number );
number=0;
for(int bit(Long::_limit-1); pos != str.size(); ++pos,--bit )
{
const int tb( bit%Long::_limit ),tn( str[pos]-'0' );
if( tn >= 0 && tn <= 9 )
(tb)?(number+=tn*static_cast<int>(pow(10,tb))):(_data->push_back(number+tn),number=0,bit=Long::_limit);
else
throw InvalidOfLong(__LINE__);
}
if(_data->empty())
_data->push_back(0);
}
inline const int Long::vectorComp( const std::vector<int>&lhs, const std::vector<int>&rhs )const
{
if( lhs.size() < rhs.size() )
return -1;
if( lhs.size() == rhs.size() )
{
for( std::vector<int>::const_iterator itl=lhs.begin(),itr=rhs.begin();
itl != lhs.end();
++itl,++itr )
{
if( *itl < *itr )
return -1;
if( *itl > *itr )
return 1;
}
}
return 0;
}
///
inline Long::Long( const Long& data )
{
if( &data != this )
{
this->_data = new std::vector<int>( * data._data );
this->_sign = data._sign;
}
}
///
inline const Long& Long::operator = ( const Long& data )
{
if( &data == this )
return data;
delete this->_data ;
this->_data = new std::vector<int>( * data._data );
this->_sign = data._sign;
return *this;
}
inline const Long Long::operator + ( const Long& data )const
{
if( this->_sign == data._sign )//同号
{
if( this->_data->size() > data._data->size() )//长度比较,temp总是为较长的数(本来是用Long::vectorComp的,不过没必要
{
Long temp( *this );
int add(0);//进位
std::vector<int>::iterator itl( temp._data->end() );
std::vector<int>::const_iterator itr( data._data->end() );
for( --itl,--itr; itr != data._data->begin(); --itl,--itr )
{
int number( *itl + *itr + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
//加itr的begin()
int number( *itl + *itr + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
//加余下的itl,如果有的话
if( itl != temp._data->begin() )
{
for( --itl; itl != temp._data->begin(); --itl )
{
int number( *itl + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
//加itl的begin()
int number( *itl + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
return temp;
}
else
{
// temp==data, itl==temp, itr==this
Long temp( data );
int add(0);
std::vector<int>::iterator itl( temp._data->end() );
std::vector<int>::const_iterator itr( this->_data->end() );
for( --itl,--itr; itr != this->_data->begin(); --itl,--itr )
{
int number( *itl + *itr + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
//加itr的begin()
int number( *itl + *itr + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
//加余下的itl,如果有的话
if( itl != temp._data->begin() )
{
for( --itl; itl != temp._data->begin(); --itl )
{
int number( *itl + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
//加itl的begin()
int number( *itl + add );
*itl = ( (number>=Long::_system)?(add=1,number-Long::_system):(add=0,number) );
}
return temp;
}
}
else//异号
{
if( this->_sign == -1 )
{
Long temp( * this );
temp._sign = 0;
return data-temp;
}
Long temp( data );
temp._sign = 0;
return *this - temp;
}
}
///
inline const Long Long::operator - ( const Long& data )const
{
if( this->_sign == data._sign ) //同号
{
switch( vectorComp( *( this->_data ), *( data._data ) ) )//绝对值比较
{
case 1:// | *this | > | data |
{
Long temp( *this );
temp._sign = this->_sign;//符号
int bit(0); //借位
std::vector<int>::iterator itl( temp._data->end() );
std::vector<int>::const_iterator itr( data._data->end() );
for( --itl,--itr; itr!=data._data->begin(); --itl,--itr )
{
int number( *itl - *itr - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
//itr's end();
int number( *itl - *itr - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
//完成剩下的itl,如果有的话
if( itl != temp._data->begin() )
{
for( --itl; itl!=temp._data->begin(); --itl )
{
int number( *itl - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
int number( *itl - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
//去头零
for( std::vector<int>::iterator it( temp._data->begin() ); it!= temp._data->end(); )
{
if( *it == 0 )
{
std::vector<int>::iterator god( it );
temp._data->erase( god );
}
else
return temp;
}
return temp;
}
case -1:// | *this | < | data |
{
Long temp( data );
temp._sign = data._sign;//符号
int bit(0); //借位
std::vector<int>::iterator itl( temp._data->end() );
std::vector<int>::const_iterator itr( this->_data->end() );
for( --itl,--itr; itr!=this->_data->begin(); --itl,--itr )
{
int number( *itl - *itr - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
//itr's end();
int number( *itl - *itr - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
//完成剩下的itl,如果有的话
if( itl != temp._data->begin() )
{
for( --itl; itl!=temp._data->begin(); --itl )
{
int number( *itl - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
int number( *itl - bit );
*itl = (number<0)?(bit=1,number+Long::_system):(bit=0,number);
}
//去头零
for( std::vector<int>::iterator it( temp._data->begin() ); it!= temp._data->end(); )
{
if( *it == 0 )
{
std::vector<int>::iterator god( it );
temp._data->erase( god );
}
else
return temp;
}
return temp;
}
case 0:// | *this | == | data |
return Long(std::string("0"));
}
throw InvalidOfLong(__LINE__);
}
else //异号
{
if( -1 == this->_sign )
{
Long temp( data );
temp._sign = -1;
return *this+temp;
}
else
{
Long temp( data );
temp._sign = 0;
return *this+temp;
}
}
throw InvalidOfLong(__LINE__);
}
inline const Long Long::operator / ( const Long& data ) const
{
}
///
inline const Long Long::operator * ( const Long& data )const
{
if( (*this->_data)[0] == 0 || (* data._data)[0] == 0 )
return Long( std::string("0") );
Long temp( std::string("0") ); /temp作返回值
if( this->_sign == data._sign )///设置符号位
{
if( temp._sign == -1 )temp._sign = 0;
}
else
{
temp._sign = -1;
}
int temp_begin(1); //初始标志位
if( this->_data->size() > data._data->size() ) /// this's vect > data's vect
{
//以 data's vect 作外层循环
Long dummy( *this );
std::vector<int>::iterator itr( --dummy._data->end() );/是要将其左移位的
std::vector<int>::const_iterator itl( --data._data->end() );
int add_count(0);//进位计数
for( ; itl != data._data->begin(); --itl,dummy<<1 )
{
for(; itr != dummy._data->begin(); --itr )
{
int number( *itl * *itr + add_count );
(number>=Long::_system)?(add_count=number/Long::_system,number%=Long::_system):(add_count=0);
//first
(1==temp_begin)?(temp_begin=0,(*temp._data)[0]=number):(temp._data->insert(temp._data->begin(),number);
}
//还有一次 this->_data->begin()
if( itr == dummy._data->begin() )
{
int number( *itl * *itr + add_count );
(number>=Long::_system)?(add_count=number/Long::_system,number%=Long::_system):(add_count=0);
//first
(1==temp_begin)?(temp_begin=0,(*temp._data)[0]=number):(temp._data->insert(temp._data->begin(),number);
}
if( add_count != 0 )
{
temp._data->insert( temp._data->begin(),add_count );
add_count = 0; //进制标志清零为下一次循环作准备
}
}
//还有一次 data._data->begin()
if( itl == data._data->begin() )
{
for(std::vector<int>::const_iterator itr(--dummy._data->end()); itr != dummy._data->begin(); --itr )
{
int number( *itl * *itr + add_count );
(number>=Long::_system)?(add_count=number/Long::_system,number%=Long::_system):(add_count=0);
//first
(1==temp_begin)?(temp_begin=0,(*temp._data)[0]=number):(temp._data->insert(temp._data->begin(),number);
}
//还有一次 this->_data->begin()
if( itr == dummy._data->begin() )
{
int number( *itl * *itr + add_count );
(number>=Long::_system)?(add_count=number/Long::_system,number%=Long::_system):(add_count=0);
//first
(1==temp_begin)?(temp_begin=0,(*temp._data)[0]=number):(temp._data->insert(temp._data->begin(),number);
}
if( add_count != 0 )
{
temp._data->insert( temp._data->begin(),add_count );
add_count = 0; //进制标志清零为下一次循环作准备
}
}
}
else this's vect <= data's vect
{
}
return temp;
}
///
inline std::ostream& operator << ( std::ostream &os, const Long& data )
{
if( !data._data->empty() )
{
if(data._sign)
os<<'-';
for( unsigned pos(0); pos!= data._data->size(); ++pos )
{
const int number( (*data._data)[pos] );
if( pos == 0 )
os<<number;
else
{
if( number != 0 )
{
int bit( number / 10 + 1 );
std::string temp;
for(int i(0); i<( Long::_limit - number/10 +1 ); ++i)
temp+="0";
os<<temp;
os<<number;
}
else
os<<"0000";
}
if( data._data->size() > 1 && pos != data._data->size() -1 )
{
os<<Long::_delim ;
}
}
return os;
}
return os<<"";
}
int main(void)
{
try
{
Long number1(std::string("100000000")),number2(std::string("10000"));
std::cout<<number1<<" * "<<number2<<" = ";
Long number = number1*number2;
std::cout<<number;
}
catch( InvalidOfLong a)
{
std::cerr<<"异常InvalidOfLong,发生在"<<a._line<<"行";
}
return 0;
}