标签:c++
也只是刚刚写出了加法,用的是1 000 000 000进制,不过发现对于加法还不如用最基本的方法方便。用1 000 000 000进制的方便也就在于处理乘法和除法了。计算机做乘除运算还是比较耗时的。
#include <random>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
using namespace std;
const int MaxNum=1000000000; //进制
class BigNumCalcu{
public:
const static int N=9;
BigNumCalcu():num_1(""),result(""),numSize_1(0),numSize_2(0){ //init
num_2="";
fopen.open("num.txt");
}
string patchZero(int M); //补0
void getInput(); //读取数据
void SplitNum();
void Sum(); //加法
string Sub(); //减法
void Multi(); //乘法
string Div(); //除法
void randomData();
private:
fstream fopen;
vector<int> BitNum_1,BitNum_2;
string num_1,num_2,result;
unsigned int numSize_1;
unsigned int numSize_2;
};
void BigNumCalcu::getInput(){
for(int i=0;i!=5;++i){
result="";
BitNum_1.clear();
BitNum_2.clear();
fopen>>num_1>>num_2;
cout<<"num_1: "<<num_1<<" num_2: "<<num_2<<endl;
SplitNum();
Sum();
}
}
void BigNumCalcu::SplitNum(){
numSize_1=(num_1.size()%9==0) ? num_1.size()/9 : num_1.size()/9+1;
numSize_2=(num_2.size()%9==0) ? num_2.size()/9 : num_2.size()/9+1;
//split
if(numSize_1==num_1.size()/9)
for(int index=1;index<=numSize_1;++index)
BitNum_1.push_back(std::stoi(num_1.substr(num_1.size()-index*BigNumCalcu::N,9)));
else
for(int index=1;index<=numSize_1;++index){
if(index==numSize_1)
BitNum_1.push_back(std::stoi(num_1.substr(0,num_1.size()-(index-1)*BigNumCalcu::N)));
else
BitNum_1.push_back(std::stoi(num_1.substr(num_1.size()-index*BigNumCalcu::N,9)));
}
if(numSize_2==num_2.size()/9)
for(int index=1;index<=numSize_2;++index)
BitNum_2.push_back(std::stoi(num_2.substr(num_2.size()-index*BigNumCalcu::N,9)));
else
for(int index=1;index<=numSize_2;++index){
if(index==numSize_2)
BitNum_2.push_back(std::stoi(num_2.substr(0,num_2.size()-(index-1)*BigNumCalcu::N)));
else
BitNum_2.push_back(std::stoi(num_2.substr(num_2.size()-index*BigNumCalcu::N,9)));
}
}
string BigNumCalcu::patchZero(int M){
auto it=std::to_string(M).size();
string temp=std::to_string(M);
if(it<BigNumCalcu::N){
for(;it<BigNumCalcu::N;it++)
temp="0"+temp;
return result=temp+result;
}
else
return result=std::to_string(M);
}
void BigNumCalcu::Sum(){
int min=std::min(numSize_1,numSize_2);
int index=0; //进位
int sum=0;
//如果兩個數字具有相同的位数
//如果两个数字位数不一样
if(min==numSize_1)
for(int count=min;count<numSize_2;++count)
if((sum=BitNum_2[count]+index)>=MaxNum){
index=1;
result=patchZero(sum-MaxNum);
}
else{
index=0;
result=patchZero(sum);
}
else
for(int count=min;count<numSize_1;++count)
if((sum=BitNum_1[count]+index)>=MaxNum){
index=1;
result=patchZero(sum-MaxNum);
}
else{
index=0;
result=patchZero(sum);
}
//如果最高位也需要进位
if(index==1)
result="1"+result;
cout<<result<<endl;
}
int main(int argc,char **argv){
BigNumCalcu Calc;
Calc.getInput();
return 0;
}
下面是一个python测试。因为python已经自带大整数计算了
__author__=‘MarkLiang‘
#coding=utf-8
fopen=open(‘num.txt‘,‘r‘)
for num1 in fopen:
num2=fopen.readline();
print("result: ",int(num1)+int(num2))
fopen.close()
至于乘法和除法,我有空在写吧。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:c++
原文地址:http://blog.csdn.net/u013220338/article/details/48107081