c++如何实现两个长度很大(最大256位)的数字加减运算

2025-03-01 15:19:42
推荐回答(4个)
回答1:

#ifndef NODE
#define NODE
class Node
{
public:
char cBit;
Node* pNext;
Node(char c):cBit(c)
{
pNext=0;
}
Node operator=(const Node& node)
{
this->pNext=node.pNext;
this->cBit=node.cBit;
return *this;
}
};
#endif
/**************************/
#ifndef STACK
#define STACK
#include "node.h"
#include "iostream.h"
#include "process.h"
class stack
{
public:
stack()
{
pTop=NULL;
nSize=0;
}
explicit stack(char);
stack(stack&);
stack operator=(stack&);

bool push(char);
char pop();
char Iter(int &);
bool empty(){
return nSize<=0;
}
int GetSize() const
{
return nSize;
}
private:
Node* pTop;
int nSize;
};
stack::stack(char init):nSize(1)
{
pTop=NULL;
Node* pNewNode=new Node(init);
if (pNewNode)
{
pTop=pNewNode;
pTop->pNext=NULL;
}
else
{
cerr<<"Memory Allocation Failed!\n";
exit(1);
}
}
stack::stack(stack& sStack)
{
pTop=NULL;
nSize=0;
int nIndex=sStack.GetSize();
stack* temp=new stack;
if(temp)
while(nIndex)
{
temp->push(sStack.Iter(nIndex));
}
else
{
cerr<<"Memory Allocation Error!\n";
exit(0);
}
while (!temp->empty())
{
this->push(temp->pop());
}
delete temp;
temp=NULL;
}
stack stack::operator =(stack& sStack)
{
int nIndex=sStack.GetSize();
stack* temp=new stack;
if(temp)
while(nIndex)
{
temp->push(sStack.Iter(nIndex));
}
else
{
cerr<<"Memory Allocation Error!\n";
exit(0);
}
while (!temp->empty())
{
this->push(temp->pop());
}
delete temp;
temp=NULL;
return *this;
}
bool stack::push(char cNum)
{
Node* pNewNode=new Node(cNum);
if (pNewNode)
{
pNewNode->pNext=pTop;
pTop=pNewNode;
nSize++;
return true;
}
else
return false;
}
char stack::pop()
{
if (nSize>0)
{
char cTop=pTop->cBit;
Node* pTempTop=pTop;
pTop=pTop->pNext;
nSize--;
delete pTempTop;
pTempTop=NULL;
return cTop;
}
else return '\0';
}
char stack::Iter(int & nIndex)
{
if (nIndex<=0||nIndex>nSize)
return '\0';
Node* pFromTop=pTop;
for (int i=nSize-nIndex;i>0;i--)
{
pFromTop=pFromTop->pNext;
}
nIndex--;
return pFromTop->cBit;
}
#endif
/*********************************/
#ifndef SUPNUM
#define SUPNUM
#include "Stack.h"
#include "conio.h"
unsigned int pow(unsigned int,int);
class SupNum
{
public:
SupNum();
explicit SupNum(char);
SupNum(SupNum&);

SupNum operator+(SupNum &);
SupNum operator*(SupNum &);
SupNum operator+=(SupNum & Right_op)
{
*this=*this+Right_op;
return *this;
}
SupNum operator*=(SupNum & Right_op)
{
*this=*this*Right_op;
return *this;
}

SupNum exp(int);
int GetStackSize(){
return pNumStr->GetSize();
}
stack* & GetStack(){
return pNumStr;
}

friend ostream& operator<<(ostream& ,SupNum&);
friend istream& operator>>(istream& ,SupNum&);
private:
stack* pNumStr;
SupNum operator*(int);
};
//////////////////////////////
SupNum::SupNum()
{
pNumStr=new stack;
if (!pNumStr)
{
cerr<<"Memory Allocation Failed!\n";
exit(1);
}
}
SupNum::SupNum(char init)
{
pNumStr=new stack(init);
if (!pNumStr)
{
cerr<<"Memory Allocation Failed!\n";
exit(1);
}

}
SupNum::SupNum(SupNum& Sup)
{
pNumStr=new stack;
if (!pNumStr)
{
cerr<<"Memory Allocation Failed!\n";
exit(1);
}
*pNumStr=*(Sup.GetStack());
}
SupNum SupNum::operator *(int iMulti)
{
SupNum temp;
for (int i=0;itemp+=*this;
}
return temp;
}

