大数加法和大数减法
2015-10-14 00:04
399 查看
两个极大的数相加或相减。主要运用字符串进行计算。
#include<iostream> #include<string> using namespace std; void reverse(string& input){ int n=input.size(); int i=0,j=n-1; while(i<j){ swap(input[i++],input[j--]); } return; } string myplus(string a,string b){ int alen=a.size(),blen=b.size(); reverse(a); reverse(b); int up=0; int i=0,j=0; string res; while(i<alen&&j<blen){ int aa=a[i]-'0',bb=b[j]-'0'; res.push_back(char((aa+bb+up)%10+'0')); up=(aa+bb+up)/10; i++; j++; } while(i<alen){ int aa=a[i]-'0'; res.push_back(char((aa+up)%10+'0')); up=(aa+up)/10; i++; } while(j<blen){ int bb=b[j]-'0'; res.push_back(char((bb+up)%10+'0')); up=(bb+up)/10; j++; } if(up) res.push_back(char(up+'0')); reverse(res); int ii=0; while(ii<res.size()&&res[ii]=='0') ii++; return ii==res.size()?"0":res.substr(ii); } string mysub(string a,string b){ string res; if(a[0]=='-'){ string tmp=b; b=a; a=tmp; } int alen=a.size(),blen=b.size(); int flag=0; if(alen<blen-1){ reverse(a); reverse(b); string tmp=b; b=a+"-"; a=tmp.substr(0,tmp.size()-1); alen=a.size(); blen=b.size(); flag=1; }else if(alen==blen-1){ int i=0,j=1; int fflag=0; while(i<alen&&j<blen){ while(a[i]==b[j]) {i++;j++;} if(a[i]<b[j]){ fflag=1; break; }else{ break; } } if(fflag){ reverse(a); reverse(b); string tmp=b; b=a+"-"; a=tmp.substr(0,tmp.size()-1); alen=a.size(); blen=b.size(); flag=1; }else{ reverse(a); reverse(b); } } else{ reverse(a); reverse(b); } int i=0,j=0; int borrow=0; while(i<alen&&b[j]!='-'){ if(borrow==0){ int aa=a[i]-'0',bb=b[j]-'0'; if(aa>=bb){ res.push_back(char(aa-bb+'0')); }else{ borrow=1; res.push_back(char(aa+10-bb+'0')); } }else{ int aa=a[i]-'0',bb=b[j]-'0'; if(aa-1>= bb){ borrow=0; res.push_back(char(aa-1-bb+'0')); }else{ res.push_back(char(aa-1+10-bb+'0')); } } i++;j++; } while(i<alen){ if(borrow==0){ res.push_back(a[i]); }else{ int aa=a[i]-'0'; if(aa-1>=0){ borrow=0; res.push_back(char(aa-1+'0')); }else{ res.push_back(char(aa-1+10+'0')); } } i++; } if(flag==1){ reverse(res); int ii=0; while(ii<res.size()&&res[ii]=='0') ii++; return ii==res.size()?"0":"-"+res.substr(ii); } else{ reverse(res); int ii=0; while(ii<res.size()&&res[ii]=='0') ii++; return ii==res.size()?"0":res.substr(ii); } } int main(){ string input1,input2; cin>>input1>>input2; if(input1[0]!='-'&&input2[0]!='-') cout<<myplus(input1,input2); else if(input1[0]=='-'&&input2[0]=='-') cout<<'-'<<myplus(input1.substr(1),input2.substr(1)); else{ cout<<mysub(input1,input2); } }
相关文章推荐
- LeetCode – Combination Sum
- mysql rsync方式备份
- 图解django 中间件
- Java基于Scanner对象的简单输入计算功能示例
- iredmail下安装脚本分析(一)---get_all.sh 文件所在目录为PKGS
- exit(-1)或者return(-1)shell得到的退出码为什么是255
- Linux rpm 命令参数使用详解
- linux shell 字符串操作(长度,查找,替换)详解
- linux whatis与whatis database 使用及查询方法(man使用实例)
- linux dev 常见特殊设备介绍与应用(loop,null,zero,full,random)
- linux shell命令快捷获得系统帮助(一)[man-pages定义规范]
- linux shell实现随机数几种方法分享(date,random,uuid)
- linux shell 脚本实现tcp/upd协议通讯(重定向应用)
- linux shell数据重定向(输入重定向与输出重定向)详细分析
- linux shell 管道命令(pipe)使用及与shell重定向区别
- jsp简单连接SQL Server2000数据库的方法
- jsp实现将动态网页转换成静态页面的方法
- 正则表达式基本语法详解
- 积累比较常用的正则表达式(例如:匹配中文、匹配html)
- 正则表达式(regex)入门、元字符(特殊字符)学习与提高