所以我們的目標是讀取兩個大整數並將它們加在一起,將它們一起減去並乘以一個40個元素的長整數數組,同時還比較初始兩個整數equalTo,notEqualTo等。其中大部分完成,但我堅持了幾點。我的isGreaterThan方法返回與它應該相反的結果,例如:大整數1 = 100和大整數2 = -100。該方法會吐出假,並說巨大的整數1 isLessThan巨大的整數2.下一部分我堅持的是乘法。到目前爲止,我可以將整數相乘,得到一個不超過11個元素的新整數。對我來說一些未知的原因,任何更大的將導致錯誤計算。有人建議我多次循環,但我不明白這會有什麼幫助。我的最後一個問題是任何操作的結果等於零,例如100 +(-100),我的add/substract/multiply方法不會返回0.下面是我當前的代碼。數組比較和乘法
import java.util.Scanner;
import java.util.Arrays;
// Class that prompts the user to enter two HugeIntegers
// then compares those integers and performs addition, subtraction,
// and multiplication on those two HugeIntegers
public class HugeInteger {
private static final int NUM_DIGITS = 40;
private int digits[] = new int[NUM_DIGITS];
private boolean positive;
// Constructor
public HugeInteger (String num){
String parts [] = num.split(" ");
digits = new int[parts.length];
for(int n = 0; n < parts.length; n++) {
digits[n] = Integer.parseInt(parts[n]);
}
}
// Finds first non-zero position for first HugeInteger
public int findFirstNonZeroPosition(){
int position = NUM_DIGITS-1;
for (int i = 0; i < digits.length; i++){
if (digits[i] > 0){
position = i;
break;
}
}
return position;
}
// Determines if h1 isEqualTo h2
public boolean isEqualTo(HugeInteger hi){
if(Arrays.equals(this.digits, hi.digits)){
return true;
}else {
return false;
}
}
// Determines if hi isGreaterThan h2
public boolean isGreaterThan(HugeInteger hi){
// different signs
if (this.positive && (!hi.positive)){
return true;
}
else if (!this.positive && hi.positive){
return false;
}
// same sign
else {
// first number’s length is less than second number’s length
if (findFirstNonZeroPosition() > hi.findFirstNonZeroPosition()) {
if (positive)
return false;
else
return true;
}
// first number’s length is larger than that of second number
else if (findFirstNonZeroPosition() < hi.findFirstNonZeroPosition()) {
if (positive)
return true;
else
return false;
}
// two numbers have same length
else {
for (int i = 0; i < digits.length; i++) {
if (this.digits[i] > hi.digits[i])
if (positive)
return true;
else
return false;
}
if (positive)
return false;
else
return true;
}
}
}
// Determines if h1 isNotEqualTo h2
public boolean isNotEqualTo(HugeInteger hi){
if(Arrays.equals(this.digits, hi.digits)){
return false;
}else {
return true;
}
}
// Determines if h1 isLessThan h2
public boolean isLessThan(HugeInteger hi){
return !(isGreaterThan(hi) || isEqualTo(hi));
}
// Determines if h1 isGreaterThanOrEqualTo h2
public boolean isGreaterThanOrEqualTo(HugeInteger hi){
return !isLessThan(hi);
}
// Determines if h1 isLessThanOrEqualTo h2
public boolean isLessThanOrEqualTo(HugeInteger hi){
return !isGreaterThan(hi);
}
// instance variables are digits, NUM_DIGITS, positive
// addArrayDigits and subtractArrayDigits
// Determines how to add h1 and h2
public void add(HugeInteger hi){
if(positive!=hi.positive){
if(this.positive){
// "this" is positive, hi is negative
hi.negate(); // negate hi temporarily
if(this.isGreaterThan(hi)){
// |this| > |hi|
this.digits = subtractArrayDigits(this.digits, hi.digits);
}else{
// |this| <= |hi|
this.digits = subtractArrayDigits(hi.digits, this.digits);
// negate the "this"
negate();
}
hi.negate(); // restore hi's sign
}else{
// "this" is negative, hi is positive
}
}else{
// same sign :)
digits = addArrayDigits(this.digits, hi.digits);
}
}
// instance variables are digits, NUM_DIGITS, positive
// addArrayDigits and subtractArrayDigits
// Determines how to subtract h1 and h2
public void subtract(HugeInteger hi){
if(positive!=hi.positive){
if(this.positive){
// "this" is positive, hi is negative
hi.negate(); // negate hi temporarily
if(this.isGreaterThan(hi)){
// |this| > |hi|
this.digits = addArrayDigits(this.digits, hi.digits);
}else{
// |this| <= |hi|
this.digits = addArrayDigits(hi.digits, this.digits);
// negate the "this"
negate();
}
hi.negate(); // restore hi's sign
}else{
// "this" is negative, hi is positive
}
}else{
// same sign :)
digits = subtractArrayDigits(this.digits, hi.digits);
}
}
// Multiplies h1 and h2
public void multiply(HugeInteger hi){
for (int i = 0; i < digits.length; ++i) {
digits[i] = this.digits[i] * hi.digits[i];
}
}
// Flips the sign
public void negate(){
positive =! positive;
}
// Determines if an element is zero
public boolean isZero(){
for(int i = 0; i < digits.length; i++)
if(digits[i]!= 0)
return false;
return true;
}
// Puts HugeInteger into a String in LSD format
public String toString() {
String str = "";
int i;
for(i = digits.length -1; i >= 0; i--) {
if(digits[i] != 0)
break;
}
for(int j = i; j >= 0; j--) {
str = digits[j] + str;
}
return str;
}
// Subtracts h2 from h1
private static int[] subtractArrayDigits(int[] array1, int[] array2){
for (int i = 0; i < array1.length; ++i) {
array1[i] = array1[i] - array2[i];
}
return array1;
}
// Adds h2 to h1
private static int[] addArrayDigits(int[] array1, int[] array2){
//int i = 0;
for (int i = 0; i < array1.length; ++i) {
array1[i] = array1[i] + array2[i];
}
return array1;
}
// Main method
public static void main(String args[]){
HugeInteger h1, h2;
String num;
Scanner scan=new Scanner(System.in);
System.out.print("Please enter the first huge integer (h1): ");
num=scan.nextLine();
h1=new HugeInteger(num);
System.out.print("Please enter the second huge integer (h2): ");
num=scan.nextLine();
h2=new HugeInteger(num);
if(h1.isEqualTo(h2)){
System.out.println("h1 is equal to h2.");
}
if(h1.isNotEqualTo(h2)){
System.out.println("h1 is not equal to h2.");
}
if(h1.isGreaterThan(h2)){
System.out.println("h1 is greater than h2.");
}
if(h1.isLessThan(h2)){
System.out.println("h1 is less than to h2.");
}
if(h1.isGreaterThanOrEqualTo(h2)){
System.out.println("h1 is greater than or equal to h2.");
}
if(h1.isLessThanOrEqualTo(h2)){
System.out.println("h1 is less than or equal to h2.");
}
h1.add(h2);
System.out.printf("h1 + h2 = %s\n",h1);
h1.subtract(h2);
h1.subtract(h2);
System.out.printf("h1 - h2 = %s\n",h1);
h1.add(h2);
h1.multiply(h2);
System.out.printf("h1 * h2 = %s\n",h1);
}
}
我加了positive = true;到構造函數,我所有的比較邏輯都工作得很好。唯一不起作用的是大整數的乘法,並得到等於零的操作的結果以顯示零。我認爲這是錯誤的,因爲,您如何指出,我的操作數可能位於數組中間的某個位置。如果我有這樣的錯誤,我該如何去對齊LSD到陣列的右側? – 2014-09-04 23:42:49
由於你的算法似乎假定LSD在索引零,我建議你離開它。當你給數字賦值時,使用'digits [n] = Integer.parseInt(parts [part.length - n - 1])''。 – 2014-09-05 15:15:07
如果您的算法似乎現在正在工作,那麼很可能是因爲您測試的數字與int可容納的值範圍相比較小。嘗試添加2000000000和2000000000這兩個單數(大數)數字。'multiply()'方法更糟糕:即使在考慮數字值之前,當任一操作數具有多個(巨大)數字時,它也會失敗。嘗試乘以1(== 2147483649十進制)乘以2 - 甚至減去1. – 2014-09-05 15:25:54