ostream& operator<<(ostream& os,SupNum& sup )
{

stack temp;
int index=sup.GetStackSize();
while(index)
temp.push(sup.GetStack()->Iter(index));
while (!temp.empty())
{
os<}
return os;
}
istream& operator >>(istream& is,SupNum& sup)
{
char bit='0';
while (1)
{
bit=getche();
if(bit!=' ')
break;
}
while (1)
{
if(bit>='0'&&bit<='9')
sup.GetStack()->push(bit);
bit=getche();
if(bit==' '||bit=='\r')
break;
}
if(bit=='\r')
cout<return is;
}
SupNum SupNum::operator +(SupNum & Right_op)
{
int check=1;
if(Right_op.GetStack()->Iter(check)=='0')
return *this;
int tag=0;
char sum='\0';

SupNum thisSup=*this;
SupNum RightSup=Right_op;

int Right_op_size=RightSup.GetStackSize();

SupNum* pTempresult=new SupNum;
if(pTempresult==NULL) exit(1);

SupNum pResult;

while (!thisSup.GetStack()->empty()&&!RightSup.GetStack()->empty())
{
sum=thisSup.GetStack()->pop()+RightSup.GetStack()->pop()-'0';
if (sum+tag>'9')
{
pTempresult->GetStack()->push(char(sum+tag-10));
tag=1;
}
else
{
pTempresult->GetStack()->push(char(sum+tag));
tag=0;
}

}//the first

SupNum *pBigger=&(this->GetStack()->GetSize()>Right_op_size?thisSup:RightSup);
if(tag==0)
while(!pBigger->GetStack()->empty())
pTempresult->GetStack()->push(pBigger->GetStack()->pop());
else
if(thisSup.GetStackSize()==Right_op_size)
pTempresult->GetStack()->push('1');
else
{
while (!pBigger->GetStack()->empty())
{
sum=pBigger->GetStack()->pop();
if (sum+tag>'9')
{
pTempresult->GetStack()->push(char(sum+tag-10));
tag=1;
}
else
{
pTempresult->GetStack()->push(char(sum+tag));
tag=0;
}
}//while
if(tag)
pTempresult->GetStack()->push('1');
}//else
while (!pTempresult->GetStack()->empty())
pResult.GetStack()->push(pTempresult->GetStack()->pop());
delete pTempresult;
return pResult;
}

SupNum SupNum::operator*(SupNum& Right_op)
{
int check=1;
if(Right_op.GetStack()->Iter(check)=='0')
return SupNum('0');
SupNum supResult;
int index=Right_op.GetStackSize();
int iRopSize=index;
int* pNumStr=new int[iRopSize];
for (int i=0;ipNumStr[i]=(Right_op.GetStack()->Iter(index)-'0')*pow(10,i);
for (int k=0;ksupResult+=*this*(pNumStr[k]);
return supResult;
}
SupNum SupNum::exp(int nIndex)
{
SupNum temp=*this;
for(int i=1;itemp=temp*(*this);
return temp;
}
unsigned int pow(unsigned int x,int e)
{
int iResult=1;
for (int i=0;iiResult*=x;
return iResult;
}
#endif
/*****************************/
#include "SupNum.h"
int main()
{
SupNum num_1,num_2;
cout<<"输入两个数:"<cin>>num_1>>num_2;
SupNum num3;
cout<<"num_1="<cout<<"num_2="<num3=num_1.exp(10);
cout<cout<<"按任意键继续...";
getch();
return 0;
}

回答2:

下面的例子支持两个正整数相加,可以拿去参考一下。
#include
#include

using namespace std;

int main()
{
string num1, num2;
int len1, len2, i, j;
cout << "输入两个数字:" << endl;
cin >> num1 >> num2;
len1 = num1.length(), len2 = num2.length();
i = len1 - 1, j = len2 - 1;

if (len1 > len2)
{
for (; j >= 0; --i, --j)
{
char temp = num1[i] + (num2[j] - '0');
if (temp > '9')
{
num1[i] = temp - 10;
num1[i - 1] += 1;
}
else
num1[i] = temp;
}

for (;i > 0; --i)
{
if (num1[i] > '9')
{
num1[i] -= 10;
num1[i - 1] += 1;
}
}

if (num1[0] > '9')
{
num1[0] -= 10;
cout << '1';
}

cout << num1 << endl;
}
else
{
char temp;
int inc = 0;

for (; i >= 0; --i, --j)
{
temp = num2[j] + (num1[i] - '0');
if (temp > '9')
{
num2[j] = temp - 10;

if (j - 1 >= 0)
num2[j - 1] += 1;
else
inc = 1;
}
else
num2[j] = temp;
}

for (;j > 0; --j)
{
if (num2[j] > '9')
{
num2[j] -= 10;
num2[j - 1] += 1;
}
}

if (num2[0] > '9')
{
num2[0] -= 10;
cout << '1';
}
else if (inc)
cout << '1';

cout << num2 << endl;
}

return 0;
}

回答3:

用数组来模拟大数字,然后按照小学时候排竖式的方法来运算.
百度知道这个版块以前有不少贴子是关于这个问题的,你可以搜索一下

回答4:

提示一下
int a[],b[];