最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實現(xiàn)高精度加法

 更新時間:2021年05月03日 11:39:08   作者:summer_awn  
這篇文章主要為大家詳細介紹了C語言實現(xiàn)高精度加法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本篇為高精度加法的計算,接下來我還會去寫高精度乘法的計算。

一、高精度運算的概念

高精度運算其實就是參與運算的數(shù)完全超出基本數(shù)據(jù)類型所能表示的范圍的運算(例如int型范圍就是 - 2147483648 ~+ 2147483647)
所以如果想求較大數(shù)的話就要創(chuàng)建新的數(shù)據(jù)類型

二、數(shù)據(jù)類型的選取

我選擇的是int型數(shù)組,這樣比較便于計算和理解

三、代碼

其中a和b我是通過隨機數(shù)來賦值

//author   summer_awn
//date    2017/6/20
 
#include<iostream>
#include<time.h>
 
#define lenth_a 200
#define lenth_b 200
 
using namespace std;
 
//計算a+b
void main() {
 
 srand((unsigned)time(NULL));
 
 int * a = new int[lenth_a];//數(shù)組a            ******
 
 for (int i = 0; i < lenth_a; ++i) {
 
  a[i] = rand() % 10;
 
 }
 
 cout << "a=";
 for (int i = lenth_a - 1; i >= 0; --i) {//輸出a
  cout << a[i];
 }
 cout << endl;
 cout << endl;
 
 int * b = new int[lenth_b];//數(shù)組b            ******
 
 for (int i = 0; i < lenth_a; ++i) {
 
  b[i] = rand() % 10;
 
 }
 
 cout << "b=";
 for (int i = lenth_b - 1; i >= 0; --i) {//輸出b
  cout << b[i];
 }
 cout << endl;
 cout << endl;
 
 
 
 int lenth_result;//結果的長度en
 
 if (lenth_a > lenth_b) lenth_result = lenth_a + 1; 
 else lenth_result = lenth_b + 1;//通過一個判斷來確定結果的長度
 
 
 int * a2 = new int[lenth_result];//a2***********
 
 int * b2 = new int[lenth_result];//b2***********
 
 memcpy(a2, a, sizeof(int)*lenth_a);//
 
 memset(a2 + lenth_a, 0, sizeof(int)*(lenth_result - lenth_a));
 
 memcpy(b2, b, sizeof(int)*lenth_b);
 
 memset(b2 + lenth_b, 0, sizeof(int)*(lenth_result - lenth_b));
 
 delete(a);
 delete(b);
 
 int * result = new int[lenth_result];//result*********
 
 result[0] = a2[0] + b2[0];
 
 for (int i = 1; i < lenth_result - 1; ++i) {
 
  result[i] = a2[i] + b2[i] + result[i - 1] / 10;
 
  result[i - 1] = result[i - 1] % 10;
 }
 
 result[lenth_result - 1] = result[lenth_result - 2] / 10;
 
 result[lenth_result - 2] = result[lenth_result - 2] % 10;
 
 delete(a2);
 delete(b2);
 
 cout << "結果=";
 for (int i = lenth_result - 1; i >= 0; --i) {
  cout << result[i];
 }
 cout << endl;
 
 system("pause");
 
 delete(result);
}

四、結果

結果有截圖,未驗證(因為懶)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

中阳县| 龙泉市| 札达县| 梅州市| 肇州县| 永顺县| 英吉沙县| 绵竹市| 汉寿县| 澳门| 铁岭市| 万源市| 浮山县| 义乌市| 平定县| 古交市| 沿河| 抚州市| 盐亭县| 曲麻莱县| 阳谷县| 呼和浩特市| 容城县| 博乐市| 安庆市| 从化市| 县级市| 南召县| 灌云县| 宜兰县| 左权县| 遂溪县| 绥芬河市| 亚东县| 浦江县| 兴隆县| 肥城市| 余庆县| 龙井市| 万州区| 开远市|