高精度 加、减、乘、除 模板
来源:互联网 发布:java private的作用 编辑:程序博客网 时间:2024/04/29 17:27
#include<stdio.h>#include<string>#include<string.h>#include<iostream>using namespace std;//compare比较函数:相等返回0,大于返回1,小于返回-1int compare(string str1,string str2){ if(str1.length()>str2.length()) return 1; else if(str1.length()<str2.length()) return -1; else return str1.compare(str2);}//高精度加法//只能是两个正数相加string add(string str1,string str2)//高精度加法{ string str; int len1=str1.length(); int len2=str2.length(); //前面补0,弄成长度相同 if(len1<len2) { for(int i=1;i<=len2-len1;i++) str1="0"+str1; } else { for(int i=1;i<=len1-len2;i++) str2="0"+str2; } len1=str1.length(); int cf=0; int temp; for(int i=len1-1;i>=0;i--) { temp=str1[i]-'0'+str2[i]-'0'+cf; cf=temp/10; temp%=10; str=char(temp+'0')+str; } if(cf!=0) str=char(cf+'0')+str; return str;}//高精度减法//只能是两个正数相减,而且要大减小string sub(string str1,string str2)//高精度减法{ string str; int tmp=str1.length()-str2.length(); int cf=0; for(int i=str2.length()-1;i>=0;i--) { if(str1[tmp+i]<str2[i]+cf) { str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str; cf=1; } else { str=char(str1[tmp+i]-str2[i]-cf+'0')+str; cf=0; } } for(int i=tmp-1;i>=0;i--) { if(str1[i]-cf>='0') { str=char(str1[i]-cf)+str; cf=0; } else { str=char(str1[i]-cf+10)+str; cf=1; } } str.erase(0,str.find_first_not_of('0'));//去除结果中多余的前导0 return str;}//高精度乘法//只能是两个正数相乘string mul(string str1,string str2){ string str; int len1=str1.length(); int len2=str2.length(); string tempstr; for(int i=len2-1;i>=0;i--) { tempstr=""; int temp=str2[i]-'0'; int t=0; int cf=0; if(temp!=0) { for(int j=1;j<=len2-1-i;j++) tempstr+="0"; for(int j=len1-1;j>=0;j--) { t=(temp*(str1[j]-'0')+cf)%10; cf=(temp*(str1[j]-'0')+cf)/10; tempstr=char(t+'0')+tempstr; } if(cf!=0) tempstr=char(cf+'0')+tempstr; } str=add(str,tempstr); } str.erase(0,str.find_first_not_of('0')); return str;}//高精度除法//两个正数相除,商为quotient,余数为residue//需要高精度减法和乘法void div(string str1,string str2,string "ient,string &residue){ quotient=residue="";//清空 if(str2=="0")//判断除数是否为0 { quotient=residue="ERROR"; return; } if(str1=="0")//判断被除数是否为0 { quotient=residue="0"; return; } int res=compare(str1,str2); if(res<0) { quotient="0"; residue=str1; return; } else if(res==0) { quotient="1"; residue="0"; return; } else { int len1=str1.length(); int len2=str2.length(); string tempstr; tempstr.append(str1,0,len2-1); for(int i=len2-1;i<len1;i++) { tempstr=tempstr+str1[i]; tempstr.erase(0,tempstr.find_first_not_of('0')); if(tempstr.empty()) tempstr="0"; for(char ch='9';ch>='0';ch--)//试商 { string str,tmp; str=str+ch; tmp=mul(str2,str); if(compare(tmp,tempstr)<=0)//试商成功 { quotient=quotient+ch; tempstr=sub(tempstr,tmp); break; } } } residue=tempstr; } quotient.erase(0,quotient.find_first_not_of('0')); if(quotient.empty()) quotient="0";}int main(){ string str1,str2; string str3,str4; while(cin>>str1>>str2) { cout<<add(str1,str2)<<endl; cout<<sub(str1,str2)<<endl; cout<<mul(str1,str2)<<endl; div(str1,str2,str3,str4); cout<<str3<<" "<<str4<<endl; } return 0;}
阅读全文
0 0
- 高精度 加、减、乘、除 模板
- 高精度加,减,乘,除,取模,模板
- [模板]高精度加、减、乘
- 高精度模板总结(string 实现加、减、乘、除)常用版
- C++高精度(加/减/乘/除)【封装】
- 高精度 加 减 乘
- 高精度代码(PSACAL)加·减·乘·除(高精度 div 2)
- 高精度模板---加、减、乘(非负数)
- 大数 加 减 乘 除
- Linux 加,减,乘,除
- [复习]高精度 高精度加/减/乘/除法
- 【高精度模板】【高精加减乘没有除】
- 复数类的加,减,乘,除
- 模拟笔算 (加 减 乘 除)
- 数的加 减 乘 除
- 浮点数加,减,乘,除运算
- 使用BigDecimal进行加、减、乘、除
- 加、减、乘、除(四则运算)
- 为什么互金平台纷纷赴美上市,国内A股失去宠爱?
- [图论紫书/Ch5] Ideal Path UVA
- 罗列当前文件夹下所有文件
- Eclipse在线安装插件进度缓慢问题
- Anaconda 错误:numpy.core.multiarray failed to import
- 高精度 加、减、乘、除 模板
- hibernate学习以及文件以及注释
- Java Web Cookie跟Session
- 總結——關於2017 10 9測試的分析總結
- Java线程池 ExecutorService
- android 抽屉菜单
- idea如何配置dtd
- 索引/PL/SQL编程/类型与赋值/控制流程/游标/动态语句/异常处理/导入/导出
- Android如何正确获得View的宽和高