关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/04 01:15:09
![关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求](/uploads/image/z/7836611-59-1.jpg?t=%E5%85%B3%E4%BA%8Ec%E8%AF%AD%E8%A8%80%E8%B6%85%E9%95%BF%E6%AD%A3%E6%95%B4%E6%95%B0%E7%9B%B8%E5%8A%A0%E7%9A%84%E9%97%AE%E9%A2%98%2C.%E8%AE%BE%E6%9F%90%E6%9C%BA%E5%99%A8%E8%A1%A8%E7%A4%BA%E7%9A%84%E6%AD%A3%E6%95%B4%E6%95%B0%E4%B8%8D%E8%B6%85%E8%BF%875%E4%BD%8D%E5%8D%81%E8%BF%9B%E5%88%B6%E6%95%B0%E5%AD%97.%E8%AF%95%E9%87%87%E7%94%A8%E9%A1%BA%E5%BA%8F%E8%A1%A8%E8%A1%A8%E7%A4%BA%E4%BB%BB%E6%84%8F%E9%95%BF%E7%9A%84%E6%AD%A3%E6%95%B4%E6%95%B0%2C%E5%B9%B6%E8%AE%BE%E8%AE%A1%E8%AE%A1%E7%AE%97%E4%B8%A4%E4%B8%AA%E6%AD%A3%E6%95%B4%E6%95%B0%E4%B9%8B%E5%92%8C%E7%9A%84%E7%A8%8B%E5%BA%8F.%E8%A6%81%E6%B1%82%EF%BC%9A1%E3%80%81%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84%E9%87%87%E7%94%A8%E9%A1%BA%E5%BA%8F%E7%BB%93%E6%9E%84.2%E3%80%81%E8%A6%81%E6%B1%82)
关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求
关于c语言超长正整数相加的问题,.
设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.
要求:
1、存储结构采用顺序结构.
2、要求程序由以下函数组成:建立顺序表函数、输出顺序表函数、相加函数和main()函数.
3、设计几组测试数据.
4、程序能正常运行,输出正确结果.
关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求
/*这里是头文件BigInt.h*/
class bigint
{
struct node //节点
{
char n;
node *next;
};
node *head,*end,*temp;//头结点,尾节点,临时节点
void addhead(char n);//增加头结点
void addend(char n);//增加尾节点
public:
bigint();
~bigint();
void getnum();//获取大整数
void dispnum();//显示
void add(const bigint &bignum1,const bigint &bignum2);
void sub(const bigint &bignum1,const bigint &bignum2);
void mul(const bigint &bignum1,const bigint &bignum2);
};
/*主文件BigInt.cpp*/
#include
#include
#include"BigInt.h"
#include
bigint::bigint()
{
head=end=temp=NULL;
}
//析构
bigint::~bigint()
{
node *nextnode;
if (head==NULL)
return;
temp=head;
while (temp) //删除节点
{
nextnode=temp->next;
delete temp;
temp=nextnode;
}
head=end=temp=NULL;
}
void bigint::addhead(char n)//增加头结点
{
temp=new node;
temp->n=n;
temp->next=NULL;
if (!head)
{
head=end=temp;
temp->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void bigint::addend(char n)//增加尾节点
{
temp=new node;
temp->n=n;
temp->next=NULL;
if (!end)
{
head=end=temp;
temp->next=NULL;
}
else//链表非空时,尾节点的指针指向临时节点,然后把临时节点赋给尾节点
{
end->next=temp;
end=temp;
}
}
void bigint::getnum()//获取大整数
{
char key;
while ((key=getchar())!=10)//判断是否是回车
{
addhead(key);
}
}
void bigint::dispnum()//显示大整数
{
if (!head)//空链表时的显示
coutn)-48)+rest;
if (num>9)
{
num=num-10;
rest=1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
}
if (rest)//判断循环结束后是否有进位
addhead(rest+48);
}
void bigint::sub(const bigint &bignum1,const bigint &bignum2) //减法
{
bigint tempa,tempb,tempc;
node *temp1,*temp2,*temp3,*temp4,*temp5;
int num1_len=0,num2_len=0;//统计两个大整数的长度
int num=0,rest=0;
temp1=bignum1.head;
temp2=bignum2.head;
while (temp1)
{num1_len++;temp1=temp1->next;}
while (temp2)
{num2_len++;temp2=temp2->next;}
temp1=bignum1.head;
temp2=bignum2.head;
if (num1_len>num2_len)//当第一个大整数比第二个大整数长时,结果为正数
{
while (temp1&&temp2)
{
num=(int(temp1->n)-48)-(int(temp2->n)-48)+rest;
if (numnext;
temp2=temp2->next;
}
while (temp1)
{
num=(int(temp1->n)-48)+rest;
if (numnext;
}
}
else if (num1_lenn)-48)-(int(temp2->n)-48)+rest;
if (numnext;
temp2=temp2->next;
}
while (temp1)
{
num=(int(temp1->n)-48)+rest;
if (numnext;
}
addhead('-');
}
else//一样长时,从头位往后依次判断各个对应位数字的大小,以判断结果的正负
{
temp1=bignum1.head;
while (temp1)
{tempa.addhead(temp1->n);temp1=temp1->next;}
temp4=tempa.head;
temp2=bignum2.head;
while (temp2)
{tempb.addhead(temp2->n);temp2=temp2->next;}
temp5=tempb.head;
temp1=bignum1.head;
temp2=bignum2.head;
while (temp4->n==temp5->n)//相同时的情况
{
temp4=temp4->next;
temp5=temp5->next;
if (temp4==NULL) break;
}
if (temp4==NULL)
addend('0');
else if (temp4->n>temp5->n)//结果为正
{
while (temp1)
{
num=(int(temp1->n)-48)-(int(temp2->n)-48)+rest;
if (numnext;
temp2=temp2->next;
}
}
else //结果为负
{
temp3=temp1;
temp1=temp2;
temp2=temp3;
while (temp1)
{
num=(int(temp1->n)-48)-(int(temp2->n)-48)+rest;
if (numnext;
temp2=temp2->next;
}
addhead('-');//向头结点增加负号
}
}
}
//乘法
void bigint::mul(const bigint &bignum1,const bigint &bignum2)
{
bigint Tempa,Tempb,result;
node *temp,*temp1,*temp2,*tempa,*tempb;
int num=0,num2=0,i=0,k=0,rest,rest2;
temp1=bignum1.head;
temp2=bignum2.head;
while (temp2)
{
rest=0;rest2=0;//归零
if (result.head!=NULL)
{result.head=result.end=NULL;}//清空结果链表
while (temp1!=NULL)//用第二个大整数的一位与第一个大整数的每一位相乘,结果存入临时链表Tempa中
{
num=(int(temp1->n)-48)*(int(temp2->n)-48)+rest;
if (num>9)
{
rest=num/10;
num=num%10;
}
else
rest=0;
Tempa.addend(num+48);
temp1=temp1->next;
}
if (rest!=0) Tempa.addend(rest+48);
for(k=i;k>=1;k--) {Tempa.addhead(0+48);}//每循环依次,临时链表都要在尾部补零,类似于手算乘法
i++;
temp1=bignum1.head;
temp2=temp2->next;
tempa=Tempa.head;
tempb=Tempb.head;
while (tempa!=NULL&&tempb!=NULL)//以下为两大整数的相加运算,与加法算法相同.
{
num2=(int(tempa->n)-48)+(int(tempb->n)-48)+rest2;
if (num2>9)
{
num2=num2-10;
rest2=1;
}
else
rest2=0;
result.addend(num2+48);
tempa=tempa->next;
tempb=tempb->next;
}
if (tempb!=NULL) tempa=tempb;
while (tempa!=NULL)
{
num2=(int(tempa->n)-48)+rest2;
if (num2>9)
{
num2=num2-10;
rest2=1;
}
else
rest2=0;
result.addend(num2+48);
tempa=tempa->next;
}
if (rest2) result.addend(rest2+48);
if (Tempa.head!=NULL) {Tempa.head=Tempa.end=NULL;}//对临时链表a置空
if (Tempb.head!=NULL) {Tempb.head=Tempb.end=NULL;}//对临时链表b置空
if (result.head!=NULL)
{
node *t=result.head;
while (t)
{Tempb.addend(t->n);t=t->next;}//将结果链表复制给临时链表b,用来下一次的相加运算
}
}
if (result.head!=NULL)
{
temp=result.head;
while (temp)
{addhead(temp->n);temp=temp->next;}
}
}
void main() //主函数
{
bigint bignum1,bignum2,bignum3;
char p='1';
while (p)
{
system("cls");
cout