❶ 在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]='