社区
C++ 语言
帖子详情
C语言长整数运算的实现
00102zwb
2003-02-27 03:41:16
要求通过键盘输入超长(可能超出计算机能够直接处理的范围)的两个整数,计算机输出其加,减,乘,除运算的结果.用C语言实现.
...全文
146
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语言
,数据结构:
长
整数
的四则
运算
题目:设计一个
实现
任意
长
的
整数
的加法
运算
的演示程序一、需求分析本演示程序中,利用双向循环链表来
实现
长
整数
的储存,每个节点含一个整型变量。输入和输出形式按照中国对于
长
整数
的表示习惯,每四位一组,组间用逗号隔开;其中输入的两个
长
整数
用 ';' (英语输入法)结尾,允许逗号位置错误,并在输入非法字符是提示错误。2.测试数据(1)0;0;应输出“0”。(2)-2345,6789;-7654,3211;应输...
长
整数
运算
加法
c语言
链表,用链表
实现
长
整数
加减法
运算
1.构建双向循环链表的数据结构:#include#include#include#define LEN sizeof(struct long_int)struct long_int{int data;int over;struct long_int *pro;struct long_int *next;};2.完成该功能所使用的各个算法:struct long_int *creat(){struc...
c语言
长
整数
加减乘除,用
C语言
实现
超
长
整数
的加减乘除四则
运算
用
C语言
实现
超
长
整数
的加减乘除四则
运算
杜丽娟;鞠宏军【期刊名称】《电脑开发与应用》【年(卷),期】2003(016)005【摘要】通过对
C语言
链表、字符串的应用,解决了高级程序设计语言处理数据存储空间的问题,
实现
了高级语言数据类型无法完成的超
长
整数
、高精度加减乘除四则
运算
,并对其
实现
算法进行了简要的分析和介绍.【总页数】3页(36-38)【关键词】程序设计语言;链表;字符串;存储空间;
长
整数
;四则...
C语言
实现
大数
运算
(
长
整数
的加、减、乘、除)
由于整型数的位数有限,因此整型数不能满足大
整数
(超
长
整数
)的
运算
要求 。大
整数
计算是利用字符串来表示大
整数
,即用字符串的一位字符表示大
整数
的一位数值,然后根据四则
运算
规则
实现
大
整数
的四则
运算
。 ...
C语言
大数加法
实现
:超
长
整数
运算
与工业级注释规范
大数加法是计算机基础中处理超出原生类型范围
整数
的核心技术,其本质是字符串模拟竖式
运算
,涉及ASCII与数值转换、内存手动管理、边界条件控制等底层原理。该技术广泛应用于密码学、金融计算、嵌入式高精度场景,尤其在无STL环境(如STM32)中必须依赖纯C的char数组与malloc/free
实现
。本文聚焦于可读性与健壮性并重的工程实践,详解如何通过三级注释体系、预分配策略、符号分离处理及12类边界测试,构建新人可读、老手可复用、编译器可移植的生产级代码,覆盖从'0000'到百万位数字的全场景需求。
C++ 语言
65,214
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章