C语言长整数运算的实现

00102zwb 2003-02-27 03:41:16
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxjddd 2003-03-04
  • 打赏
  • 举报
回复
THPInt::THPNSign
THPInt::AbsCompare( const vector<THPInt::TPlace>& lop,
const vector<THPInt::TPlace>& rop ) const
{
if( lop.size() > rop.size() )
return HPNPositive;
else if( lop.size() < rop.size() )
return HPNNegative;
vector<TPlace>::const_iterator ptr1 = lop.begin();
vector<TPlace>::const_iterator ptr2 = rop.begin();
while( ptr1 != lop.end() )
if( *ptr1 > *ptr2 )
return HPNPositive;
else if( *ptr1 < *ptr2 )
return HPNNegative;
else
{
ptr1++;
ptr2++;
}
return HPNZero;
}

THPInt::THPNSign
THPInt::AbsCompare( const vector<THPInt::TPlace>& mul,
vector<THPInt::TPlace>::reverse_iterator ptr,
bool more_digit ) const
{
vector<TPlace>::const_reverse_iterator mptr;
if( more_digit )
{
int head = mul[mul.size()-1] * HPNPlaceInc + mul[mul.size()-2];
if( head > *ptr )
return HPNPositive;
else if( head < *ptr )
return HPNNegative;
else
{
mptr = mul.rbegin() + 2;
ptr++;
}
}
else
mptr = mul.rbegin();
while( mptr != mul.rend() )
if( *mptr > *ptr )
return HPNPositive;
else if( *mptr < *ptr )
return HPNNegative;
else
{
mptr++;
ptr++;
}
return HPNZero;
}

int
THPInt::AbsInc( vector<THPInt::TPlace>& lop,
const vector<THPInt::TPlace>& rop )
{
int shrt, lng;
if( lop.size() <= rop.size() )
{
shrt = lop.size();
lng = rop.size();
lop.insert( lop.end(), rop.begin()+shrt, rop.end() );
}
else
{
shrt = rop.size();
lng = lop.size();
}
lop.resize( lng + 1 );
int i;
for( i = 0; i < shrt; i++ )
{
lop[i] += rop[i];
if( lop[i] > HPNPlaceMax )
{
lop[i] -= HPNPlaceInc;
lop[i+1]++;
}
}
for( i = shrt; i < lng; i++ )
if( lop[i] > HPNPlaceMax )
{
lop[i] -= HPNPlaceInc;
lop[i+1]++;
}
if( lop[lng] == 0 )
lop.pop_back();
return lop.size();
}

int
THPInt::AbsDec( vector<THPInt::TPlace>& lop,
const vector<THPInt::TPlace>& rop )
{
int i;
for( i = 0; i < rop.size(); i++ )
{
if( lop[i] < rop[i] )
{
lop[i] += HPNPlaceInc;
lop[i+1]--;
}
lop[i] -= rop[i];
}
while( lop[i] < 0 )
{
lop[i]+=HPNPlaceInc;
lop[++i]--;
}
vector<TPlace>::iterator rptr = lop.end();
rptr--;
while( *rptr == 0 )
lop.erase( rptr-- );
return lop.size();
}

int
THPInt::AbsDecTo( vector<THPInt::TPlace>& lop,
const vector<THPInt::TPlace>& rop )
{
vector<TPlace> op( rop );
AbsDec( op, lop );
swap( lop, op );
return lop.size();
}

