电脑上自带的计算器在计算结果数值较小的运算时较为精确,比如乘法,结果通常不能大于2的64次方,这里给出高精度乘法的源代码:
#include<iostream>#include<string>using namespace std;void JinWei(int *Result, int length);int main(){ string a, b; cout << "请输入两个数:" << endl; cin >> a >> b; int ResultLength = a.length() + b.length()-1; int *Result = new int[ResultLength]; memset(Result, 0, sizeof(int)*ResultLength); for (int i = a.length()-1; i >=0; i--) { for (int j = b.length()-1; j >=0; j--) { Result[i + j] +=int(a[i]-48) * int(b[j]-48); JinWei(Result, ResultLength); } } cout <<"结果="; for (int i = 0; i < ResultLength;i++) { cout << Result[i]; } return 0;}void JinWei(int *Result, int length){ for (int _t = length - 1; _t >0; _t--) { if (Result[_t]>9) { Result[_t-1] +=Result[_t]/10; Result[_t] %= 10; } }}新闻热点
疑难解答