当前位置:首页 » 编程语言 » c语言加长整数
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言加长整数

发布时间: 2022-10-18 01:12:38

A. c语言超长整数相加

#include <stdio.h>#include <string.h>#define MAXLEN 1000void longadd(char* s1,char* s2 ,int* s3)
{ int arr1[MAXLEN]; int arr2[MAXLEN]; int arr3[MAXLEN]; int i; //求字符串长度倒序放在int 数组里面
int len1 = strlen(s1); for( i = 0; i < len1; i++)
{
arr1[i] = s1[len1-1-i] - '0';
} //求字符串长度倒序放在int 数组里面
int len2 = strlen(s2); for( i = 0; i < len2; i++)
{
arr2[i] = s2[len2-1-i] - '0';
} //加法运算
for( i = 0; i < MAXLEN; i++)
{
arr3[i] = arr1[i] + arr2[i];
} //数据处理超过9的进行取余并且进位
for( i = 0; i < MAXLEN; i++)
{ if(arr3[i] >= 10)
{
arr3[i+1] += arr3[i] / 10;
arr3[i] = arr3[i] % 10;
}
s3[i] = arr3[i];
} return;
}int main(int argc, char const *argv[])
{ char buff1[MAXLEN]; char buff2[MAXLEN]; int arr[MAXLEN]; puts("input 1:");
gets(buff1); puts("input 2:");
gets(buff2);

longadd(buff1,buff2,arr); //通过z变量的值过滤前面的0 倒序输出
int z = 0; for(int j =MAXLEN -1 ; j >= 0 ; j-- )
{ if(z == 0)
{ if(arr[j] != 0)
{ printf("%d",arr[j]);
z= 1;
}
} else
{ printf("%d",arr[j]);
}
} //如果z值没有改变则输出0
if( z== 0)
{ printf("0\n");
} return 0;
}

B. 用C语言编程计算两个超长正整数(长度小于100)的加法

这是大数加法问题,我给你一个参考代码

输入至多100行的文本,每行是一个至多100位的十进制无符号大整数,最后的输入行为零。要求计算出这些大整数的和,并以十进制方式显示。

示例输入:
0

示例输出:#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
const int n = 100;// 数字的最大位数
const int b = 10; // 数字的进制
char r[n+1] = {0};// 加数
char s[n+1] = {0};// 求和
while (scanf("%s", r) && strcmp(r, "0")) { // 对每一个加数...
for (int i = strlen(r)-1, j = n-1; i >= 0; i--, j--) { // 中的每一个数字位
r[i] -= '0'; // char -> int
for (int k = j, c = 0; 1; k--) { // update s
const int sum = (!c ? r[i] : 0) + s[k] + c; // local sum
if ((c = sum > b - 1)) s[k] = sum - b; // do update
else { s[k] = sum; break; } // end update
}
}
}
for (int i = 0; i < n; putchar(s[i++] + '0'));
return 0;
}

C. 如何用C语言实现长整数的运算

如果你的编译器支持c99标准那么用 long long 来定义64位的整数,这样的数已经很大了!
当然c中可以自己定义有关大数储存运算的函数.要求熟悉数据结构的很多基本知识.当然也可以变通的去实现大数的运算!
举个例子:求10000!的程序:
1.变通的方法求1000!
#include<math.h>
#include<stdio.h>
int main()
{
long i;
double ln=0;
for(i=1;i<=10000;i++)
ln+=log10(i);
i=(long)ln;ln=pow(10,ln-i);
printf("10000!=%lf*10^%ld\n",ln,i);
return 0;
}
2.如果熟悉数据结构的知识可以用下面的方法解决
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
short mul(short a[],short d,short x)
{
long i,y=0;
for(i=0;i<d;i++)
{
y+=a[i]*(long)x;
a[i]=(short)(y%10000);
y/=10000;
}
a[d]=(short)y;
return d+!!y;
}
void main()
{
long s;
short *a,i,j,n,ws=1;
printf("N=");scanf("%d",&n);
#define Pi 3.14159265358979323846L
s=(long)((log(2*Pi*n)/2+n*(log(n)-1))/log(10)+1);
a=(short*)malloc((s/4+2)*sizeof(short));*a=1;

for(i=2;i<=n;i++)
ws=mul(a,ws,i);

printf("%d!=%d",n,a[ws-1]);
for(j=ws-2;j>=0;j--)
printf("%04d",a[j]);
printf("\n");
free(a);
}
===============================================

