社区
C++ 语言
帖子详情
C语言长整数运算的实现
00102zwb
2003-02-27 03:41:16
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
...全文
139
9
打赏
收藏
C语言长整数运算的实现
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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语言
的双向循环链表数据结构来
实现
长
整数
的加法和减法
运算
。通过定义链表节点结构,创建、插入、删除等操作,
实现
了将输入的字符串转换成
长
整数
链表,并完成加减法计算。最后展示了主程序中用户输入
长
整数
并选择
运算
类型的示例。
c语言
长
整数
加减乘除,用
C语言
实现
超
长
整数
的加减乘除四则
运算
该文通过
C语言
的链表和字符串应用,解决了处理超
长
整数
的存储问题,详细介绍了如何
实现
超
长
整数
的加、减、乘、除四则
运算
,并对算法进行了分析。
C语言
实现
大数
运算
(
长
整数
的加、减、乘、除)
本文介绍了如何使用字符串表示大
整数
并
实现
大
整数
的加法、减法、乘法和除法。通过逐位
运算
,处理进位和借位,确保了大
整数
运算
的正确性。提供的
C语言
代码示例展示了具体的
实现
过程。
数组
实现
长
整数
四则
运算
c语言
,[转载]
长
整数
四则
运算
程序(1)
本文介绍了如何使用C++编程
实现
链表结构,通过`LinkList`类构建双向链表,并演示了如何创建链表、引入字符串创建
整数
链表以及
实现
两个
整数
链表的相加功能。`addtwo()`函数中结合了`Creat()`和`Add()`函数,最终通过`Display()`展示结果。
C++ 语言
65,211
社区成员
250,516
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章