大数四则运算java(转)
// 大数的四则运算
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; class BIGINTEGEROPERATIONS
{
private:
static int COMPARE(string number1, string number2)
{
int j; int length1 = number1.size();
int length2 = number2.size(); if(number1.size() == 0) number1 = "0";
if(number2.size() == 0) number2 = "0"; j = 0;
for(int i = 0; i < length1; ++i)
{
if(number1[i] == '0') ++j;
else break;
}
number1 = number1.substr(j); j = 0;
for(int i = 0; i < length2; ++i)
{
if(number2[i] == '0') ++j;
else break;
}
number2 = number2.substr(j); length1 = number1.size();
length2 = number2.size(); if(length1 > length2)
{
return 1;
}
else if(length1 == length2)
{
if(number1.compare(number2) > 0)
{
return 1;
}
else if(number1.compare(number2) == 0)
{
return 0;
}
else
{
return -1;
}
}
else
{
return -1;
} return 0;
} public:
static string PLUS(string number1,string number2)
{
int i;
int length1 = number1.size();
int length2 = number2.size(); string result=""; reverse(number1.begin(), number1.end());
reverse(number2.begin(), number2.end()); for(i = 0; i < length1 && i < length2; i++)
{
char c = (char)(number1[i] + number2[i] - 48);
result = result + c;
} while(i < length1)
{
result = result + number1[i];
++i;
} while(i < length2)
{
result = result + number2[i];
++i;
} int carry = 0;
for(i = 0; i < (int)result.size(); ++i)
{
int value = result[i] - 48 + carry;
result[i] = (char)(value % 10 + 48);
carry = value / 10;
} if(carry !=0 )
{
result = result + (char)(carry + 48);
} for(i = result.size() - 1; i >= 0; i--)
{
if(result[i] != '0') break;
} result = result.substr(0, i + 1); reverse(result.begin(), result.end());
if(result.length() == 0) result = "0";
return result;
} static string MINUS(string number1,string number2)
{
int i;
string result = ""; int length1 = number1.size();
int length2 = number2.size(); if(COMPARE(number2,number1) > 0)
{
return "-" + MINUS(number2, number1);
} reverse(number1.begin(),number1.end());
reverse(number2.begin(),number2.end()); for(i = 0; i < length1 && i < length2; i++)
{
char c = number1[i] - number2[i] + 48;
result = result + c;
} if(i < length1)
{
for(; i < length1; i++)
{
result = result + number1[i];
}
} int carry = 0;
for(i = 0; i < (int)result.length(); i++)
{
int value = result[i] - 48 + carry;
if(value < 0)
{
value = value + 10;
carry = -1;
}
else carry = 0;
result[i]=(char)(value + 48);
} for(i = result.size() - 1; i >= 0; i--)
{
if(result[i] != '0')break;
} result = result.substr(0, i+1); reverse(result.begin(), result.end());
if(result.length()==0) result = "0";
return result;
} static string MULTIPLY(string number1, string number2)
{
int i, j;
int *iresult;
int length1 = number1.size();
int length2 = number2.size();
string result = ""; reverse(number1.begin(), number1.end());
reverse(number2.begin(), number2.end()); iresult = (int*)malloc(sizeof(int) * (length1 + length2 + 1));
memset(iresult, 0, sizeof(int) * (length1 + length2 + 1)); for(i = 0; i < length1; i++)
{
for(j = 0; j < length2; j++)
{
iresult[i+j] += ((number1[i] - 48) * (number2[j] - 48));
}
} int carry = 0;
for(i = 0; i < length1 + length2; i++)
{
int value = iresult[i] + carry;
iresult[i] = value % 10;
carry = value / 10;
} for(i = length1 + length2 - 1; i >= 0; i--)
{
if(iresult[i] != 0)break;
} for(; i >= 0; i--)
{
result = result + (char)(iresult[i]+48);
} free(iresult); if(result == "") result = "0";
return result;
} // 缺省地,商数向下取整, floatpoint用于指定保留小数点的位数
static string DIVIDE(string number1, string number2, int floatpoint = 0)
{
int i, j, pos;
string result = "";
string tempstr = "";
int length1 = number1.size();
int length2 = number2.size(); if((COMPARE(number2, number1) > 0) && (floatpoint == 0))
{
return "0";
} tempstr = number1.substr(0, length2);
pos = length2 - 1; while(pos < length1)
{
int quotient = 0;
while(COMPARE(tempstr, number2) >= 0)
{
quotient++;
tempstr = MINUS(tempstr, number2);
} result = result + (char)(quotient + 48);
pos++;
if(pos < length1)
{
tempstr += number1[pos];
}
} if(floatpoint > 0)
{
result += '.';
string stmp = "1";
int itmp = 0;
for(int k = 0; k < floatpoint; ++k)
{
stmp += '0';
if(COMPARE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2) < 0)
{
result += '0';
++itmp;
}
} string temp = DIVIDE(MULTIPLY(MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2)), stmp), number2);
if(temp[0] != '0') result += temp;
} j = result.size();
for(i = 0; i < j; i++)
{
if(result[i] != '0') break;
} result = result.substr(i, j); return result;
} static string MOD(string number1, string number2)
{
if(COMPARE(number2, number1) > 0)
{
return number1;
}
else if(COMPARE(number2, number1) == 0)
{
return "0";
}
else
{
return MINUS(number1, MULTIPLY(DIVIDE(number1, number2), number2));
}
}
}; int main(int argc, char* argv[])
{
string str1 = "9999999999999999999999999999999999999999";
string str2 = "9998999899989998999899989998999899989998"; cout << BIGINTEGEROPERATIONS::PLUS(str1, str2) << endl;
cout << "===============" << endl;
cout << BIGINTEGEROPERATIONS::MINUS(str1, str2) << endl;
cout << "===============" << endl;
cout << BIGINTEGEROPERATIONS::MULTIPLY(str1, str2) << endl;
cout << "===============" << endl;
cout << BIGINTEGEROPERATIONS::DIVIDE(str1, str2, 4) << endl;
cout << "===============" << endl;
cout << BIGINTEGEROPERATIONS::MOD(str1, str2) << endl; return 0;
}
大数四则运算java(转)的更多相关文章
- 四则运算 Java 杨辉鹏,郑冠华
四则运算 Java 杨辉鹏,郑冠华 GitHub链接:https://github.com/yanghuipeng/arithmetic 项目相关要求 使用 -n 参数控制生成题目的个数,例如 -n ...
- hdu 1002大数(Java)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 字串数_hdu_1261(大数极致).java
字串数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 两个大数相乘-Java
两个字符串表示两个非常大的数,请设计算法计算这两个大数的乘积,结果用字符串表示.例如S1="7832974972840919321747983209327",S2="19 ...
- 四则运算Java语言实验设计过程1
题目要求: 像二柱子那样,花二十分钟写一个能自动生成三十道小学四则运算题目的 “软件”.要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性).题目避免重复.可定制出题的数量. 设计思路 ...
- 大明A+B(hdu1753)大数,java
大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- 四则运算(Java)--温铭淇,付夏阳
GitHub项目地址: https://github.com/fxyJAVA/Calculation 四则运算项目要求: 程序处理用户需求的模式为: Myapp.exe -n num -r size ...
- 四则运算 Java 实现 刘丰璨,王翠鸾
四则运算 GitHub仓库 功能实现 [x] 使用 -n 参数控制生成题目的个数,并且根据解空间限制用户设定的范围(如 range == 2 时,用户却要求生成 10000 道题目,这明显不合理) [ ...
- 四则运算 Java (于泽浩,袁浩越)
GitHub 地址 一. 项目要求 题目 实现一个自动生成小学四则运算题目的命令行程序. 需求(全部完成) 使用 -n 参数控制生成题目的个数 Myapp.exe -n 10 使用 -r 参数控制题目 ...
随机推荐
- ArrayList反转
Collections.reverse(arrayList); 下面看例子: import java.util.ArrayList;import java.util.Collections; publ ...
- C#z中的虚方法,抽象类
1.虚方法 我在这动物类中定义了一个虚方法 Dosth class Animal { public int Aid; public string Name; public void SetAid(in ...
- Java学习笔记(一)语法
基本语法 大小写敏感 类名:对于所有的类来说,类名的首字母应该大写 方法名:所有的方法名都应该以小写字母开头.如果方法名含有若干单词,则后面的每个单词首字母大写. 源文件名:源文件名必须和类名相同.当 ...
- limit 检索记录行
LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数.LIMIT 接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定 ...
- Linux之shell备份数据库
功能概述: 使用shell脚本对服务器的数据库(全部或指定数据库)进行备份.对15天(固定天数)之前的数据库备份进行删除 命令实现: 待定
- Codeforces Round #522 Div2C(思维)
#include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...
- PAT12-012【建最小堆】
卧槽..没看清 值 还有负的.. PS: 注意他说是一个一个插入的,也就是插在完全二叉树的最末位置,然后向上更新. #include<bits/stdc++.h> using namesp ...
- cf780E(dfs)
题目链接: http://codeforces.com/problemset/problem/780/E 题意: 给出一个 n 个点 m 条边的图, 有 k 个人, 初始位置可以为任意位置, 每个人最 ...
- 2、kvm基础常用命令操作
KVM 虚拟机默认的配置文件在 /etc/libvirt/qemu 目录下,默认是以虚拟机名称命名的.xml文件,如下: root@xuedianhu:~# ls /etc/libvirt/qemu ...
- LDAP相关操作注意事项
lc.Modify(entry.DN, new LdapModification(LdapModification.REPLACE, new LdapAttribute("mDBUseDef ...