编程爱好者群:24410693 只要对c有兴趣就可以申请加入本群.

D. C语言如何计算长整数啊

#include"stdio.h"
#include"string.h"
#include<stdlib.h>

//数组长度对齐
voidAlignment(charc[],intlength_c,intlength){
inttemp=length;
while(length){
if(length_c>0){
c[length-1]=c[length_c-1];
}else{
c[length-1]='0';
}
length--;
length_c--;
}
c[temp]='';
}

intchar2int(charc){
chartemp[]="0123456789";
inti=10;
while(i){
if(c==temp[i]){
returni;
}
i--;
}
return0;
}

intmain(){
chara[50];
charb[50];

scanf("%s",&a);
scanf("%s",&b);
intlength_a=strlen(a);
intlength_b=strlen(b);
intlength;
if(length_a>length_b){
length=length_a;
Alignment(b,length_b,length_a);
}elseif(length_a<length_b){
length=length_b;
Alignment(a,length_a,length_b);
}else{
length=length_a;
}

inti_a,i_b,i_result,flag=0;//flag是进位标志
charresult[50];
inttemp=length;
while(length){
i_a=char2int(a[length-1]);
i_b=char2int(b[length-1]);
i_result=i_a+i_b+flag;
if(i_result>9){
i_result-=10;
flag=1;
}else{
flag=0;
}
result[length-1]=i_result+'0';
length--;
}
result[temp]='';

printf("%s",result);
}

ㄨ‘正典’灏皴 解释的很清楚了,就是这样做的。

E. c语言编写的任意长整数的加减法,急急急!先谢谢了

#include <iostream>

using namespace std;

