/**
*
*/
package homeWork;
import javax.swing.JOptionPane;
/**
* @author Administrator
*
*/
public class HugeInteger {//表示类
private int array[] = new int[40];
private boolean signed = false;//符号
public HugeInteger() {
for (int i = 0; i < array.length; i++) {
array[i] = 0;
}
}
public HugeInteger(int[] setArray) {
for (int i = 0; i < setArray.length; i++) {
array[i] = setArray[i];
}
}
public void input() {
String Int;
char charArray[] = new char[array.length];
StringBuffer buffer;
for (int i = 0; i < charArray.length; i++) {
charArray[i] = '0';
}
Int = JOptionPane.showInputDialog("Please input the HugeInteger:");
buffer = new StringBuffer(Int);
buffer.reverse();// 倒序,调整高位
buffer.getChars(0, buffer.length(), charArray, 0);
for (int i = 0; i < charArray.length; i++) {
array[i] = charArray[i] - 48;// 把char转换成int
}
}
public String output() {
String outputString = "";
boolean type = false;//用于标记高位的0
// outputString += "The HugeInteger is: ";
if (signed)
outputString += "-";
for (int i = array.length - 1; i >= 0; i--) {
if (array[i] != 0) {
outputString += array[i];
type = true;
} else if (type)
outputString += array[i];// 不输出高位的0
}
return outputString;
}
public HugeInteger add(HugeInteger huge) {
int TempArray[] = new int[40];
int n = 0;
for (int i = 0; i < array.length; i++) {
n = array[i] + huge.array[i] + TempArray[i];
if (n >= 10) {
TempArray[i + 1] += 1;// 进位
n = n - 10;
}
TempArray[i] = n;
}
return new HugeInteger(TempArray);
}
public HugeInteger sbstact(HugeInteger huge) {
int result[] = new int[40];
if (isGreaterThan(huge))
for (int i = 0; i < array.length; i++) {
if (array[i] < huge.array[i] && i < array.length - 1) {
array[i + 1]--;
array[i] += 10;
}
result[i] = array[i] - huge.array[i];
}
else {
signed = true;
for (int i = 0; i < array.length; i++) {
if (array[i] > huge.array[i] && i < array.length - 1) {
huge.array[i + 1]--;
huge.array[i] += 10;
}
result[i] = huge.array[i] - array[i];
}
}
return new HugeInteger(result);
}
public boolean isGreaterThan(HugeInteger huge) {
for (int i = array.length - 1; i >= 0; i--) {
if (array[i] < huge.array[i])
return false;
}
return true;
}
public boolean isEqualTo(HugeInteger huge) {
for (int i = 0; i < array.length; i++) {
if (array[i] != huge.array[i])
return false;
}
return true;
}
public boolean isZero() {
for (int i = 0; i < array.length; i++) {
if (array[i] != 0)
return false;
}
return true;
}
}