#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
using namespace std; //compare比较函数:相等返回0,大于返回1,小于返回-1
int compare(string str1,string str2)
{
if(str1.length()>str2.length()) return ;
else if(str1.length()<str2.length()) return -;
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=;i<=len2-len1;i++)
str1=""+str1;
}
else
{
for(int i=;i<=len1-len2;i++)
str2=""+str2;
}
len1=str1.length();
int cf=;
int temp;
for(int i=len1-;i>=;i--)
{
temp=str1[i]-''+str2[i]-''+cf;
cf=temp/;
temp%=;
str=char(temp+'')+str;
}
if(cf!=) str=char(cf+'')+str;
return str;
}
//高精度减法
//只能是两个正数相减,而且要大减小
string sub(string str1,string str2)//高精度减法
{
string str;
int tmp=str1.length()-str2.length();
int cf=;
for(int i=str2.length()-;i>=;i--)
{
if(str1[tmp+i]<str2[i]+cf)
{
str=char(str1[tmp+i]-str2[i]-cf+''+)+str;
cf=;
}
else
{
str=char(str1[tmp+i]-str2[i]-cf+'')+str;
cf=;
}
}
for(int i=tmp-;i>=;i--)
{
if(str1[i]-cf>='')
{
str=char(str1[i]-cf)+str;
cf=;
}
else
{
str=char(str1[i]-cf+)+str;
cf=;
}
}
str.erase(,str.find_first_not_of(''));//去除结果中多余的前导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-;i>=;i--)
{
tempstr="";
int temp=str2[i]-'';
int t=;
int cf=;
if(temp!=)
{
for(int j=;j<=len2--i;j++)
tempstr+="";
for(int j=len1-;j>=;j--)
{
t=(temp*(str1[j]-'')+cf)%;
cf=(temp*(str1[j]-'')+cf)/;
tempstr=char(t+'')+tempstr;
}
if(cf!=) tempstr=char(cf+'')+tempstr;
}
str=add(str,tempstr);
}
str.erase(,str.find_first_not_of(''));
return str;
} //高精度除法
//两个正数相除,商为quotient,余数为residue
//需要高精度减法和乘法
void div(string str1,string str2,string &quotient,string &residue)
{
quotient=residue="";//清空
if(str2=="")//判断除数是否为0
{
quotient=residue="ERROR";
return;
}
if(str1=="")//判断被除数是否为0
{
quotient=residue="";
return;
}
int res=compare(str1,str2);
if(res<)
{
quotient="";
residue=str1;
return;
}
else if(res==)
{
quotient="";
residue="";
return;
}
else
{
int len1=str1.length();
int len2=str2.length();
string tempstr;
tempstr.append(str1,,len2-);
for(int i=len2-;i<len1;i++)
{
tempstr=tempstr+str1[i];
tempstr.erase(,tempstr.find_first_not_of(''));
if(tempstr.empty())
tempstr="";
for(char ch='';ch>='';ch--)//试商
{
string str,tmp;
str=str+ch;
tmp=mul(str2,str);
if(compare(tmp,tempstr)<=)//试商成功
{
quotient=quotient+ch;
tempstr=sub(tempstr,tmp);
break;
}
}
}
residue=tempstr;
}
quotient.erase(,quotient.find_first_not_of(''));
if(quotient.empty()) quotient="";
} 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 ;
}

加、减、乘、除 高精度 string的更多相关文章

  1. java加减天数

    指定日期的加减天数 public static String setDay(int num,String newDate) throws ParseException{ SimpleDateForma ...

  2. Flutter实战视频-移动电商-61.购物车_商品数量的加减操作

    61.购物车_商品数量的加减操作 provide/cart.dart pages/cart_page/cart_count.dart 先引入provide和cartProvide 定义接收一个item ...

  3. golang入门time与string转换, time加减时间, 两个时间差

    package main import ( "fmt" "time") var timeLayoutStr = "2006-01-02 15:04:0 ...

  4. Android带加减的edittext

    看了网上这样自带加减的edittext写得好复杂,还有各种监听事件,我觉得没有必有.于是我自己写了一个. 我这个edittext仅仅限制整数,每次加减1. public class TestEditT ...

  5. freemarker 数据做加减计算

    controller的部分: @Controller@RequestMapping("/ContactsFrameIndex")public class ContactsFrame ...

  6. Delphi日期函数、日期加减

    Delphi里有现成的函数可以实现日期加减,是在DateUtils单元里的. function IncYear(const AValue: TDateTime; const ANumberOfYear ...

  7. java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)

    获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...

  8. JAVA日期加减运算

    1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance();      calendar.setTime(new Date()) ...

  9. JS日期的获取与加减

    1)获取当前日期: var today = new Date(); 2)设定某个日期: var d = new Date("2015/1/08".replace(/-/g,&quo ...

  10. java日期加减

    1.用java.util.Calender来实现 Calendar calendar=Calendar.getInstance();      calendar.setTime(new Date()) ...

随机推荐

  1. LINUX下用C语言历遍目录 C语言列出目录 dirent.h在C/C++中的使用

    LINUX下历遍目录的方法一般是这样的打开目录->读取->关闭目录相关函数是opendir -> readdir -> closedir #include <dirent ...

  2. 496. Next Greater Element I 另一个数组中对应的更大元素

    [抄题]: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subse ...

  3. Inception安装

    前言: MySQL语句需要审核,这一点每个DBA及开发人员都懂,但介于语句及环境的复杂性,大部分人都是望而却步,对其都是采取妥协的态度,从而每个公司都有自己的方法. 大多数公司基本都是半自动化(脚本+ ...

  4. OpenCV 2.4.13 installed in Ubuntu 14 and CMakeLists Demo

    1. 配置编译器环境 [compiler] sudo apt-get install build-essential 2. 安装OpenCV的依赖包 [required] -dev pkg-confi ...

  5. Hyperledger Fabric Ordering Service过程

    排序服务在超级账本 Fabric 网络中起到十分核心的作用.所有交易在发送给 Committer 进行验证接受之前,需要先经过排序服务进行全局排序. 在目前架构中,排序服务的功能被抽取出来,作为单独的 ...

  6. 6.AND & OR 运算符

    AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤 AND 和 OR 运算符 AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来. 如果第一个条件和第二个条件都成立,则 A ...

  7. 1.介绍templates

    我们现在要计算int和double类型数据的平方,我们就需要2个函数: #include <iostream> using namespace std; int square(int x) ...

  8. ESP8266文档阅读ESP8266 SDK 入门指南

    ESP8266 SDK  入门指南 1.概述 1.2.ESP8266 HDK 1.3.ESP8266 SDK 1.4.ESP8266 FW 1.5.ESP8266 工具集 2.1.开发板方案 3.软件 ...

  9. 下载特定区域内街景照片数据 | Download Street View Photos within Selected Region

    作者:姜虹,刘子煜,王玥瑶,杨安琪,天靖居士 街景图片可以通过api下载,但需要提供参数,参数中的poiid.panoid.location可以用来确定位置或全景图片的ID以确定对应的街景图片.优先级 ...

  10. MSSQL 静态值查询

    有些时候可能会需要查询一些静态值 ), (),()) AS tabName ( c1 ) 演变1.多列 ,), (,),(,)) AS tabName ( c1,c2 ) 演变2.聚合 ), (),( ...