C 高精度加 減 乘 除運算詳解

2021-09-25 11:02:45 字數 2819 閱讀 7820

一、高精度加法

#include

using namespace std;

intmain()

len++

;//下面是去掉最高位的0,然後輸出。

while

((a[len]==0

)&&(len>1)

) len--

;for

(i=len;i>=

1;i--

) cout<;return0;

}//注意:兩個數相加,結果的位數,應該比兩個數中大的那個數多一位。

二、高精度減法

#include 

#include

#include

using namespace std;

int a[

200000

],b[

200000];

//自定義函式,比較兩個字串數字大小,大於等於返回true,小於返回false

bool compare

(string str1,string str2)

}return

true

;//長度相等且每一位的數也相等,返回false

}int

main()

} a[0]

++;while

((a[a[0]

]==0)

&&(a[0]

>1)

)//清除前導零

a[0]

--;for(

int i=a[0]

;i>=

1;i--

)//前面反向存入陣列,現在反向輸出

printf

("%d"

,a[i]);

printf

("\n");

}else

} b[0]

++;while

((b[b[0]

]==0)

&&(b[0]

>1)

)//清除前導零

b[0]

--;for(

int i=b[0]

;i>=

1;i--

)//反向輸出

printf

("%d"

,b[i]);

printf

("\n");

}return0;

}

三、高精度乘法

#include

#include

#include

using namespace std;

intmain()

}int len=a[0]

+b[0]+

1;while

((c[len]==0

)&&(len>1)

)//清除前導零

len--

;for

(int i=len;i>=

1;i--

)printf

("%d"

,c[i]);

return0;

}

四、高精度除法:

第一種情況:高精除以低精,實際上就是對被除的每一位,包括前面的餘數都除以除數。

#include .h>

using namespace std;

intmain()

lenc=1;

while

(c[lenc]==0

&&lenclenc++

;//刪除前導0

for(i=lenc;i<=lena;i++

) cout<; cout

}

第二種情況:高精除以高精

#include .h>

using namespace std;

int a[

100]

,b[100

],c[

100]

;int

compare

(int a,

int b)

//比較a、b,若a>b為1;若a

return0;

}void

subduction

(int a,

int b)

//計算a=a-b

if(flag==1)

//大於

a[i]

-=b[i];}

while

(a[0

]>

0&&a[a[0]

]==0)

//刪除前導0

a[0]

--;return;}

}int

main()

}while

(c[0

]>

0&&c[c[0]

]==0)

//刪除前導0

c[0]

--;cout<<

"商為:";if

(c[0]==

0)//輸出結果

cout<<

0

cout<<

"餘數為:";if

(a[0]==

0)//輸出餘數

cout<<

0

return0;

}

高精度加減乘除 C

高精度加法 使用陣列儲存,高位在高角標,低位在低角標,相加向高位進製 c a b,a 0,b 0 vectoradd vector a,vector b if t c.push back t return c 高精度減法 使用陣列儲存,高位在高角標,低位在低角標,相加向高位借位 c a b,滿足a ...

高精度加減乘除 C

高精度加法 使用陣列儲存,高位在高角標,低位在低角標,相加向高位進製 c a b,a 0,b 0 vectoradd vector a,vector b if t c.push back t return c 高精度減法 使用陣列儲存,高位在高角標,低位在低角標,相加向高位借位 c a b,滿足a ...

高精度加減乘除

一。高精度乘法 敲了好久。頭禿 str1,str2是存數字的字串,返回最後的位數,c儲存結果 int highmult char str1,char str2,int c b len2 1 int i,j memset a,0,sizeof a memset b,0,sizeof b memset ...