template<int LIM,int MAX> class hp
{
public:
//vars
int sect[MAX];
int scnt;
//constructors
hp()
{
scnt=1;
sect[0]=0;
}
//functions
void (const hp<LIM,MAX> &A)
{
for (int i=0;i<A.scnt;i++)
sect[i]=A.sect[i];
scnt=A.scnt;
}
void (int A)
{
scnt=0;
while (A)
{
sect[scnt++]=A % LIM;
A /=LIM;
}
}
void print()
{
int i,k;
printf("%d",sect[scnt-1]);
for (i=scnt-2;i>=0;i--)
{
k=LIM/10;
while(sect[i]<k)
{
printf("0");
k/=10;
}
if (sect[i])
printf("%d",sect[i]);
}
}
void read(int LIE)
{
char A[LIM*MAX];
int len,i,j,k,b=0;
scanf("%s",A);
len=strlen(A);
k=len % LIE;
j=scnt=len / LIE;
if (k)
{
scnt++;
j=scnt-1;
sect[j]=0;
for (i=0;i<k;i++)
{
sect[j]*=10;
sect[j]+=A[b++]-'0';
}
}
for (j--;j>=0;j--)
{
sect[j]=0;
for (i=0;i<LIE;i++)
{
sect[j]*=10;
sect[j]+=A[b++]-'0';
}
}
}
void plus(hp<LIM,MAX> &A,int offset=0)
{
int sc=scnt > A.scnt + offset ? scnt : A.scnt + offset;
int i,j,up=0;
for (i=0;i<sc;i++)
{
j=i - offset;
if (j<0) continue;
if (i>=scnt) sect[i]=0;
if (j>=A.scnt) A.sect[j]=0;
sect[i]+=A.sect[j] + up;
up=sect[i] / LIM;
sect[i]%=LIM;
}
scnt=sc;
if (up) sect[scnt++]=up;
}
void minus(hp<LIM,MAX> &A)
{
int sc=scnt;
int i,lend=0;
for (i=0;i<sc;i++)
{
if (i>=A.scnt) A.sect[i]=0;
sect[i]-=A.sect[i] + lend;
lend=0;
if (sect[i]<0)
{
lend=1;
sect[i]+=LIM;
}
}
scnt=sc;
if (lend) scnt--;
for (;scnt>1 && sect[scnt-1]==0;scnt--);

}
void multiply(int p)
{
if (p==0)
{
scnt=1;
sect[0]=0;
return;
}
int sc=scnt;
int i,up=0;
for (i=0;i<sc;i++)
{
if (i>=scnt) sect[i]=0;
sect[i]=sect[i] * p + up;
up=sect[i] / LIM;
sect[i]%=LIM;
}
scnt=sc;
if (up) sect[scnt++]=up;
}
void multiply(hp<LIM,MAX> &A)
{
hp<LIM,MAX> T,C;
int sc=A.scnt;
int i;
for (i=0;i<sc;i++)
{
T=(*this);
T.multiply(A.sect[i]);
C.plus(T,i);
}
scnt=sc;
(*this)=C;
}
int compare(hp<LIM,MAX> &A)
{
if (scnt > A.scnt)
return 1;
if (scnt < A.scnt)
return -1;
for (int i=scnt-1;i>=0;i--)
{
if (sect[i] > A.sect[i])
return 1;
if (sect[i] < A.sect[i])
return -1;
}
return 0;
}
//operators
bool operator == (hp<LIM,MAX> &A)
{
int t=compare(A);
return t==0;
}
bool operator < (hp<LIM,MAX> &A)
{
int t=compare(A);
return t==-1;
}
bool operator > (hp<LIM,MAX> &A)
{
int t=compare(A);
return t==1;
}
bool operator <= (hp<LIM,MAX> &A)
{
int t=compare(A);
return t==-1 || t==0;
}
bool operator >= (hp<LIM,MAX> &A)
{
int t=compare(A);
return t==1 || t==0;
}
void operator =(hp<LIM,MAX> A)
{
(A);
}
void operator =(int A)
{
(A);
}
void operator +=(hp<LIM,MAX> &A)
{
plus(A);
}
void operator -=(hp<LIM,MAX> &A)
{
minus(A);
}
void operator *=(hp<LIM,MAX> &A)
{
multiply(A);
}
void operator *=(int A)
{
multiply(A);
}
hp<LIM,MAX> operator +(hp<LIM,MAX> &A)
{
hp<LIM,MAX> C(*this);
C.plus(A);
return C;
}
hp<LIM,MAX> operator -(hp<LIM,MAX> &A)
{
hp<LIM,MAX> C(*this);
C.minus(A);
return C;
}
hp<LIM,MAX> operator *(int A)
{
hp<LIM,MAX> C(*this);
C.multiply(A);
return C;
}
hp<LIM,MAX> operator *(hp<LIM,MAX> &A)
{
hp<LIM,MAX> C(*this);
C.multiply(A);
return C;
}
};

typedef hp<10000,100> hpnum;

hpnum A,B;

hpnum qp(int P)
{
hpnum A;
if (P/2==1)
A=2;
else
A=qp(P/2);
A*=A;
if (P%2==1)
A*=2;
return A;
}

void mason()
{
int P;
B=1;
freopen("mason.in","r",stdin);
freopen("mason.out","w",stdout);
scanf("%d",&P);
A=qp(P);
A-=B;
A.print();
}

void mul()
{
char c;
hpnum A,B,C;
freopen("mul.in","r",stdin);
freopen("mul.out","w",stdout);
A.read(4);
while ((c=getchar())==10 || c==13);
ungetc(c,stdin);
B.read(4);

C=A*B;
C+=B;
C.print();
}

int main()
{
mason();
return 0;
}

F. 关于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();//获取大整数
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;
}
}
}

G. 两个超长正整数相加(C语言)

既然楼主要求用C语言,那就用经典的C指针吧

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

char*BigIntAdd(char*x,char*y,char*z)

{

intlenx=strlen(x);

intleny=strlen(y);

char*pmax=x,*pmin=y,*pz=z,*p1,*p2,t;

inti,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;

}

returnz;

}

voidmain()

{

system("color1e");

charx[1024*10]={0},y[1024]={0},z[1024]={0};

printf("EnterLargeIntegers1: ");

scanf("%s",x);

printf("EnterLargeIntegers2: ");

scanf("%s",y);

BigIntAdd(x,y,z);

printf(" BigIntAdd: %s + %s = %s ",x,y,z);

system("pause");

}

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

H. C语言 超长整数加法

模拟!就是实现你做加法的过程,一位一位算,注意进位就行了