int
THPInt::AbsMulti( vector<THPInt::TPlace>& lop,
const vector<THPInt::TPlace>& rop )
{
vector<TPlace> mul( lop );
lop.clear();
lop.resize( mul.size() + rop.size(), 0 );
for( int i = 0; i < rop.size(); i++ )
if( rop[i] != 0 )
{
for( int j = 0; j < mul.size(); j++ )
{
lop[j+i] += rop[i] * mul[j];
if( lop[j+i] > HPNPlaceMax )
{
lop[j+i+1] += lop[j+i] / HPNPlaceInc;
lop[j+i] %= HPNPlaceInc;
}
}
int j = mul.size() + i;
while( lop[j] > HPNPlaceMax )
{
lop[j] -= HPNPlaceInc;
lop[++j]++;
}
}
if( lop[ mul.size() + rop.size() - 1 ] == 0 )
lop.pop_back();
return lop.size();
}
cxjddd 2003-03-04
  • 打赏
  • 举报
回复
#ifndef __BenBear_High_Precise_Number
#define __BenBear_High_Precise_Number

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <sstream>
using namespace std;

namespace BenBear
{

#include "HPNumberDef.h"
using namespace BenBear;

class THPInt : public THPN_Base
{
protected:
vector<TPlace> places;
THPNSign sign;

THPNSign AbsCompare( const vector<TPlace>& p ) const;
THPNSign AbsCompare( const vector<TPlace>& lop,
const vector<TPlace>& rop ) const;
THPNSign AbsCompare( const vector<TPlace>& mul,
vector<TPlace>::reverse_iterator ptr,
bool more_digit ) const;

int AbsInc ( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsDec ( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsDecTo( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsMulti( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsDiv ( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsMod ( vector<TPlace>& lop, const vector<TPlace>& rop );
int AbsDiv_1( vector<TPlace>& lop, int rop );
int AbsMod_1( vector<TPlace>& lop, int rop );

int AbsMultiIntTo( const vector<TPlace>& lop, int num,
vector<TPlace>& mul );
int AbsDec ( vector<TPlace>& lop, int k, const vector<TPlace>& rop );

bool StrToHPInt( const string& str );
int IntToHPInt( int num );

public:
explicit
THPInt( int num = 0 )
{ IntToHPInt( abs( num ) ); }

THPInt( const string& str )
{ StrToHPInt( str ); }

THPInt( const THPInt& hpi )
: places( hpi.places ), sign( hpi.sign )
{}

THPInt& operator= ( const THPInt& hpi )
{
places = hpi.places;
sign = hpi.sign;
}

THPInt& operator+= ( const THPInt& hpi );
THPInt& operator-= ( const THPInt& hpi );
THPInt& operator*= ( const THPInt& hpi );
THPInt& operator/= ( const THPInt& hpi );
THPInt& operator%= ( const THPInt& hpi );

bool operator== ( const THPInt& hpi ) const
{ return ( sign == hpi.sign ) && ( places == hpi.places ); }

bool operator!= ( const THPInt& hpi ) const
{ return ! ( *this == hpi ); }

bool operator< ( const THPInt& hpi ) const
{ return sign < hpi.sign ? true :
( AbsCompare( hpi.places ) == HPNNegative );
}

bool operator> ( const THPInt& hpi ) const
{ return hpi < *this; }

bool operator<= ( const THPInt& hpi ) const
{ return ! ( *this > hpi ); }

bool operator>= ( const THPInt& hpi ) const
{ return ! ( *this < hpi ); }

string str();
};

THPInt& //************ += **************//
THPInt::operator+= ( const THPInt& hpi )
{
if( hpi.sign == HPNZero )
;// do nothing :)
else if( sign == HPNZero )
*this = hpi;
else if( sign != hpi.sign )
{
sign = -sign;
this->operator-=( hpi );
sign = -sign;
}
else
AbsInc( places, hpi.places );
return *this;
}

THPInt& //************ -= **************//
THPInt::operator-= ( const THPInt& hpi )
{
if( hpi.sign == 0 )
;// do nothing :)
else if( sign == 0 )
{
*this = hpi;
sign = -hpi.sign;
}
else if( sign != hpi.sign )
{
sign = -sign;
this->operator+=( hpi );
sign = -sign;
}
else
{
switch( AbsCompare( hpi.places ) )
{
case HPNZero:
{ places.clear(); sign = HPNZero; break; }
case HPNPositive:
{ AbsDec( places, hpi.places ); break; }
case HPNNegative:
{ AbsDecTo( places, hpi.places ); sign = -sign; break; }
}
}
return *this;
}

THPInt& //************ *= **************//
THPInt::operator*= ( const THPInt& hpi )
{
if( sign == 0 || hpi.sign == 0 )
{
places.clear();
sign = HPNZero;
}
else
{
AbsMulti( places, hpi.places );
sign *= hpi.sign;
}
return *this;
}

THPInt& //************ /= **************//
THPInt::operator/= ( const THPInt& hpi )
{
if( hpi.sign == HPNZero )
{
cout << "Error:Divide by zero" << endl;
exit( 1 );
}
else if( sign == HPNZero )
; // do nothing :)
else
{
switch( AbsCompare( hpi.places ) )
{
case HPNPositive : if( hpi.places.size() != 1 )
AbsDiv( places, hpi.places );
else
AbsDiv_1( places, hpi.places[0] );
break;
case HPNZero : places.clear(); places.push_back( 1 ); break;
case HPNNegative : places.clear(); sign = HPNZero; break;
}
sign *= hpi.sign;
}
return *this;
}

THPInt& //************ %= **************//
THPInt::operator%= ( const THPInt& hpi )
{
if( hpi.sign == HPNZero )
{
cout << "Error:Divide by zero" << endl;
exit( 1 );
}
else if( sign == HPNZero )
;
else
{
switch( AbsCompare( hpi.places ) )
{
case HPNPositive : if( hpi.places.size() != 1 )
AbsMod( places, hpi.places );
else
AbsMod_1( places, hpi.places[0] );
break;
case HPNZero : places.clear(); sign = HPNZero; break;
case HPNNegative : break;
}
sign *= hpi.sign;
}
return *this;
}

THPInt::THPNSign
THPInt::AbsCompare( const vector<THPInt::TPlace>& p ) const
{
if( places.size() > p.size() )
return HPNPositive;
else if( places.size() < p.size() )
return HPNNegative;
vector<TPlace>::const_iterator ptr1 = places.begin();
vector<TPlace>::const_iterator ptr2 = p.begin();
while( ptr1 != places.end() )
if( *ptr1 > *ptr2 )
return HPNPositive;
else if( *ptr1 < *ptr2 )
return HPNNegative;
else
{
ptr1++;
ptr2++;
}
return HPNZero;
}

cxjddd 2003-03-04
  • 打赏
  • 举报
回复
#ifndef __BenBear_HPNumber_Def
#define __BenBear_HPNumber_Def

#include <cstddef>
using namespace std;

namespace BenBear
{

class THPN_Base
{
protected:
typedef int THPNSign;
static const int HPNNegative = -1;
static const int HPNZero = 0;
static const int HPNPositive = 1;

typedef int TPlace;
static const int HPNPlaceInc = 10000;
static const int HPNPlaceMax = HPNPlaceInc - 1;
static const int HPNPlaceLength = 4;
};

} // namespace BenBear

#endif
lca8u8 2003-03-04
  • 打赏
  • 举报
回复
原程序在程序员教程里有
你可以去找找
kerry365 2003-03-04
  • 打赏
  • 举报
回复
数据结构中使用链表来储存长整型
00102zwb 2003-03-04
  • 打赏
  • 举报
回复
能不能来点详细一点的啊:)
最好能给出原程序来啊~~~
谢谢
flyingbugs 2003-02-28
  • 打赏
  • 举报
回复
用数组就可以了
evilaworm 2003-02-27
  • 打赏
  • 举报
回复
是不是就是高精度运算啊?
diabloqin 2003-02-27
  • 打赏
  • 举报
回复
用链表存储长整数,每个node存储一位,可以很方便地实现加,减。
乘,除运算可能复杂一点,可以考虑C语言与汇编的混合编程实现。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