OPI_LR2011_ch3
.pdf61
= 5.2 – Character CharacterTest
= -
- .
5.2.2 7 Biglnteger
? @ ( .
+ Java f ,
@, .
E i (
-@ @, Java API Biglnteger, i f java.math. D g @f Number, (, doubleValue(), floatValue(), intValue(), longValue(). byteValue() shortValue()
, @ Number.
E 'f Biglnteger
, @. G i
, g f. / @
+ .
62
' @@ (
.
• @@ 'f BigDecimal
( +) .
E - ZERO ONE - @@ @
'f Biglnteger.
toByteArray() @f 'f . m g Biglnteger @@
+, @ 'f BigInteger:
–abs() – f 'f, i @ , i f 'f this;
–add(x) – this + ;
–and(x) – this & ;
–andNot(x) – this &(~ ) ;
–divide(x) – this / ;
–divideAndRemainder( ) – f 'f
Biglnteger, i g this ;
–gcd(x) – g , @,
'f this ;
–m ( ) – g 'f this ;
–min(x) – g 'f this ;
–mod(x) – g 'f this
;
–modInverse(x) – g ,
'f this, ;
–modPow(n, m) – g 'f this,
n, m ;
–multiply( ) – this * ;
–negate() – , i f 'f;
–not() – ~this ;
–r( ) – this | ;
–pow(n) – , i f 'f,
n ;
–remainder( ) – this % ;
–shiftLeft(n) – this « n ;
–shiftRight(n) – this » n;
–signum() – + sign(x) ;
–subtract(x) – this - x ;
63
– xor(x) – this ^ x.
5.3 ,
. 5.3 f f .
= 5.3 – Biglnteger BiglntegerTest
' 5.2. Biglnteger BiglntegerTest
import Java.math.Biglnteger; class BiglntegerTest{
public static void main(String[] args){
Biglnteger a = new Biglnteger("99999999999999999") ; Biglnteger b = new Biglnteger("88888888888888888888"); System.out.println("bits in a = " + a.bitLength()); System.out.println("bits in b = " + b.bitLength); System.out.println("a + b = " + a.add(b)); System.out.println("a & b = " + a.and(b)); System.out.println("a & ~b = " + a.andNot(b)); System.out.println("a / b = " + a.divide(b));
Biglnteger[] r = a.divideAndRemainder(b); System.out.println("a / b: q = " + r[0] + ", r = " + r[l]); System.out.println("gcd(a, b) = " + a.gcd(b));
64
System.out.println("max(a, b) = " + a.max(b)); System.out.printin("min(a, b) = " + a.min(b)); System.out.println("a mod b = " + a.mod(b)); System.out.println("I/a mod b = " + a.modlnverse(b)); System.out.println(" mod b = " + a.modPow(a, b)); System.out.println("a * b = " + a.multiply(b)); System.out.println("-a = " + a.negate());
System, out. println("~a = " + a.not()); System.out.println("a | b = " + a.or(b)); System.out.println(" 3 = " + a.pow(3)); System.out.println("a % b = " + a.remainder(b)); System.out.println("a « 3 = " + a.shiftLeft(3)}; System.out.println("a » 3 = " + a.shiftRight(3)); System.out.println("sign(a) = " + a.signum()); System.out.println("a - b = " + a.subtract(b)); System.out.println(" b = " + a.xor(b));
}
}
, i 5.2
Java.math.
5.2.3 7 Big Decimal
C BigDecimal g java.math.
C ( 'f f
: 'f BigInteger,
int.
:, 76.34862
7634862 'f Biglnteger, 5 int. ; , ( - +,
( integer.MAX_VALUE. = 'f BigDecimal f
,
:
–ROUND_CEILING – g ;
–ROUND_DOWN – , g
@ @;
–ROUND_FLOOR – g ;
–ROUND_HALF_DOWN – ( ,
f f g ;
–ROUND_HALF_EVEN – ( ,
f f ;
65
–ROOND_HALF_UP – ( ,
f f g ;
–ROUND_UNNECESSARY – f, i
, ;
–ROUND_UP – , g @
@.
BigDecimal :
–BigDecimal(Biglnteger bi) – 'f
bi, @f @;
–BigDecimal(Biglnteger mantissa, int scale) – f
mantissa scale 'f; i scale
, f ;
–BigDecimal(double d) – 'f
f d; i d Na, f
;
–BigDecimal(String val) – f val,
Java.
'
f f , . .
+ f,
, ,
'f, , BigDecimal(0.1), , i f
'f, ( @. ? . 4.5. 5
( 'f , BigDecimal("0.1"), @f 1.
C doubleValue(), floatValue(), intValue(), longValue().
m g @@
. ? @ 'f BigDecimal. ;
f 'f BigDecimal, n - int,
r – , i
:
–abs() – @ 'f this ;
–add(x) – this + ;
–divide( , r) – this / r ;
–divide( , n, r) – this / @
r ;
–m ( ) – g this ;
66
–min(x) – g this ;
–movePointLeft(n) – n ;
–movePointRight(n) – n ;
–multiply( ) – this * ;
–negate() – f 'f ;
–scale() – f ;
–setscale(n) – @f n ;
–setscale(n, r) – @f n f
r ;
–signum() – , i f 'f;
–subtract( ) – this - ;
–toBiginteger() – , i f 'f; unScaledValue() – f .
' 5.3 f ,
. 5.4 – .
= 5.4 – BigDecimal BigDecimalTest
67
' 5.3. BigDecimal
BigDecimalTest
import java.math.*; class BigDecimalTest{
public static void main,( String [] args) {
BigDecimal x = new BigDecimal("-12345.67890123456789"); BigDecimal y = new BigDecimal("345.7896e-4");
BigDecimal z = new BigDecimal(new Biglnteger("123456789"),8); System.out.println("|x| = " + x.abs()); System.out.println("x + y = " + x.add(y));
System.out.println("x / y = " + x.divide(y, BigDecimal.ROUND__DOWN)); System.out.println("S/N = " +
x.divide(y, 6, BigDecimal.ROUND_HALF_EVEN)); System.out.println("max(x, y) = " + x.max(y)); System.out.println("min(x, y) = " + x.min(y)); System.out.println("x « 3 = " * x.movePointLeft(3)); System.out.println("x » 3 = " + x.mpvePQintRight(3)); System.out.println("x * y = " + x.multiply(y)); System.out.println("-x = " + x.negate()); System.out.println("scale of x = " + x.scale());
System.out.println("increase scale of x to 20 = " + x.setScale(20)); System.out.println("decrease scale of x to 10 = " +
x.setScale(10, BigDecimal.ROUND_HALF__UP)) ; System.out.println("sign(x) = " + x.signum()); System.out.println("x - y = " + x.subtract(y)}; System.out.println("round x = " + x.toBiglnteger()); System.out.println("mantissa of x = " + x.unscaledValue()); System.out.println("mantissa of 0.1 =\n= " +
new BigDecimal(0.1).unscaledValue()); } }
5.3 "9 ) 9 4
5.3.1.
@ , @ , (
.
5.3.2? Java -
.
5.3.3? :
1.sin(x/n), tan(x/n)
: double, int. ; , i
f,
. . @ 15 .
2.cos(x/n),ctan(x/n)
: long, float. ; , i
68
f,
. . @ 15 .
5.3.4.+ .
5.3.5? .
5.4 !, #",
5.4.1; .
5.4.2.
5.4.3C .
5.4.4; .
5.4.5C , i ( @
.
5.4.6 ?, i
(5 g. ), ( ( @
.
5.5 7] , # 4 )
5.5.1‘ - ?
5.5.2E - ?
5.5.3. ?
5.5.4. Boolean?
5.5.5. Character?
5.5.6: Character.
5.5.7. BigInteger?
5.5.8: BigInteger.
5.5.9. BigDecimal?
5.5.10: BigDecimal.
69
6 ? 8 8 8 ? 6 8 ? 8^
6.1 ( 4
: Java.
6.2 " , ( ( 4-, ",9 ! ,
E ( + f
. G g, Java f'f. ? @ string
stringBuffer.
? 'f string @ -
( . D @f f
i ( ' , @ ( 'f, i
@ . E ( , i @ 'f
stringBuffer, ( , @ @ ,
@ @@ .
, ( string,
Java stringBuffer, f
(, string. :, i
f
String s = "•O" + " 267= " + "4562P";
f :
String s=new StringBuffer().append("•O").append(" 267= ").append("4562P").toString();
m 'f stringBuffer,
"D", " ", " ", 'f, i g,
StringBuffer String toString().
' @ . D (
.
: g -
string - :
String s1 = "•O 4562P.";
G i , (
, '@ f@ :
String s2 = "•O 62@\/> 4562P, " + "3=0/9=7= N 6@2S 456P=S @/S8672\2 :OP9:N";
70
> 'f ..' -
new. C string f
' :
–string() - @f 'f ( ;
–string(String str) – 'f @f g,
f ;
–string(StringBuffer str) – 'f
BufferString;
–string(byte[] byteArray) – 'f @f
byteArray;
–string(char[] charArray) – 'f @f
charArray Unicode;
–string(byte[] byteArray, int offset, int count) – 'f @f
byteArray, i f offset
count ;
–string(char[] charArray, int offset, int count) – (,
f Unicode;
–string(byte[] byteArray, String encoding) – ,
, @ Unicode- ,
encoding ;
–string(byte[] byteArray, int offset, int count, String encoding) – ( , .
C , c[] @f s1,
, >=866, @f s2. : g,
@f s3 - . ' 6.1. >
class StringTest{
public static void main(String[] args){
String winLikeWin = null, winLikeDOS = null, winLikeUNIX = null; String dosLikeWin = null, dosLikeDOS = null, dosLikeUNIX = null; String unixLikeWin = null, unixLikeDOS = null, unixLikeUNIX = null; String msg = null;
byte[] byteCp!251 = { (byte)0x0,(byte)0xEE,(byte)0xFl, (byte)0xFl,(byte)0xES,(byte)0xFF };
byte[] byteCp866 = { (byte)0x90,(byte)0xAE,(byte)0x1, (byte)0xEl,(byte)0x8,(byte)0xEF };
byte[] byteKOISR =( (byte)0x2,(byte)0xCF,(byte)0x3,