数值计算之高精度加减乘除
一. 高精度正整数的高精度计算
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.除法:用高精度减法实现
二. 注意清零和对位操作
三. 代码
//
// main.cpp
// 正整数高精度运算
//
// Created by ashley on 14-11-9.
// Copyright (c) 2014年 ashley. All rights reserved.
// #include <iostream>
#include <string>
using namespace std; string clearZeros(string data)
{
if (data[] == '') {
int key = (int) data.length() - ;
for (int i = ; i < data.length(); i++) {
if (data[i] != '') {
key = i;
break;
}
}
data.erase(, key);
}
if (data == "") {
data = "";
}
return data;
} //对位操作
void countPoint(string &operand1, string &operand2)
{
while (operand1.length() < operand2.length()) {
operand1 = "" + operand1;
}
while (operand1.length() > operand2.length()) {
operand2 = "" + operand2;
}
} //判断大小
bool bigger(string operand1, string operand2)
{
return operand1 >= operand2;
} string addition(string addent, string adder)
{
//先对位,在加数和被加数前面适当补0,使他们包含相同的位数
countPoint(addent, adder);
//前面再补一个0,确定和的最多位数
addent = "" + addent;
adder = "" + adder;
//从低位开始,对应位相加,结果写进被加数中,如果有进位,直接给被加数前一位加1
for (int i = (int) addent.length() - ; i > ; i--) {
addent[i] = addent[i] + adder[i] - ;
if (addent[i] > '') {
addent[i] = addent[i] - ;
addent[i - ] = addent[i - ] + ;
}
}
return clearZeros(addent);
} string subtraction(string subtrahend, string subtractor)
{
//先对位,在减数和被减数前面适当补0,使他们包含相同的位数
countPoint(subtrahend, subtractor);
//判断被减数和减数谁大,保证被减数大于减数
if (bigger(subtrahend, subtractor)) {
subtrahend[] = subtrahend[] - subtractor[] + ;
for (int i = ; i < (int)subtrahend.length(); i++) {
if (subtrahend[i] >= subtractor[i]) {
subtrahend[i] = subtrahend[i] - subtractor[i] + ;
} else {
subtrahend[i] = subtrahend[i] - subtractor[i] + + ;
subtrahend[i - ]--;
}
}
} else {
subtrahend = '-' + subtraction(subtractor, subtrahend);
}
return subtrahend;
} string multiplication(string multiplicand, string multiplier)
{
string result = "";
for (int i = (int)multiplier.length() - ; i >= ; i--) {
for (char c = ''; c <= multiplier[i]; c++) {
result = addition(result, multiplicand);
}
multiplicand = multiplicand + "";
}
return clearZeros(result);
} // 试商法
string division(string dividend, string divisor)
{
// 存放商
string result;
// 存放余数
string remains;
for (int i = ; i < (int)dividend.length(); i++) {
remains = remains + dividend[i];
result = result + "";
// 从1往上试
while (bigger(remains, result)) {
cout << result << "-----------" << remains << endl;
result[result.length() - ]++;
remains = subtraction(remains, divisor);
}
}
return clearZeros(result);
}
int main(int argc, const char * argv[])
{
string a, b;
int tests;
cin >> tests;
while (tests--) {
cin >> a >> b;
//正整数高精度加法,从低位开始
//cout << addition(a, b) << endl;
//正整数高精度减法,从高位开始
//cout << subtraction(a, b) << endl;
//正整数高精度乘法,将乘法转换为加法进行运算
//cout << multiplication(a, b) << endl;
cout << division(a, b) << endl;
//正整数高精度除法 }
return ;
}