社区
C++ 语言
帖子详情
C语言长整数运算的实现
00102zwb
2003-02-27 03:41:16
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
...全文
114
9
打赏
收藏
C语言长整数运算的实现
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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语言与汇编的混合编程实现。
c语言
任意
长
整数
的加减法代码
c语言
代码写的任意
长
整数
加减法,虽然不是最优化的,但时间度杂度还行。
c语言
实现
的大
整数
数
运算
程序
c语言
实现
的大
整数
相加,已通过相当严格的测试,保证正确,请注明cherry
免费下载:
C语言
难点分析整理.doc
C语言
中
实现
数组的动态增
长
通过使用指针和动态内存分配,可以在运行时动态增加数组的大小。这通常涉及到重分配更大的内存块并将原有内容复制过去。 ### 11.
C语言
中的位
运算
位
运算
符允许对
整数
进行按位操作,如...
200个经典C程序【源码】
108 递归
整数
四则
运算
109 复平面作图 110 绘制彩色抛物线 111 绘制正态分布曲线 112 求解非线性方程 113 实矩阵乘法
运算
114 求解线性方程 115 n阶方阵求逆 116 复矩阵乘法 117 求定积分 118 求满足特异...
c语言
长
整数
运算
,
长
整数
运算
已结贴√问题点数:10回复次数:3
长
整数
运算
输入用三行,第一行和第三行为数值,第二行为
运算
符,能进行加法、减法
运算
,减法
运算
包括差为负数的情况。具备乘法
运算
功能具备除法功能具备小数点功能输入用一行完成,...
C++ 语言
65,192
社区成员
250,525
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章