用数组表示巨大的数

/**

 * 

 */

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;

}

}

/**
 * 
 */
package homeWork;
import javax.swing.JOptionPane;
/**
 * @author Administrator
 * 
 */
public class HugeIntegerTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HugeInteger hugeInteger1 = new HugeInteger();
HugeInteger hugeInteger2 = new HugeInteger();
HugeInteger resultHugeInteger = new HugeInteger();
String output = "";
hugeInteger1.input();
hugeInteger2.input();
output += "hugeInteger1 is: " + hugeInteger1.output()
+ "/nhugeInteger2 is: " + hugeInteger2.output();
resultHugeInteger = hugeInteger1.add(hugeInteger2);
output += "/nhugeInteger1 + hugeInteger2 = "
+ resultHugeInteger.output();
resultHugeInteger = hugeInteger1.sbstact(hugeInteger2);
output += "/nhugeInteger1 - hugeInteger2 = ";
if (!hugeInteger1.isGreaterThan(hugeInteger2))
output += "-";
output += resultHugeInteger.output();
JOptionPane.showMessageDialog(null, output, "HugeInteger Test",
JOptionPane.INFORMATION_MESSAGE);
}
}

你可能感兴趣的:(JAVA)