C++编程(返回任意大的两整数之和)
发布网友
发布时间:2023-05-24 14:49
我来回答
共3个回答
热心网友
时间:2023-10-12 10:25
我有个大整数阶乘的程序。其中有个BigInt类,有一个Add方法可以参考。
缺一个把输入的字串每4位截开,初试化大整数的程序,但是已经很容易了。你看看吧。
bigint.h
---------------------
#pragma once
#define LMAX 10000
struct _Node
{
int Value;
struct _Node *pNext;
struct _Node *pPrev;
};
typedef struct _Node Node, *PNode;
class CBigInt
{
PNode pHead;
PNode pTail;
int iSize;
public:
CBigInt(int n);
public:
~CBigInt(void);
void Multply(int iNumber);
void MultplyBig(CBigInt);
void CalcF(int n);
char* ToString();
void setValue(int l);
void ClearList();
void AppendNode(int l);
};
------------------------
bigint.cpp
------------------------
#include "StdAfx.h"
#include "BigInt.h"
CBigInt::CBigInt(int n)
{
this->iSize = 0;
this->pHead = NULL;
this->setValue(n);
}
CBigInt::~CBigInt(void)
{
this->ClearList();
}
void CBigInt::setValue(int l)
{
this->ClearList();
this->AppendNode(l);
}
void CBigInt::AppendNode(int l)
{
PNode pNewNode = new Node();
pNewNode->Value = l;
// 如果是个空链,那么挂到第一链上。否则正常挂在最后一链
if (this->pTail == NULL)
{
pNewNode->pPrev = NULL;
pNewNode->pNext = NULL;
this->pHead = pNewNode;
this->pTail = pNewNode;
}
else
{
pNewNode->pNext = NULL;
pNewNode->pPrev = this->pTail;
this->pTail->pNext = pNewNode;
this->pTail = pNewNode;
}
this->iSize ++;
}
void CBigInt::ClearList()
{
PNode p = this->pHead;
PNode pTmp;
while(p != NULL)
{
pTmp = p->pNext;
delete p;
p = pTmp;
}
this->pHead = NULL;
this->pTail = NULL;
this->iSize = 0;
}
void CBigInt::Multply(int iNumber)
{
// 从低位向高位乘,不考虑进位
PNode p = this->pHead;
while (p != NULL)
{
p->Value = p->Value * iNumber;
p = p->pNext;
}
// 从低位再向高位进一遍位.
p = this->pHead;
while(p != NULL)
{
if (p->Value >= LMAX)
{
if (p->pNext == NULL)
{
this->AppendNode(0);
}
p->pNext->Value = p->pNext->Value + p->Value / LMAX;
p->Value = p->Value % LMAX;
}
p = p->pNext;
}
return;
}
void CBigInt::CalcF(int n)
{
this->setValue(1);
for (int i = 2; i <= n; i++)
{
this->Multply(i);
}
}
char * CBigInt::ToString()
{
// 计算链表的长度,分配足够的字符串
char *s;
char sTmp[20];
if (this->iSize == 0 )
{
s = new char[2];
sprintf_s(s, 2, "0");
return s;
}
int iLen = (this->iSize + 1 )* 9;
s = new char[iLen];
// 倒序10进制输出结果
// 第一个不要加0,之后的要补0.
sprintf_s(s, iLen, "%ld", this->pTail->Value);
PNode p = this->pTail->pPrev;
while(p != NULL)
{
::sprintf_s(sTmp, 20, "%04ld", p->Value);
::strcat_s(s, iLen, sTmp);
p = p->pPrev;
}
return s;
}
char * CBigInt::Add(CBigInt bi2)
{
// 从低位向高位加,不考虑进位
PNode p1 = this->pHead;
PNode P2 = bi2->pHead;
while (p1 != NULL && p2 != NULL)
{
p1->Value = p1->Value + p2->Value;
p1 = p1->pNext;
p2 = p2->pNext;
}
// bi2比较长, 把bi2长出来的部分加到自身
if (p1 == NULL)
{
while(p2!=NULL)
{
this->AppendNode(p2->Value);
p2 = p2->pNext;
}
}
// 从低位再向高位进一遍位.
p = this->pHead;
while(p != NULL)
{
if (p->Value >= LMAX)
{
if (p->pNext == NULL)
{
this->AppendNode(0);
}
p->pNext->Value = p->pNext->Value + p->Value / LMAX;
p->Value = p->Value % LMAX;
}
p = p->pNext;
}
return;
}
-----------------------------
热心网友
时间:2023-10-12 10:26
大整数的运算跟存储方式是相关的, 先贴贴输入输出的代码吧
热心网友
时间:2023-10-12 10:26
lz,你强调的是"任意大"?还是两整数?