62,634
社区成员




public final class RealNumber {
private final boolean positive;//正负
private final char[] integer;//整数部分
private final char[] decimal;//小数部分
public RealNumber(String value) {
positive = value.charAt(0)!='-';
int index = value.indexOf('.');
if(index>0){
this.integer = value.substring(positive?0:1, index).toCharArray();
this.decimal = value.substring(index+1).toCharArray();
}else{
this.integer = value.substring(positive?0:1).toCharArray();
this.decimal = "".toCharArray();
}
}
public RealNumber(boolean positive, char[] integer, char[] decimal) {
super();
this.positive = positive;
this.integer = integer;
this.decimal = decimal;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder(positive?"":"-");
buffer.append(integer);
if(decimal.length>0){
buffer.append('.').append(decimal);
}
return buffer.toString();
}
public RealNumber add(RealNumber that){
int size = Math.max(this.integer.length, that.integer.length)+Math.max(this.decimal.length,that.decimal.length);
char[]thisnumber = new char[size+1];
char[]thatnumber = new char[size+1];
char[]result = new char[size+1];
fill(thisnumber, '0');
fill(thatnumber, '0');
fill(result, '0');
int index = Math.max(this.integer.length, that.integer.length);
System.arraycopy(this.integer, 0, thisnumber, index-this.integer.length+1, this.integer.length);
System.arraycopy(that.integer, 0, thatnumber, index-that.integer.length+1, that.integer.length);
System.arraycopy(this.decimal, 0, thisnumber, index+1, this.decimal.length);
System.arraycopy(that.decimal, 0, thatnumber, index+1, that.decimal.length);
boolean positive = this.positive;
if(this.positive==that.positive){
boolean carry = false;
int value = 0;
for(int i=result.length-1; i>=0; i--){
value = (thisnumber[i]-'0') + (thatnumber[i]-'0') + (carry?1:0);
result[i] = (char)( value%10 + '0');
carry = value>=10;
}
}else{
int compare = compare(thisnumber,thatnumber);
if(compare==0){
return new RealNumber("0");
}
positive = compare>0 ? this.positive : that.positive;
if(compare<0){
char[] temp = thisnumber;
thisnumber = thatnumber;
thatnumber = temp;
}
boolean carry = false;
int value = 0;
for(int i = result.length-1; i>=0; i--){
value = (thisnumber[i]-'0') - (thatnumber[i]-'0') - (carry?1:0);
carry = value<0;
value = value<0 ? 10+value : value;
result[i] = (char)(value%10 + '0');
}
}
int frontIndex = 0;
for(frontIndex = 0; frontIndex < index; frontIndex++){
if(result[frontIndex]!='0'){
break;
}
}
char integer[] = new char[index-frontIndex+1];
System.arraycopy(result, frontIndex, integer, 0, integer.length);
int lastIndex = result.length-1;
for(lastIndex=result.length-1; lastIndex>=index; lastIndex--){
if(result[lastIndex]!='0'){
break;
}
}
char decimal[] = new char[lastIndex-index];
System.arraycopy(result, index+1, decimal, 0, decimal.length);
return new RealNumber(positive, integer, decimal);
}
private static int compare(char[] thisnumber, char[] thatnumber) {
for(int i=0; i<thisnumber.length; i++){
if(thisnumber[i] == thatnumber[i]){
continue;
}
return thisnumber[i] > thatnumber[i] ? 1 : -1;
}
return 0;
}
private static void fill(char[] buffer, char value) {
for(int i=0;i<buffer.length;i++){
buffer[i] = value;
}
}
public static void main(String[] args) {
RealNumber a = new RealNumber("100.1");
RealNumber b = new RealNumber("-92.001");
System.out.println(a.add(b).toString());
}
}
public class B {
public static void main(String[] args) throws Exception {
Big a=new Big("99999999999999999999999999999999999999999999999999999");
Big b=new Big("1");
System.out.println(a+","+b);
System.out.println(a.add(b));
}
}
class Big{
private String str;
private List<Long> val;
public Big(String str){
this.str=str;
val=new ArrayList<Long>();
while(str.length()>18){
String s=str.substring(str.length()-18);
Long l=Long.parseLong(s);
val.add(l);
str=str.substring(0, str.length()-18);
}
val.add(Long.parseLong(str));
}
public Big add(Big b){
for(int i=0;i<val.size()&&i<b.val.size();i++){
val.set(i, val.get(i)+b.val.get(i));
}
if(b.val.size()>val.size()){
val.addAll(b.val.subList(val.size(),b.val.size()));
}
int inc=0;
String sb=new String();
DecimalFormat df=new DecimalFormat("000000000000000000");
for(int i=0;i<val.size();i++){
Long l=val.get(i);
if(inc==1){
l++;
}
inc=(l>999999999999999999l)?1:0;
if(inc==1){
l=l- 1000000000000000000l;
}
val.set(i, l);
if(i!=val.size()-1){
sb=df.format(l)+sb;
}else{
sb=l+sb;
}
}
str=sb;
return this;
}
public String toString(){
List<Long> l=new ArrayList<Long>(val);
Collections.reverse(l);
return str+":"+l;
}
}
package dmiaes.client.view;
public class 大数据计算 {
//大数据相加结果
static String result = "";
//用来存放临时进位的数
static int jinwei = 0;
/**
* @param args
*/
public static void main(String[] args) {
//加数和被加数
String a = "123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123";
String b = "7453652334213274536523342132745365233421327453652334213274536523342132745365233421327453652334213274536523342132745365233421327453652334213274536523342132";
//计算
compute(a, b);
System.out.println(result);
}
/**
* 计算大数据相加
* @param str1 加数
* @param str2 被加数
*/
static void compute(String str1, String str2) {
for (int i = Math.max(str1.length(), str2.length()); i > 0; i--) {
//从大数据的最低位开始计算
add(getlast(str1), getlast(str2) );
//加数去除已计算过的最低位
if(str1.length()>=1){
str1 = str1.substring(0, str1.length()-1);
}
//被加数去除已计算过的最低位
if(str2.length()>=1){
str2 = str2.substring(0, str2.length()-1);
}
}
//不能忘记计算结束后,最高的的进位需要加上来哦
if(jinwei>0){
result = jinwei + result;
jinwei = 0;
}
}
/**
* 取出字符串最后一个数字,即取得最低位
* @param str
* @return
*/
static int getlast(String str){
if(str == null || "".equals(str)){
return 0;
}
return Integer.valueOf(str.substring(str.length()-1)).intValue();
}
/**
* 取得进位,即两个1位int相加的10位数
* @param str
* @return
*/
static int getjinwei(String str){
if(str == null || str == "" || str.length()<2){
return 0;
}
return Integer.valueOf(str.substring(0, 1)).intValue();
}
/**
* 按小学数学计算方式,从最低位开始计算
* @param a 为加数当前最低位
* @param b 为被加数当前最低位
*/
static void add(int a, int b) {
//当前计算到的最低位相加,同时加上上次最低位相加的进位
int temp = a + b + jinwei;
String strTemp = String.valueOf(temp);
//将结果拼到当前结果的最前面
result = getlast(strTemp) + result;
//得到下次计算的进位
jinwei = getjinwei(strTemp);
}
}
public class CalculatorMain {
public static void main(String[] args) {
BigDicimalCalculator calculator = new BigDicimalCalculator();
System.out.println(calculator.add("99999999","999999999"));
}
}
class BigDicimalCalculator {
public String add(final String a, final String b) {
if (a==null || a.equals("")) {
return b;
}
if (b==null || b.equals("")) {
return a;
}
StringBuilder c = new StringBuilder();
int i=a.length() - 1;
int j=b.length() - 1;
boolean hasOverBit = false;
while (i >= 0 && j >= 0) {
int ia = Integer.parseInt(a.substring(i, i+1));
int ib = Integer.parseInt(b.substring(j, j+1));
int ic = ia + ib;
if (hasOverBit) {
ic++;
hasOverBit = false;
}
if (ic > 9) {
hasOverBit = true;
ic = ic % 10;
}
c.insert(0, ic);
i--;
j--;
}
while (i>=0) {
int ia = Integer.parseInt(a.substring(i, i+1));
int ic = ia;
if (hasOverBit) {
ic++;
hasOverBit = false;
}
if (ic > 9) {
hasOverBit = true;
ic = ic % 10;
}
c.insert(0, ic);
i--;
}
while (j>=0) {
int ib=Integer.parseInt(b.substring(j, j+1));
int ic = ib;
if (hasOverBit) {
ic++;
hasOverBit = false;
}
if (ic > 9) {
hasOverBit = true;
ic = ic % 10;
}
c.insert(0, ic);
j--;
}
if (hasOverBit) {
c.insert(0, 1);
}
return c.toString();
}
}
public static void main(String[] args) {
String a = "999968", b = "989";
byte[] l, s;
final char zero = '0', nine = '9';
if (a.length() > b.length()) {
l = a.getBytes();
s = b.getBytes();
} else {
l = b.getBytes();
s = a.getBytes();
}
if ((l.length == s.length && l[0] + s[0] >= nine) || l[0] >= nine) {
byte[] _l = new byte[l.length + 1];
System.arraycopy(l, 0, _l, 1, l.length);
l = _l;
l[0] = zero;
}
int il = l.length, is = s.length, i, start;
for (i = 1; i <= is; i++) {
byte p = (byte) (l[il - i] + s[is - i] - zero);
if (p > nine) {
l[il - i] = (byte) (p - 10);
l[il - i - 1] += 1;
} else {
l[il - i] = p;
}
}
do {
if (l[il - i] > nine) {
l[il - i] -= 10;
l[il - i - 1] += 1;
} else break;
} while (i++ < il);
System.out.println(new String(l, start=l[0]==zero?1:0, il-start));
}
public class Test {
public class BigInt{
int low;
int mid;
int high;
public BigInt(int high,int mid,int low) {
this.high=high;
this.mid=mid;
this.low=low;
}
public BigInt() {
}
public BigInt add(BigInt b) {
if(this.low>=1000000000||b.low>=1000000000||
this.mid>=1000000000||b.low>=1000000000||
this.high>=1000000000||b.low>=1000000000){
return null;
}
BigInt result=new BigInt();
result.low=this.low+b.low;
int low2mid=result.low/1000000000;
result.low=result.low%1000000000;
result.mid=this.mid+b.mid+low2mid;
int mid2high=result.mid/1000000000;
result.mid=result.mid%1000000000;
result.high=this.high+b.high+mid2high;
int isOver=result.high/1000000000;
result.high=result.high%1000000000;
if(isOver>0){return null;}
return result;
}
public String toString() {
String str=new String(String.format("%9d", high)+String.format("%9d", mid).replace(" ", "0")+String.format("%9d", low).replace(" ", "0"));
return str;
}
}
public static void main(String[] args) {
Test test=new Test();
test.addTest();
}
public void addTest() {
BigInt aBigInt=new BigInt(123456789,6789,123456789);
BigInt bBigInt=new BigInt(234567890,7890,234567890);
BigInt result=aBigInt.add(bBigInt);
System.out.println(result);
}
}
package socket;
public class meth {
public static void main(String[] args) {
String a = "123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123";
String b = "7453652334213274536523342132745365233421327453652334213274536523342132745365233421327453652334213274536523342132745365233421327453652334213274536523342132";
String c ="";
int ab;
boolean f=false;
if (a.length()>b.length()) {
ab=a.length()-b.length();
for (int i = a.length(); i >0; i--) {
String aa = a.substring(i-1,i);
String bb="";
if(i-1-ab>=0){
bb = b.substring(i-1-ab<0?0:i-1-ab,i-ab<0?0:i-ab);
}else{
bb="0";
}
if(f){
c=((Integer.parseInt(aa)+Integer.parseInt(bb))%10+1)+c;
}else{
c=((Integer.parseInt(aa)+Integer.parseInt(bb))%10)+c;
}
if(Integer.parseInt(aa)+Integer.parseInt(bb)>10){
f=true;
}else{
f=false;
}
}
}
System.out.println(c);
}
}