关于c语言超长正整数相加的问题,。求高手指教!!!!!

作者&投稿:生纪 (若有异议请与网页底部的电邮联系)
两个超长正整数相加(C语言)~

既然楼主要求用C语言,那就用经典的C指针吧
#include
#include
#include
char* BigIntAdd(char* x,char* y,char* z)
{
int lenx=strlen(x);
int leny=strlen(y);
char *pmax=x,*pmin=y,*pz=z,*p1,*p2,t;
int i,lenmax=lenx,lenmin=leny;
if (lenx < leny)
{
pmax=y;
pmin=x;
lenmax=leny;
lenmin=lenx;
}
p1=pmax+lenmax-1;
p2=pmin+lenmin-1;
while(p2>=pmin)
{
*pz = *p1 + *p2 -'0';
if (pz>z && *(pz-1)>='0'+10)
{
*(pz-1)-=10;
*pz+=1;
}
p1--;p2--;pz++;
}
for(i=0;i<lenmax-lenmin;i++)
{
*pz=*p1;
if (pz>z && *(pz-1)>='0'+10)
{
*(pz-1)-=10;
*pz+=1;
}
pz++;p1--;
}
pz--;
if (*pz>='0'+10)
{
*(pz++)-=10;
*pz='1';
}
for(p1=z;p1<pz;p1++,pz--)
{
t=*p1;*p1=*pz;*pz=t;
}
return z;
}
void main()
{
system("color 1e");
char x[1024*10]={0},y[1024]={0},z[1024]={0};
printf("Enter Large Integers 1:
");
scanf("%s",x);
printf("Enter Large Integers 2:
");
scanf("%s",y);
BigIntAdd(x,y,z);
printf("

BigInt Add:
%s
+
%s
=
%s
",x,y,z);
system("pause");
}
主要想法是,判断两个数字的长短,找出最短的数字,从个位数开始与另一个数的相应位进行相加(注意判断向上进位),将结果逐个保存在结果字符串中。最后将长的那个数字 剩下的部分直接 放在结果字符串中,然后将结果字符串反转,得到结果

可以的。

我曾经编写过100的阶乘的程序。在这个程序中,计算结果达到几千位,
这样大的数据很明显是任何数据类型都表示不了的。

不过,你可以把大数用数组来表示,比如1258746这个数,你用数组
a[]={0,0,0,...,0,1,2,5,8,7,4,6}来表示,然后对数组的每一位进行运算。

按照你题目的意思,还应该定义一个大数的加法的函数,
类似
Add(int a[], int b[], int m, int n);

数组a,b是两个表示大数的数组,m,n是数组的大小。注意,要从末尾对齐。

以上是我个人的一些意见,你可以搜索并学习一下我开头提到的那个程序,即关于大数的阶乘,网上应该有很多的资料,对你解题会有帮助的。

请问楼上,实际应用中用的到大数吗?
double类型可以表示21亿大的数据,几乎可以满足绝大多数的实际应用了,更何况是单片机。

这里讨论大数的运算是一种方法、一种思路。

/*这里是头文件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<iostream.h>
#include<stdio.h>
#include"BigInt.h"
#include <windows.h>

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)//空链表时的显示
cout<<"错误!"<<endl;
else
{
node *tempnode;
tempnode=head;
while (tempnode)
{
cout<<tempnode->n;
tempnode=tempnode->next;
}
cout<<endl;
}
}

//加法
void bigint::add(const bigint &bignum1,const bigint &bignum2)
{

node *temp1,*temp2;
int rest=0,num=0;
temp1=bignum1.head;//临时节点指针1指向第一个大整数的头结点
temp2=bignum2.head;//临时节点指针2指向第二个大整数的头结点
while (temp1&&temp2)
{
num=(int(temp1->n)-48)+(int(temp2->n)-48)+rest;
if (num>9)
{
num=num-10;
rest=1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;//节点下移
temp2=temp2->next;
}
if (temp2) temp1=temp2;//当一个链表到达尾部时,另一个链表继续循环
while (temp1)
{
num=(int(temp1->n)-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 (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
temp2=temp2->next;
}
while (temp1)
{
num=(int(temp1->n)-48)+rest;
if (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
}
}
else if (num1_len<num2_len)//第二个大整数比第一个大整数长,则结果为负数
{
temp3=temp2;
temp2=temp1;
temp1=temp3;
while (temp1&&temp2)
{
num=(int(temp1->n)-48)-(int(temp2->n)-48)+rest;
if (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
temp2=temp2->next;
}
while (temp1)
{
num=(int(temp1->n)-48)+rest;
if (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
}
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 (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
temp2=temp2->next;
}
}
else //结果为负
{
temp3=temp1;
temp1=temp2;
temp2=temp3;
while (temp1)
{
num=(int(temp1->n)-48)-(int(temp2->n)-48)+rest;
if (num<0)
{
num=num+10;
rest=-1;
}
else
rest=0;
addhead(num+'0');
temp1=temp1->next;
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<<"大整数基本运算器"<<endl<<endl;;
cout<<"请选择:"<<endl;
cout<<"1.加法"<<endl;
cout<<"2.减法"<<endl;
cout<<"3.乘法"<<endl;
cout<<"按其他键退出!"<<endl<<endl;
cout<<"请输入:"<<endl;
int s;
cin>>s;
switch(s)
{
case 1:
{
cout<<"请输入第一个数字:"<<endl;
bignum1.getnum();
cout<<"请输入第二个数字:"<<endl;
bignum2.getnum();
bignum3.add(bignum1,bignum2);
cout<<"结果是:"<<endl;
bignum3.dispnum();
cout<<"按0退出,其他键继续:";
cin>>p;
p=p-48;
}break;
case 2:
{
cout<<"请输入第一个数字:"<<endl;
bignum1.getnum();
cout<<"请输入第二个数字:"<<endl;
bignum2.getnum();
bignum3.sub(bignum1,bignum2);
cout<<"结果是:"<<endl;
bignum3.dispnum();
cout<<"按0退出,其他键继续:";
cin>>p;
p=p-48;
}break;

case 3:
{
cout<<"请输入第一个数字:"<<endl;
bignum1.getnum();
cout<<"请输入第二个数字:"<<endl;
bignum2.getnum();
bignum3.mul(bignum1,bignum2);
cout<<"结果是:"<<endl;
bignum3.dispnum();
cout<<"按0退出,其他键继续:";
cin>>p;
p=p-48;
}break;
default:
p=0;
}
}
}

本想敲代码,忽然楼上已经回答,代码完整,非常专业!

#include <stdio.h>
#include <conio.h>
#include <string.h>
void add(char a[],char b[],char back[]){
int i,j,k,up,x,y,z,l;
char *c;
if (strlen(a)>strlen(b))
l=strlen(a)+2;
else
l=strlen(b)+2;
c=(char *)malloc(l*sizeof(char));
i=strlen(a)-1,j=strlen(b)-1;
k=0,up=0;
while(i>=0||j>=0){
if(i<0) x='0'; else x=a[i];
if(j<0) y='0'; else y=b[j];
z=x-'0'+y-'0';
if(up) z+=1;
if(z>9) else up=0;
c[k++]=z+'0';
i--,j--;
}
if(up) c[k++]='1';
c[k]=i=0;
for(k-=1;k>=0;k--)back[i++]=c[k];
back[i]='\0';
}

void main(){
#define MAX 5000
char a[MAX],b[MAX],c[MAX+1];
for(;;)
{
printf("★★★★(to Exit,Enter 'e' or 'E')★★★★★");
printf("\n★★Enter two numbers with [enter]-key:★★\n>>");
scanf("%s",&a);
if(a[0]=='e'||a[0]=='E')return;
printf(">>");
scanf("%s",&b);
add(a,b,c),printf("%s\n",c);
}
}



怎么用数组完成两个超长的正整数乘法
\/\/例如:100011 1011101\/\/程序中先输入被乘数和乘数的位数,然后动态申请空间,以字符的形式来处理长整数的每一位。\/\/模拟计算机乘法的实现,将整数乘法转换为整数加法来实现。#include <stdio.h>\/\/将数组元素左移x位,低位补0 void leftmove(char c[],int len,int x){ if(x==0)return ;else ...

c语言求解。 从键盘输入一个正整数N,再输入N个整数,按从小到大的顺序...
推荐于2017-12-16 17:14:17 最佳答案 #include<stdio.h>void main(){ int n,i,j,t,a[10000]; printf("请输入正整数N:"); scanf("%d",&...C语言超长正整数相加 c语言正整数 C语言译密码 C语言解码游戏 C语言数组元素逆置 C语言字符统计 funC语言 从键盘输入10个整数 将一个整数因式...

输出double形数据,用%什么
完整输出double形数据,使用%f格式。C99标准规定用%f输出double类型,%lf等价于%f(可以在"%"和字母之间加小写字母l, 表示输出的是长型数)。示例代码如下图:g把输出的值按照%e或者%f类型中输出长度较小的方式输出。

求问一道超难的C语言题
"The key is already in this array! "); else printf("The key is inserted in position %d ",flag);}昨天写好没测试,今天测了才发给你。这道题目确实不难,只是题目长。另外关于输出格式有点含糊,如果我理解有偏差,你看了我的代码改下就好。主要逻辑都已经实现了。

在哪里能买到《 c语言实例解析精粹 》书啊,或者谁能发一份视频以及光盘...
以下为它的具体目录:C语言实例解析精粹(第二版) 电子书及源代码 附清晰版电子书及源代码 第一部分 基础篇 实例1 第一个C程序 实例2 运行多个源文件 实例3 求整数之积 实例4 比较实数大小 实例5 字符的输出 实例6 显示变量所占字节数 实例7 自增\/自减运算 实例8 数列求和 实例9 乘法...

用C语言程序编辑对于一次考试成绩进行统计,考M科,有N人(如10人)参加...
j]\/i;} for(i=0,p=0;i<n;i++){ p++;for(j=0;j<m;j++)if(N[i][j]<M[j]){p--;break;} } for(i=0;i<n;i++)printf("%.2lf%c",R[i],i<(n-1)?' ':'\\n');for(j=0;j<m;j++)printf("%.2lf%c",M[j],j<(m-1)?' ':'\\n');} return 0;} ...

华为C语言上机试题
include <stdio.h> include <assert.h> void take_num(const char *strIn, int *n, unsigned int *outArray){ const char * p = strIn;int next = 0;n = 0;outArray[0] = 0;assert(p && n && outArray);for (p = strIn;*p;p++) { if (('0'<=*p)&&(*p<='9')) { o...

c语言long long short什么意思?
long short。long 是长整型。 short是短整型。一个变量不可能又长又短。C 语言标准定义了以下整数类型:1) short int(可简写为 short),和 int 一样,也是有符号整数 2) long int(简写:long),有符号整数 3) long long int(简写:long long),C99 标准添加的类型,有符号整数 4) ...

编写一个程序,实现冒泡排序,将输入的n个整数按降序进行排序,并输出...
如图所示:short:修饰int,短整型数据,可省略被修饰的int。(K&R时期引入)long:修饰int,长整型数据,可省略被修饰的int。(K&R时期引入)long long:修饰int,超长整型数据,可省略被修饰的int。(C99标准新增)signed:修饰整型数据,有符号数据类型。(C89标准新增)unsigned:修饰整型数据,无符号...

赤水市19463613570: 关于c语言超长正整数相加的问题,.求高手指教!!!!! -
经狄蒲地: /*这里是头文件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(...

赤水市19463613570: 两个超长正整数相加(C语言) -
经狄蒲地: 大整数不应该用double做为输入,应该用字符串,代码如下:#include <iostream>#include <string>#include <sstream> using namespace std; const int M = 4; const int N = 99; void Sort(int a[], string z) { int i = 0; int zlen = 0; while (z.size() > 0) { zlen ...

赤水市19463613570: C语言大作业 题目是:请设计一个算法完成两个超长正整数的加法.谁会的帮帮忙? -
经狄蒲地: 有没有具体的要求,比方说数据结构方面,我这有一个,你可以参考参考#include"stdio.h"#include"stdlib.h"#define n 10 /*假定系统允许的最大作业为n,假定模拟实验中n值为10*/#define m 10 /*假定系统允许的空闲区表最大为m,假定模拟...

赤水市19463613570: 用c语言实现超长整数的加法运算 -
经狄蒲地: #include "stdio.h" int sum(int a[],int b[],int c[]) { int i=0,j=0,f=0; for(;i=0;i--) scanf("%4d",&a[i]); } void main() { static int a[20],b[20],c[21],i,n,f; char d[80]; get(a); get(b); f=sum(a,b,c); for(i=f;i>=0;i--) printf("%4d",c[i]); }

赤水市19463613570: 用C语言编程计算两个超长正整数(长度小于100)的加法 -
经狄蒲地: 这是大数加法问题,我给你一个参考代码 输入至多100行的文本,每行是一个至多100位的十进制无符号大整数,最后的输入行为零.要求计算出这些大整数的和,并以十进制方式显示.示例输入:...

赤水市19463613570: 用C语言编程“使用数组完成两个超长(长度小于100)正整数的加法.” -
经狄蒲地: #include<stdio.h>#include<string.h> void f(char* from,char* to) { int i,n; n=strlen(from); for(i=0;i<n;i++) to[n-1-i]=from[i]; to[n]='\0'; } int main(int argc, char* argv[]) { int i; char a[101],b[101],c[101],d='0'; //声明1001个长度的数组,就能求1000位的加法 ...

赤水市19463613570: 数据结构c语言:超长正整数相加 正整数不超过五位,并求和 -
经狄蒲地: 要求 :存储采用顺序表, 建立顺序表函数,,输出顺序表函数,,相加函数和main函数. 设计几组测试数据

赤水市19463613570: 用C语言编写一个程序,实现任意长度的两个正整数的加法运算 -
经狄蒲地: 整数有范围的 建议用三个整型数组,一个表示加数,一个表示被加数,另一个表示结果 每个元素只有5位或者别的固定的位数,这样就是几千位也没关系 别的我就不说了,太简单了

赤水市19463613570: 怎样用C语言做超大整数的加减运算?
经狄蒲地: 用高精度算法来实现,即用数组或指针来储存数字,例如A〔20〕来储存a ,用B〔20〕来储存b,这样a 和b就可以是很大的数,再用一个C〔21〕来储存结果,为什么C要21呢,你知道,加法是要近位的,呵呵.这里给出相加的伪代码,d =0/*用来存储近位*/,for i=0到19{c=A〔i〕+B〔i〕+d ,d =c/10,c=c%10,C〔i〕=c}if d 不等于0 C〔i+1〕=d ,再逆的输出C就可以了!编程要学会思考,现在你可以试试编下高精度乘法,例如可以输出100的阶乘!

赤水市19463613570: c语言大数相加,求解 -
经狄蒲地: 溢出了,int也就是6万多,long也就是10几位,再多了,就报错了.这个你该知道 大数,需要字符串来处理,不能简单的相加就好 需要每个位,也就是ab的每个单元相加,个位对齐,进位处理

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网