大整数加法和乘法(stl)
程序代码:
#include <iostream> #include <string> #include <deque> #include <functional> #include <algorithm> using namespace std; class MyBigNum { deque<int>v; public: MyBigNum(){}; MyBigNum(string strNum) { copy(strNum.begin(),strNum.end(),back_inserter(v)); transform(v.begin(),v.end(),v.begin(),bind2nd(minus<int>(),'0')); } deque<int>::iterator begin() { return v.begin(); } deque<int>::iterator end() { return v.end(); } int size() { return v.size(); } back_insert_iterator<deque<int>>Back_Inserter()//这段有错误,不知道怎么改 { return back_inserter(v); } void push_front(int n) { v.push_front(n); } void push_back(int n) { v.push_back(n); } void adjust() { int nSize=v.size(); for(int i=nSize-1;i>=1;i--) { int value=v[i]; if(value<10) continue; v[i]=value%10; v[i-1]=value/10; } int value=v[0]; if(value>=10) { v[0]=value%10; value=value/10; while(value>0) { v.push_front(value%10); value/=10; } } nSize=v.size(); } MyBigNum Add(MyBigNum &m) { MyBigNum result; int n=size()-m.size(); if(n>=0) { transform(begin()+n,end(),m.begin(),result.Back_Inserter(),plus<int>()); for(int i=n-1;i>=0;i--) { result.push_front(*(begin()+i)); } } else { transform(begin(),end(),m.begin()-n,result.Back_Inserter(),plus<int>()); for(int i=-n-1;i>=0;i--) { result.push_font(*(m.begin()+i)); } } result.adjust(); return result; } MyBigNum Multiply(MyBigNum& m) { MyBigNum mid; for(int i=0;i<m.size();i++) { mid=*this; for(int j=0;j<i;j++) { mid.push_back(0); } transform(mid.begin(),mid.end(),mid.begin(),bind2nd(multiplies<int>(), *(m.begin()+i))); result=mid.Add(result); } return result; } }; void main() { MyBigNum m1("1234567890"); MyBigNum m2("99999999998"); MyBigNum result=m1.Add(m2); cout<<"1234567890+99999999998="; copy(result.begin(),result.end(),ostream_iterator<int>(cout)); cout<<endl; MyBigNum m3("99"); MyBigNum m4("99999"); MyBigNum m5=m3.Multiply(m4); cout<<"99*99999="; copy(m5.begin(),m5.end(),ostream_iterator<int>(cout)); cout<<endl; }错误已经指出,麻烦各位大侠指点一下