关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/09 20:44:11
关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求

关于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

关于c语言超长正整数相加的问题,.设某机器表示的正整数不超过5位十进制数字.试采用顺序表表示任意长的正整数,并设计计算两个正整数之和的程序.要求:1、存储结构采用顺序结构.2、要求 C语言大作业 请设计一个算法完成两个超长正整数的加法. C语言大作业 请设计一个算法完成两个超长正整数的加法.要求用数组和指针做的 最好能画流程图 C语言中的通用两数相加问题 ,综合小数相加,分数相加,整数相加以及不同进制之间的两数相加要源代码 一道关于C语言函数的问题. 一道关于C语言函数的问题. 多项式相加C语言 C#或者C/C++语言中一维数组中的元素相加的问题.会的朋友,希望不吝赐教!如何实现在一个固定长度的数组中(数组元素为0至30的有序正整数),把任意6个元素相加,得到自己想要的任意一个和值 C语言 素数求和问题输入一个正整数 repeat (0 关于C语言的一个简单的问题 .if(a 从键盘输入一个四位的正整数X,c语言编程计算X的每一个数字相加之和,怎么写以及思路 用C语言写两个正整数相加的函数,只能用逻辑运算符,就是写一个unsigned int addr(unsigned int,unsigned int);不能用加减号 关于c语言产生一个随机数的问题怎么修改 关于C语言的, 关于c语言的, 关于数组中元素相加的问题int c[5];for(a=0;a c语言1到10奇数相加的程序 C语言的一个问题,关于条件循环的编辑一个求矩形面积的句子,要求输入值中有负数时,输出语句“请输入正整数!”,否则求出面积