當前位置:首頁 » 編程語言 » 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語言 超長整數加法

模擬!就是實現你做加法的過程,一位一位算,注意進位就行了