用链表实现两大数相乘



#include<iostream> #include<string> #include<list> using namespace std; //大整数相加 list<char> long_add(list<char> num1,list<char>num2); //大整数相减 list<char> long_sub(list<char>num1,list<char>num2); //大整数相乘 list<char> long_mutliply(list<char>num1,list<char>num2); //输出结果 void print(list<char> rs); int main(void) { string str1,str2; cout<<"Please input the value of a&& b"<<endl; cout<<" a> "; cin>>str1; cout<<" b> "; cin>>str2; cout<<"input the operation"<<endl; cout<<"operation: >"; char ch; cin>>ch; //存放操作数和结果 list<char>num1,num2; list<char> rs; for(int i=0;i<str1.size();i++) { num1.push_back(str1[i]); } for( i=0;i<str2.size();i++) { num2.push_back(str2[i]); } switch(ch){ //test the add function case '+': rs=long_add(num1,num2); break; case '-': rs=long_sub(num1,num2); break; case'*': rs=long_mutliply(num1,num2); break; } print(rs); getchar(); getchar(); return 0; } //the definition of long_add list<char> long_add(list<char> num1,list<char>num2) { list<char> rs; //两个数符号不同 if((num1.front()=='-')&&(num2.front()!='-')) { num1.erase(num1.begin()); rs=long_sub(num1,num2); return rs; } if((num1.front())!='-'&&(num2.front()=='-')) { num2.erase(num2.begin()); rs=long_sub(num2,num1); return rs; } //两个数均为负数 if((num1.front()=='-')&&(num2.front()=='-')) { num1.erase(num1.begin()); num2.erase(num2.begin()); rs=long_add(num1,num2); rs.push_front('-'); return rs; } //两个数均为正数 if((num1.front())!='-'&&(num2.front())!='-') { int size1=num1.size(); int size2=num2.size(); //补全位数并确定size值 if(size1<size2) { for(int i=0;i<(size2-size1);i++) num1.push_front('0'); } if(size2<size1) { for(int i=0;i<(size1-size2);i++) num2.push_front('0'); } int c_high=0;//表示进位 list<char>::iterator iter1=num1.end(); iter1--; list<char>::iterator iter2=num2.end(); iter2--; while(iter1!=num1.begin()&&iter2!=num2.begin()) { int m=(*iter1)-'0'; int n=(*iter2)-'0'; int temp=m+n+c_high; rs.push_front(temp%10+'0'); c_high=temp/10; iter1--; iter2--; } //最高位的计算 int m=(*iter1)-'0'; int n=(*iter2)-'0'; int temp=m+n+c_high; rs.push_front(temp%10+'0'); c_high=temp/10; //判断最高位是否有进位 if(c_high!=0) { rs.push_front(c_high+'0'); } return rs; } } //the definition of long_sub list<char> long_sub(list<char> num1,list<char> num2) { list<char> rs;//存储结果 //两个数符号相异 if((num1.front())=='-'&&(num2.front())!='-') { num1.erase(num1.begin()); rs=long_add(num1,num2); return rs; } if((num1.front())!='-'&&(num2.front())=='-') { num2.erase(num2.begin()); rs=long_add(num1,num2); rs.push_front('-'); return rs; } //两个数均为负数 if((num1.front())=='-'&&(num2.front())=='-') { num1.erase(num1.begin()); num2.erase(num2.begin()); rs=long_sub(num2,num1); return rs; } //两个数均为正数 if((num1.front())!='-'&&(num2.front())!='-') { int size1=num1.size(); int size2=num2.size(); int flag=0;//表示结果的符号 flag=0 为正数,flag=1 为负数 if(size1<size2) { flag=0; for(int i=0;i<(size2-size1);i++) num1.push_front('0'); } if(size2<size1) { flag=1; for(int i=0;i<(size1-size2);i++) num2.push_front('0'); } if(size1==size2) { list<char>::iterator iter1=num1.begin(); list<char>::iterator iter2=num2.begin(); while((*iter1)==(*iter2)&&iter1!=num1.end()) { iter1++; iter2++; } if(iter1==num1.end()) { rs.push_front('0'); return rs; } else if((*iter1)>(*iter2)) flag=1; else flag=0; } //结果为负 if(flag==1) { rs=long_sub(num2,num1); rs.push_front('-'); return rs; } if(flag==0) { list<char>::iterator iter1=num1.end(); iter1--; list<char>::iterator iter2=num2.end(); iter2--; int c_high=0;//表示借位 c_high=0不借位 c_high=1 借位 while(iter1!=num1.begin()&&iter2!=num2.begin()) { int m=(*iter1)-'0'; int n=(*iter2)-'0'; int temp=n-(m+c_high); if(temp<0) { c_high=1; temp=temp+10; rs.push_front(temp+'0'); } else { rs.push_front(temp+'0'); c_high=0; } iter1--; iter2--; } //处理最高位的减法,同时根据判断,借位为0 int m=(*iter1)-'0'; int n=(*iter2)-'0'; int temp=n-(m+c_high); rs.push_front(temp+'0'); return rs; } } } list<char> long_mutliply(list<char> num1,list<char>num2) { list<char> rs;//存储结果 //如果两数符号相异,结果为负 if(num1.front()=='-'&&num2.front()!='-') { num1.erase(num1.begin()); rs=long_mutliply(num1,num2); rs.push_front('-'); return rs; } if(num2.front()=='-'&&num1.front()!='-') { num2.erase(num2.begin()); rs=long_mutliply(num1,num2); rs.push_front('-'); return rs; } //两数均为负数 if(num1.front()=='-'&&num2.front()=='-') { num1.erase(num1.begin()); num2.erase(num2.begin()); rs=long_mutliply(num1,num2); return rs; } //两数均为正数 if(num1.front()!='-'&&num2.front()!='-') { int size1=num1.size(); int size2=num2.size(); //只剩下一位 if(size1==1&&size2==1) { int m=(num1.front())-'0'; int n=(num2.front())-'0'; int temp=m*n; int high=temp/10; int low=temp%10; if(high!=0) rs.push_back(high+'0'); rs.push_back(low+'0'); return rs; } if(size1==1&&size2!=1) { list<char> num2_high;//num2的高位部分 list<char> num2_low;//num2的低位部分 list<char>::iterator iter2; int i; for(i=0,iter2=num2.begin();i<size2/2;i++,iter2++) { num2_high.push_back(*iter2); } for(i=size2/2;i<size2;i++,iter2++) { num2_low.push_back(*iter2); } list<char> rs_high;//高位的积 list<char> rs_low;//低位的积 rs_high=long_mutliply(num1,num2_high); rs_low=long_mutliply(num1,num2_low); //高位移位 for( i=0;i<num2_low.size();i++) rs_high.push_back('0'); rs=long_add(rs_high,rs_low); return rs; } if(size1!=1&&size2==1) return long_mutliply(num2,num1);//根据乘法交换律 //******************************************************************************************** //******************************************************************************************** //******************************************************************************************** if(size1!=1&&size2!=1) { list<char> num1_high; list<char> num1_low; list<char> num2_high; list<char> num2_low; list<char>::iterator iter1; list<char>::iterator iter2; int i; //分别找出num1和num2的低位和高位 for(i=0,iter1=num1.begin();i<size1/2;i++,iter1++) { num1_high.push_back(*iter1); } for(;iter1!=num1.end();iter1++) { num1_low.push_back(*iter1); } for(i=0,iter2=num2.begin();i<size2/2;i++,iter2++) { num2_high.push_back(*iter2); } for(;iter2!=num2.end();iter2++) { num2_low.push_back(*iter2); } list<char> rs_high;//存储高位乘积 list<char> rs_low;//存储低位乘积 list<char> rs_high_low; list<char> rs_low_high; rs_high=long_mutliply(num1_high,num2_high); rs_low=long_mutliply(num1_low,num2_low); //num1和num2的高位进行移位 for( i=0;i<num1_low.size();i++) { num1_high.push_back('0'); } for( i=0;i<num2_low.size();i++) { num2_high.push_back('0'); } rs_high_low=long_mutliply(num1_high,num2_low); rs_low_high=long_mutliply(num1_low,num2_high); //高位乘积移位 for( i=0;i<num1_low.size()+num2_low.size();i++) rs_high.push_back('0'); //把四项结果进行相加 rs=long_add(rs_high,rs_low); rs=long_add(rs,rs_high_low); rs=long_add(rs,rs_low_high); return rs; } return rs; } return rs; } void print(list<char> rs) { list<char>::iterator iter=rs.begin(); cout<<(*iter); iter++; //如果结果是0 ,则输出0值 while((*iter)=='0'&&iter!=rs.end()) { iter++; } if(iter==rs.end()) cout<<0; else { while(iter!=rs.end()) { cout<<(*iter); iter++; } } }

/***********描述:实现两个大整数的加、减、乘的运算,其中乘法运算
*********************利用分治的方法解决:假设X*Y可以分解为
********************* (X1*(10^n/2)+X2)*(Y1*(10^n/2)+Y2)=******************
********************X1*Y1*10^n+((X1-X2)(Y2-Y1)+X1*Y1+X2*Y2)*10^(n/2)+X2*Y2
********************此算法降低了乘法的次数***********************/

posted @ 2013-05-21 20:53  任智康  阅读(2243)  评论(0编辑  收藏  举报