當前位置:首頁 » 編程語言 » 超長整數加法c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

超長整數加法c語言

發布時間: 2023-01-08 10:41:38

❶ 在c語言環境下實現長整數的加減法運算

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

#define MAX 100 /*位數*/
typedef struct {
int a[MAX];
int b[MAX];
int s[MAX+1];
} high;

void init(high *x,char *a,char *b)
{
int i,len;
len=strlen(a);
for (i = 0; i<MAX; i++) {
x->a[i]=x->b[i]=x->s[i]=0;
}
x->s[MAX]=0;
for (i=0; i<len; ++i) {
x->a[MAX+i-len]=a[i]-48;
}
len=strlen(b);
for (i=0; i<len; ++i) {
x->b[MAX+i-len]=b[i]-48;
}
}
int plus(high *x)
{
int i;
for (i=MAX-1; x->a[i]||x->b[i]; --i) {
x->s[i+1]+=(x->a[i]+x->b[i]);
x->s[i]+=x->s[i+1]/10;
x->s[i+1]%=10;

}
return (MAX-i);
}
void prt(high *a,int len)
{
int i;
for (i=MAX-len+1; i<=MAX; i++) printf("%d",a->s[i]);

}
int main(void)
{
char a[MAX+1],b[MAX+1];
int len;
high sp;
scanf("%[0123456789]",a);
fflush(stdin);
scanf("%[1234567890]",b);
init(&sp,a,b);
len=plus(&sp);
prt(&sp,len);
return 0;
}

❷ C語言 超長整數加法

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

❸ 用c語言實現超長整數的加法運算

#include "stdio.h"
int sum(int a[],int b[],int c[])
{
int i=0,j=0,f=0;
for(;i<21;i++)
{
j=a[i]+b[i]+j;
if(j!=0) f=i;
c[i]=j%10000;
j=j/10000;
}
c[i]=j;
return f;
}
void get(int a[])
{
int n,i,j;
scanf("%d",&n);
j=(n-1)/4;
switch (n%4)
{
case 3:scanf("%3d",&a[j]);break;
case 2:scanf("%2d",&a[j]);break;
case 1:scanf("%1d",&a[j]);break;
case 0:scanf("%4d",&a[j]);break;
}

for(i=j-1;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]);
}

❹ C語言中怎麼實現兩個超大整數的相加減乘除

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#define N 100

int main(int argc, char const *argv[])

{

char arr[N] = {};

gets(arr);

char brr[N] = {};

gets(brr);

int len1,len2,i = 0,j = 0;

len1 = strlen(arr);

len2 = strlen(brr);

int len = len1>len2?len1:len2;

/* c99之後數組初始化支持整型表達式,稱為可變長數組,但按照c89的標準是不對的

int num1[len]; //將字元串轉換成翻轉的整型數組

int num2[len];

*/

int* num1 = (int*)malloc(len*sizeof(int));

int* num2 = (int*)malloc(len*sizeof(int));

for (i = 0; i < len; i++)

{

num1[i] = i<len1 ? arr[len1-i-1]-'0':0;

}

for (j = 0; j < len; j++)

{

num2[j] = j<len2 ? brr[len2-j-1]-'0':0;

}

//int sum[len]; //定義和數組

int* sum = (int*)malloc(len*sizeof(int));

int flag=0; //設進位符

for (i = 0; i < len; i++)

{

sum[len-1-i] = (num1[i]+num2[i]+flag)%10;

flag = (num1[i]+num2[i]+flag)/10;

}

if (flag == 1) printf("1"); //如果最高位有進位 則輸出一個1

for (i = 0; i < len; i++)

{

printf("%d",sum[i]);

}

printf(" ");

free(num1);

free(num2);

free(sum);

num1 = NULL;

num2 = NULL;

sum = NULL;

return 0;

}

(4)超長整數加法c語言擴展閱讀:

gets()函數用法

gets是從標准輸入設備讀字元串函數。

函數原型:char*gets(char*str);

功能為:從stdin流中讀取字元串,直至接受到換行符或EOF時停止,並將讀取的結果存放在buffer指針所指向的字元數組中。換行符不作為讀取串的內容,讀取的換行符被轉換為『\0』空字元,並由此來結束字元串。

注意:不會判斷上限,以回車結束讀取,所以程序員應該確保buffer的空間足夠大,以便在執行讀操作時不發生溢出。使用時需要包含stdio.h頭文件

參數

str為字元串指針,用來存放讀取到的數據。

返回值

讀入成功,返回與參數buffer相同的指針;讀入過程中遇到EOF(End-of-File)或發生錯誤,返回NULL指針。所以在遇到返回值為NULL的情況,要用ferror或feof函數檢查是發生錯誤還是遇到EOF。

❺ 關於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;
}
}
}

❻ 兩個超長正整數相加(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");

}

主要想法是,判斷兩個數字的長短,找出最短的數字,從個位數開始與另一個數的相應位進行相加(注意判斷向上進位),將結果逐個保存在結果字元串中。最後將長的那個數字剩下的部分直接放在結果字元串中,然後將結果字元串反轉,得到結果

❼ 超大整數的加法--C語言

這題要用高精度加法來做

參考:http://www.oschina.net/code/snippet_564999_11104

隨手寫了一下,沒有測試大量數據,不保證完全正確

#include<stdio.h>
#include<string.h>
#defineMAXSIZE100
voidadd(char*num1,char*num2,char*ans)
{
inti,j;
//逆向賦值
//例如num1="123456"
//則a="654321000000...0"(後面用0填充)
//b同理
chara[MAXSIZE+2],b[MAXSIZE+2],c[MAXSIZE+2];
for(i=0;i<MAXSIZE+1;i++)
{
a[i]=b[i]=c[i]='0';
}
a[MAXSIZE+1]=b[MAXSIZE+1]=c[MAXSIZE+1]='';

for(i=0;i<strlen(num1);i++)
{
a[i]=num1[strlen(num1)-1-i];
}
for(i=0;i<strlen(num2);i++)
{
b[i]=num2[strlen(num2)-1-i];
}
for(i=0;i<MAXSIZE+1;i++)
{
chariAns=c[i]-'0';//ascii轉為數字
chariA=a[i]-'0';
chariB=b[i]-'0';
iAns+=iA+iB;
chariJW=iAns/10;//處理進位
iAns%=10;
c[i]=iAns+'0';
c[i+1]=iJW+'0';
}
//除去前端的0
//將結果逆向賦值給ans
//返回ans
for(i=MAXSIZE;(i>=0)&&(c[i]=='0');i--);
if(i==-1)strcpy(ans,"0");
else
{
for(j=0;j<=i;j++)
{
ans[j]=c[i-j];
}
ans[j]='';
}
}
intmain(intargc,char*argv[])
{
chara[MAXSIZE+2],b[MAXSIZE+2],ans[MAXSIZE+2];
while(1)
{
scanf("%s%s",a,b);
if((strcmp(a,"0")==0)&&(strcmp(b,"0")==0))break;
add(a,b,ans);
printf("%s ",ans);
}
return0;
}

❽ 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;
}

❾ 用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語言 超長整數加法 考慮正負號

使用字元數組模擬加法運算