西电实验:Java实现大整数类(重写、完善)
作者:互联网
西电实验:Java实现大整数类(重写、完善)
关于实验内容
https://blog.csdn.net/qq_44274276/article/details/105079948
重写大整数类
自然数类和大整数类其实没啥本质区别,就是自然数是非负的,不需要考虑符号而已!所以我就不重写自然数了,就写大整数吧!
MyInteger.java程序
package number;
import java.util.Scanner;
/**
* @author jiangzl
*/
public class MyInteger {
private static final int DEFAULT_SIZE = 32;
public static final MyInteger MAX_VALUE = new MyInteger("9999999999999999999999999999999999");
public static final MyInteger MIN_VALUE = new MyInteger("-99999999999999999999999999999999999");
private static final MyInteger zero = new MyInteger(0);
private static final MyInteger one = new MyInteger(1);
private int[] number;
private int size;
private int capacity = DEFAULT_SIZE;
private String value;
private boolean isNotNegative;
private boolean isNumber() {
int len = value.length();
boolean flag = true;
for (int i = 0; i < len; ++i) {
if (value.charAt(i) < '0' || value.charAt(i) > '9') {
if (',' == value.charAt(i) && 0 == (len - i + 1) % 3) {
continue;
}
if ('-' == value.charAt(i) && 0 == i) {
continue;
}
flag = false;
}
}
return flag;
}
public MyInteger() {
value = "0";
size = 1;
isNotNegative = true;
number = new int[capacity];
}
public MyInteger(int i) {
String builder = Integer.toString(i);
size = builder.length();
number = new int[size];
StringBuilder valueBuilder = new StringBuilder();
isNotNegative = (i >= 0);
if (!isNotNegative) {
valueBuilder.append('-');
}
int l = (isNotNegative ? 0 : 1);
for (; l < size; ++l) {
number[l] = builder.charAt(l) - '0';
valueBuilder.append(number[l]);
}
value = valueBuilder.toString();
}
public MyInteger(int[] num) {
size = num.length;
number = new int[capacity];
isNotNegative = true;
System.arraycopy(num, 0, number, 0, size);
StringBuilder valueBuilder = new StringBuilder();
for (int i = 0; i < size; ++i) {
valueBuilder.append(number[i]);
}
value = valueBuilder.toString();
}
public MyInteger(String s) {
size = s.length();
number = new int[size];
isNotNegative = (s.charAt(0) != '-');
int i = (isNotNegative ? 0 : 1);
for (; i < size; ++i) {
number[i] = s.charAt(i) - '0';
}
value = s;
}
public static MyInteger getMyIntegerWithThousands(String s) {
StringBuilder builder = new StringBuilder();
MyInteger ret = new MyInteger();
int len = s.length();
if ('-' == s.charAt(0)) {
ret.isNotNegative = false;
}
for (int i = 0; i < len; ++i) {
if (',' != s.charAt(i)) {
builder.append(s.charAt(i));
}
}
ret.value = builder.toString();
return ret;
}
public MyInteger(final MyInteger integer) {
capacity = integer.capacity;
size = integer.size;
number = new int[size];
isNotNegative = integer.isNotNegative;
System.arraycopy(integer.number, 0, number, 0, size);
value = integer.value;
}
public void scanInteger() {
Scanner scanner = new Scanner(System.in);
value = scanner.next();
if (isNumber()) {
isNotNegative = (value.charAt(0) != '-');
size = value.length();
number = new int[size];
int i = (isNotNegative ? 0 : 1);
for (; i < size; ++i) {
number[i] = value.charAt(i) - '0';
}
}
else {
value = "0";
isNotNegative = true;
size = 1;
number = new int[capacity];
}
}
private boolean isAbsLessThan(final MyInteger integer) {
int size1 = size, size2 = integer.size;
if (!isNotNegative) {
size1--;
}
if (!integer.isNotNegative) {
size2--;
}
if (size1 < size2) {
return true;
}
else if (size1 > size2) {
return false;
}
int len = size;
for (int i = 0; i < len; ++i) {
if (number[i] < integer.number[i]) {
return true;
}
else if (number[i] > integer.number[i]) {
return false;
}
}
return false;
}
private boolean isLessThan(final MyInteger integer) {
if (!(isNotNegative && integer.isNotNegative)) {
if (!isNotNegative) {
return true;
}
else {
return false;
}
}
return isAbsLessThan(integer);
}
public boolean equals(final MyInteger integer) {
return (!isLessThan(integer) && !integer.isLessThan(this));
}
private MyInteger addAbs(final MyInteger integer) {
MyInteger ret = new MyInteger(integer);
int len = Math.max(size, integer.size);
int[] t1 = new int[len + 1];
int[] t2 = new int[len + 1];
int[] ad = new int[len + 1];
for (int i = 0; i < size; ++i) {
t1[i] = number[size - i - 1];
}
for (int i = 0; i < integer.size; ++i) {
t2[i] = integer.number[integer.size - i - 1];
}
for (int i = 0; i < len; ++i) {
ad[i] = t1[i] + t2[i];
}
for (int i = 0; i < len; ++i) {
if (ad[i] >= 10) {
ad[i + 1] += ad[i] / 10;
ad[i] %= 10;
}
}
while (0 == ad[len] && len > 0) {
len--;
}
ret.size = len + 1;
ret.number = new int[ret.size];
StringBuilder addBuilder = new StringBuilder();
for (int i = 0; i < ret.size; ++i) {
ret.number[i] = ad[ret.size - i - 1];
addBuilder.append(ret.number[i]);
}
ret.value = addBuilder.toString();
return ret;
}
private MyInteger subAbs(final MyInteger integer) {
MyInteger ret = new MyInteger();
int len = Math.max(size, integer.size);
int[] i1 = new int[len + 1];
int[] i2 = new int[len + 1];
int[] su = new int[len + 1];
for (int i = 0; i < size; ++i) {
i1[i] = number[size - i - 1];
}
for (int i = 0; i < integer.size; ++i) {
i2[i] = integer.number[integer.size - i - 1];
}
for (int i = 0; i < len; ++i) {
su[i] = i1[i] - i2[i];
}
for (int i = 0; i < len; ++i) {
if (su[i] < 0) {
su[i + 1]--;
su[i] += 10;
}
}
while (0 == su[len] && len > 0) {
len--;
}
ret.size = len + 1;
ret.number = new int[ret.size];
StringBuilder subBuilder = new StringBuilder();
for (int i = 0; i < ret.size; ++i) {
ret.number[i] = su[ret.size - i - 1];
subBuilder.append(ret.number[i]);
}
ret.value = subBuilder.toString();
return ret;
}
private MyInteger mulAbs(final MyInteger integer) {
MyInteger ret = new MyInteger(integer);
int len = size + integer.size;
int[] i1 = new int[size + 1];
int[] i2 = new int[integer.size + 1];
int[] mu = new int[len + 1];
for (int i = 0; i < size; ++i) {
i1[i] = number[size - i - 1];
}
for (int i = 0; i < integer.size; ++i) {
i2[i] = integer.number[integer.size - i - 1];
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < integer.size; ++j) {
mu[i + j] += i1[i] * i2[j];
}
}
for (int i = 0; i < len; ++i) {
if (mu[i] >= 10) {
mu[i + 1] += mu[i] / 10;
mu[i] %= 10;
}
}
while (0 == mu[len] && len > 0) {
len--;
}
ret.size = len + 1;
ret.number = new int[ret.size];
StringBuilder mulBuilder = new StringBuilder();
for (int i = 0; i < ret.size; ++i) {
ret.number[i] = mu[ret.size - i - 1];
mulBuilder.append(ret.number[i]);
}
ret.value = mulBuilder.toString();
return ret;
}
private MyInteger divAbs(final MyInteger integer) throws Exception {
MyInteger ret = new MyInteger();
if ((new MyInteger(0).equals(integer))) {
throw new Exception("Exception: The divisor can not be zero!");
}
if (isAbsLessThan(integer)) {
return new MyInteger(0);
}
MyInteger divInt = new MyInteger(this);
while (!divInt.isAbsLessThan(integer)) {
ret = ret.addAbs(one);
divInt = divInt.subAbs(integer);
}
return ret;
}
private MyInteger modAbs(final MyInteger integer) throws Exception {
MyInteger ret = new MyInteger();
if ((new MyInteger(0).equals(integer))) {
throw new Exception("Exception: The divisor can not be zero!");
}
if (!isNotNegative || !integer.isNotNegative) {
throw new Exception("Exception: The modulus cannot be negative!");
}
ret = this.subAbs(integer.mulAbs(this.divAbs(integer)));
return ret;
}
public MyInteger add(final MyInteger integer) {
MyInteger ret = new MyInteger(integer);
if (isNotNegative && integer.isNotNegative) {
ret.isNotNegative = true;
ret = addAbs(integer);
}
else if (!isNotNegative && !integer.isNotNegative) {
ret.isNotNegative = false;
ret = addAbs(integer);
}
else {
if (isAbsLessThan(integer)) {
ret = integer.subAbs(this);
ret.isNotNegative = !isNotNegative;
}
else {
ret = subAbs(integer);
ret.isNotNegative = isNotNegative;
}
}
if (!ret.isNotNegative) {
ret.value = "-" + ret.value;
}
return ret;
}
public MyInteger sub(final MyInteger integer) {
MyInteger ret = new MyInteger(integer);
if (isNotNegative == integer.isNotNegative) {
if (isAbsLessThan(integer)) {
ret = integer.subAbs(this);
ret.isNotNegative = integer.isNotNegative;
}
else {
ret = subAbs(integer);
ret.isNotNegative = isNotNegative;
}
}
else {
ret = addAbs(integer);
ret.isNotNegative = isNotNegative;
}
if (!ret.isNotNegative) {
ret.value = "-" + ret.value;
}
return ret;
}
public MyInteger mul(final MyInteger integer) {
MyInteger ret = mulAbs(integer);
ret.isNotNegative = (isNotNegative == integer.isNotNegative);
if (!ret.isNotNegative) {
ret.value = "-" + ret.value;
}
return ret;
}
public MyInteger div(final MyInteger integer) throws Exception {
MyInteger ret = divAbs(integer);
ret.isNotNegative = (isNotNegative == integer.isNotNegative);
if (!ret.isNotNegative) {
ret.value = "-" + ret.value;
}
return ret;
}
public MyInteger mod(final MyInteger integer) throws Exception {
return modAbs(integer);
}
@Override
public String toString() {
return value;
}
}
Main测试程序
package test;
import number.MyInteger;
import java.util.Scanner;
/**
* @author jiangzl
*/
public class MainOfInteger {
public static void main(String[] args) throws Exception {
MyInteger integer = new MyInteger();
System.out.println(integer);
integer = new MyInteger(-35535);
System.out.println(integer);
// integer=-35535
int[] num = {1, 2, 3, 4, 5};
integer = new MyInteger(num);
System.out.println(integer);
// integer=12345
MyInteger newInt = new MyInteger();
System.out.println(newInt);
// newInt=0
newInt = integer;
System.out.println(newInt);
MyInteger integer1 = new MyInteger("-123");
// integer1=-123
System.out.println(integer1);
MyInteger scanInt = new MyInteger();
scanInt.scanInteger();
System.out.println(scanInt);
MyInteger addRes = integer.add(integer1);
System.out.println(addRes);
// add : -123+12345=12222
MyInteger subRes = integer.sub(integer1);
System.out.println(subRes);
// sub : 12345-(-123)=12468
MyInteger mulRes = integer.mul(integer1);
System.out.println(mulRes);
// mul : -123*12345=-1518435
MyInteger divRes = integer.div(integer1);
System.out.println(divRes);
// 12345/-123=-100
MyInteger i = new MyInteger(123);
MyInteger modRes = integer.mod(i);
System.out.println(modRes);
// 12345 % -123
MyInteger i2 = MyInteger.getMyIntegerWithThousands("-1,000,000");
System.out.println(i2);
}
}
结束
这次的代码写的就比以前写的完善很多了,也花了一下午的时间,希望大家多提出意见~
标签:Java,MyInteger,int,西电,ret,new,integer,重写,size 来源: https://blog.csdn.net/qq_44274276/article/details/116607548