數值計算之高精度加減乘除
一. 高精度正整數的高精度計算
1.加法
2.減法
減法和加法的最大區別在於:減法是從高位開始相減,而加法是從低位開始相加
3.乘法:用高精度加法實現
l 乘法的主要思想是把乘法轉化為加法進行運算。請先看下面的等式:
12345*4=12345+12345+12345+12345
12345*20=123450*2
12345*24=12345*20+12345*4
l 等式(1)說明,多位數乘一位數,可以直接使用加法完成。
l 等式(2)說明,多位數乘形如d*10n的數,可以轉換成多位數乘一位數來處理。
l 等式(3)說明,多位數乘多位數,可以轉換為若干個“多位數乘形如d*10n的數與多位數乘一位數”之和。
l 因此,多位數乘多位數最終可以全部用加法來實現。
4.除法:用高精度減法實現
二. 注意清零和對位操作
三. 代碼
1 // 2 // main.cpp 3 // 正整數高精度運算 4 // 5 // Created by ashley on 14-11-9. 6 // Copyright (c) 2014年 ashley. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <string> 11 using namespace std; 12 13 string clearZeros(string data) 14 { 15 if (data[0] == '0') { 16 int key = (int) data.length() - 1; 17 for (int i = 0; i < data.length(); i++) { 18 if (data[i] != '0') { 19 key = i; 20 break; 21 } 22 } 23 data.erase(0, key); 24 } 25 if (data == "") { 26 data = "0"; 27 } 28 return data; 29 } 30 31 //對位操作 32 void countPoint(string &operand1, string &operand2) 33 { 34 while (operand1.length() < operand2.length()) { 35 operand1 = "0" + operand1; 36 } 37 while (operand1.length() > operand2.length()) { 38 operand2 = "0" + operand2; 39 } 40 } 41 42 //判斷大小 43 bool bigger(string operand1, string operand2) 44 { 45 return operand1 >= operand2; 46 } 47 48 string addition(string addent, string adder) 49 { 50 //先對位,在加數和被加數前面適當補0,使他們包含相同的位數 51 countPoint(addent, adder); 52 //前面再補一個0,確定和的最多位數 53 addent = "0" + addent; 54 adder = "0" + adder; 55 //從低位開始,對應位相加,結果寫進被加數中,如果有進位,直接給被加數前一位加1 56 for (int i = (int) addent.length() - 1; i > 0; i--) { 57 addent[i] = addent[i] + adder[i] - 48; 58 if (addent[i] > '9') { 59 addent[i] = addent[i] - 10; 60 addent[i - 1] = addent[i - 1] + 1; 61 } 62 } 63 return clearZeros(addent); 64 } 65 66 string subtraction(string subtrahend, string subtractor) 67 { 68 //先對位,在減數和被減數前面適當補0,使他們包含相同的位數 69 countPoint(subtrahend, subtractor); 70 //判斷被減數和減數誰大,保證被減數大於減數 71 if (bigger(subtrahend, subtractor)) { 72 subtrahend[0] = subtrahend[0] - subtractor[0] + 48; 73 for (int i = 1; i < (int)subtrahend.length(); i++) { 74 if (subtrahend[i] >= subtractor[i]) { 75 subtrahend[i] = subtrahend[i] - subtractor[i] + 48; 76 } else { 77 subtrahend[i] = subtrahend[i] - subtractor[i] + 10 + 48; 78 subtrahend[i - 1]--; 79 } 80 } 81 } else { 82 subtrahend = '-' + subtraction(subtractor, subtrahend); 83 } 84 return subtrahend; 85 } 86 87 string multiplication(string multiplicand, string multiplier) 88 { 89 string result = "0"; 90 for (int i = (int)multiplier.length() - 1; i >= 0 ; i--) { 91 for (char c = '1'; c <= multiplier[i]; c++) { 92 result = addition(result, multiplicand); 93 } 94 multiplicand = multiplicand + "0"; 95 } 96 return clearZeros(result); 97 } 98 99 // 試商法 100 string division(string dividend, string divisor) 101 { 102 // 存放商 103 string result; 104 // 存放余數 105 string remains; 106 for (int i = 0; i < (int)dividend.length(); i++) { 107 remains = remains + dividend[i]; 108 result = result + "0"; 109 // 從1往上試 110 while (bigger(remains, result)) { 111 cout << result << "-----------" << remains << endl; 112 result[result.length() - 1]++; 113 remains = subtraction(remains, divisor); 114 } 115 } 116 return clearZeros(result); 117 } 118 int main(int argc, const char * argv[]) 119 { 120 string a, b; 121 int tests; 122 cin >> tests; 123 while (tests--) { 124 cin >> a >> b; 125 //正整數高精度加法,從低位開始 126 //cout << addition(a, b) << endl; 127 //正整數高精度減法,從高位開始 128 //cout << subtraction(a, b) << endl; 129 //正整數高精度乘法,將乘法轉換為加法進行運算 130 //cout << multiplication(a, b) << endl; 131 cout << division(a, b) << endl; 132 //正整數高精度除法 133 134 } 135 return 0; 136 }