Use an array to represent a huge number

Source: Internet
Author: User

/**

*

*/

Package homework;

 

Import javax. Swing. joptionpane;

 

/**

* @ Author Administrator

*

*/

Public class hugeinteger {// represents a class

Private int array [] = new int [40];

Private Boolean signed = false; // symbol

 

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 (); // reverse order, adjust high

 

Buffer. getchars (0, buffer. Length (), chararray, 0 );

For (INT I = 0; I <chararray. length; I ++ ){

Array [I] = chararray [I]-48; // converts a char to an int

}

}

 

Public String output (){

String outputstring = "";

Boolean Type = false; // It is used to mark 0 at a high level.

 

// 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]; // do not output a high 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; // carry

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]

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]

Return false;

}

 

Return true;

}

 

Public Boolean isrelative to (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 stubhugeinteger hugeinteger1 = new hugeinteger (); hugeinteger hugeinteger2 = new hugeinteger (); hugeinteger resulthugeinteger = new hugeinteger (); string output = ""; hugeinteger1.input (); hugeinteg Er2.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 );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.