Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5d59a2e

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
Formatted with Google Java Formatter
1 parenta23bac9 commit5d59a2e

File tree

219 files changed

+13754
-14578
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+13754
-14578
lines changed

‎Conversions/AnyBaseToAnyBase.java‎

Lines changed: 102 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -6,130 +6,122 @@
66
importjava.util.Scanner;
77

88
/**
9-
* Class for converting from "any" base to "any" other base, when "any" means from 2-36.
10-
*Works bygoing from base 1 to decimal to base 2. Includes auxiliary method for
11-
*determining whether anumber is valid for a given base.
9+
* Class for converting from "any" base to "any" other base, when "any" means from 2-36. Works by
10+
* going from base 1 to decimal to base 2. Includes auxiliary method for determining whether a
11+
* number is valid for a given base.
1212
*
1313
* @author Michael Rolland
1414
* @version 2017.10.10
1515
*/
1616
publicclassAnyBaseToAnyBase {
1717

18-
/**
19-
* Smallest and largest base you want to accept as valid input
20-
*/
21-
staticfinalintMINIMUM_BASE =2;
22-
staticfinalintMAXIMUM_BASE =36;
18+
/** Smallest and largest base you want to accept as valid input */
19+
staticfinalintMINIMUM_BASE =2;
2320

24-
publicstaticvoidmain(String[]args) {
25-
Scannerin =newScanner(System.in);
26-
Stringn;
27-
intb1,b2;
28-
while (true) {
29-
try {
30-
System.out.print("Enter number: ");
31-
n =in.next();
32-
System.out.print("Enter beginning base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): ");
33-
b1 =in.nextInt();
34-
if (b1 >MAXIMUM_BASE ||b1 <MINIMUM_BASE) {
35-
System.out.println("Invalid base!");
36-
continue;
37-
}
38-
if (!validForBase(n,b1)) {
39-
System.out.println("The number is invalid for this base!");
40-
continue;
41-
}
42-
System.out.print("Enter end base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): ");
43-
b2 =in.nextInt();
44-
if (b2 >MAXIMUM_BASE ||b2 <MINIMUM_BASE) {
45-
System.out.println("Invalid base!");
46-
continue;
47-
}
48-
break;
49-
}catch (InputMismatchExceptione) {
50-
System.out.println("Invalid input.");
51-
in.next();
52-
}
21+
staticfinalintMAXIMUM_BASE =36;
22+
23+
publicstaticvoidmain(String[]args) {
24+
Scannerin =newScanner(System.in);
25+
Stringn;
26+
intb1,b2;
27+
while (true) {
28+
try {
29+
System.out.print("Enter number: ");
30+
n =in.next();
31+
System.out.print(
32+
"Enter beginning base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): ");
33+
b1 =in.nextInt();
34+
if (b1 >MAXIMUM_BASE ||b1 <MINIMUM_BASE) {
35+
System.out.println("Invalid base!");
36+
continue;
37+
}
38+
if (!validForBase(n,b1)) {
39+
System.out.println("The number is invalid for this base!");
40+
continue;
5341
}
54-
System.out.println(base2base(n,b1,b2));
55-
in.close();
42+
System.out.print(
43+
"Enter end base (between " +MINIMUM_BASE +" and " +MAXIMUM_BASE +"): ");
44+
b2 =in.nextInt();
45+
if (b2 >MAXIMUM_BASE ||b2 <MINIMUM_BASE) {
46+
System.out.println("Invalid base!");
47+
continue;
48+
}
49+
break;
50+
}catch (InputMismatchExceptione) {
51+
System.out.println("Invalid input.");
52+
in.next();
53+
}
5654
}
55+
System.out.println(base2base(n,b1,b2));
56+
in.close();
57+
}
5758

58-
/**
59-
* Checks if a number (as a String) is valid for a given base.
60-
*/
61-
publicstaticbooleanvalidForBase(Stringn,intbase) {
62-
char[]validDigits = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E',
63-
'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V',
64-
'W','X','Y','Z'};
65-
// digitsForBase contains all the valid digits for the base given
66-
char[]digitsForBase =Arrays.copyOfRange(validDigits,0,base);
59+
/** Checks if a number (as a String) is valid for a given base. */
60+
publicstaticbooleanvalidForBase(Stringn,intbase) {
61+
char[]validDigits = {
62+
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I',
63+
'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
64+
};
65+
// digitsForBase contains all the valid digits for the base given
66+
char[]digitsForBase =Arrays.copyOfRange(validDigits,0,base);
6767

68-
// Convert character array into set for convenience of contains() method
69-
HashSet<Character>digitsList =newHashSet<>();
70-
for (inti =0;i <digitsForBase.length;i++)
71-
digitsList.add(digitsForBase[i]);
68+
// Convert character array into set for convenience of contains() method
69+
HashSet<Character>digitsList =newHashSet<>();
70+
for (inti =0;i <digitsForBase.length;i++)digitsList.add(digitsForBase[i]);
7271

73-
// Check that every digit in n is within the list of valid digits for that base.
74-
for (charc :n.toCharArray())
75-
if (!digitsList.contains(c))
76-
returnfalse;
72+
// Check that every digit in n is within the list of valid digits for that base.
73+
for (charc :n.toCharArray())if (!digitsList.contains(c))returnfalse;
7774

78-
returntrue;
79-
}
75+
returntrue;
76+
}
8077

81-
/**
82-
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
83-
* then decimal to b2.
84-
*
85-
* @param n The integer to be converted.
86-
* @param b1 Beginning base.
87-
* @param b2 End base.
88-
* @return n in base b2.
89-
*/
90-
publicstaticStringbase2base(Stringn,intb1,intb2) {
91-
// Declare variables: decimal value of n,
92-
// character of base b1, character of base b2,
93-
// and the string that will be returned.
94-
intdecimalValue =0,charB2;
95-
charcharB1;
96-
Stringoutput ="";
97-
// Go through every character of n
98-
for (inti =0;i <n.length();i++) {
99-
// store the character in charB1
100-
charB1 =n.charAt(i);
101-
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
102-
if (charB1 >='A' &&charB1 <='Z')
103-
charB2 =10 + (charB1 -'A');
104-
// Else, store the integer value in charB2
105-
else
106-
charB2 =charB1 -'0';
107-
// Convert the digit to decimal and add it to the
108-
// decimalValue of n
109-
decimalValue =decimalValue *b1 +charB2;
110-
}
78+
/**
79+
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
80+
* then decimal to b2.
81+
*
82+
* @param n The integer to be converted.
83+
* @param b1 Beginning base.
84+
* @param b2 End base.
85+
* @return n in base b2.
86+
*/
87+
publicstaticStringbase2base(Stringn,intb1,intb2) {
88+
// Declare variables: decimal value of n,
89+
// character of base b1, character of base b2,
90+
// and the string that will be returned.
91+
intdecimalValue =0,charB2;
92+
charcharB1;
93+
Stringoutput ="";
94+
// Go through every character of n
95+
for (inti =0;i <n.length();i++) {
96+
// store the character in charB1
97+
charB1 =n.charAt(i);
98+
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
99+
if (charB1 >='A' &&charB1 <='Z')charB2 =10 + (charB1 -'A');
100+
// Else, store the integer value in charB2
101+
elsecharB2 =charB1 -'0';
102+
// Convert the digit to decimal and add it to the
103+
// decimalValue of n
104+
decimalValue =decimalValue *b1 +charB2;
105+
}
111106

112-
// Converting the decimal value to base b2:
113-
// A number is converted from decimal to another base
114-
// by continuously dividing by the base and recording
115-
// the remainder until the quotient is zero. The number in the
116-
// new base is the remainders, with the last remainder
117-
// being the left-most digit.
118-
if (0 ==decimalValue)
119-
return"0";
120-
// While the quotient is NOT zero:
121-
while (decimalValue !=0) {
122-
// If the remainder is a digit < 10, simply add it to
123-
// the left side of the new number.
124-
if (decimalValue %b2 <10)
125-
output =Integer.toString(decimalValue %b2) +output;
126-
// If the remainder is >= 10, add a character with the
127-
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
128-
else
129-
output = (char) ((decimalValue %b2) +55) +output;
130-
// Divide by the new base again
131-
decimalValue /=b2;
132-
}
133-
returnoutput;
107+
// Converting the decimal value to base b2:
108+
// A number is converted from decimal to another base
109+
// by continuously dividing by the base and recording
110+
// the remainder until the quotient is zero. The number in the
111+
// new base is the remainders, with the last remainder
112+
// being the left-most digit.
113+
if (0 ==decimalValue)return"0";
114+
// While the quotient is NOT zero:
115+
while (decimalValue !=0) {
116+
// If the remainder is a digit < 10, simply add it to
117+
// the left side of the new number.
118+
if (decimalValue %b2 <10)output =Integer.toString(decimalValue %b2) +output;
119+
// If the remainder is >= 10, add a character with the
120+
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
121+
elseoutput = (char) ((decimalValue %b2) +55) +output;
122+
// Divide by the new base again
123+
decimalValue /=b2;
134124
}
125+
returnoutput;
126+
}
135127
}

‎Conversions/AnyBaseToDecimal.java‎

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
packageConversions;
22

3-
/**
4-
* @author Varun Upadhyay (https://github.com/varunu28)
5-
*/
3+
/** @author Varun Upadhyay (https://github.com/varunu28) */
64

75
// Driver program
86
publicclassAnyBaseToDecimal {
9-
publicstaticvoidmain(String[]args) {
10-
assertconvertToDecimal("1010",2) ==Integer.valueOf("1010",2);
11-
assertconvertToDecimal("777",8) ==Integer.valueOf("777",8);
12-
assertconvertToDecimal("999",10) ==Integer.valueOf("999",10);
13-
assertconvertToDecimal("ABCDEF",16) ==Integer.valueOf("ABCDEF",16);
14-
assertconvertToDecimal("XYZ",36) ==Integer.valueOf("XYZ",36);
15-
}
7+
publicstaticvoidmain(String[]args) {
8+
assertconvertToDecimal("1010",2) ==Integer.valueOf("1010",2);
9+
assertconvertToDecimal("777",8) ==Integer.valueOf("777",8);
10+
assertconvertToDecimal("999",10) ==Integer.valueOf("999",10);
11+
assertconvertToDecimal("ABCDEF",16) ==Integer.valueOf("ABCDEF",16);
12+
assertconvertToDecimal("XYZ",36) ==Integer.valueOf("XYZ",36);
13+
}
1614

17-
/**
18-
* Convert any radix to decimal number
19-
*
20-
* @param s the string to be convert
21-
* @param radix the radix
22-
* @return decimal of bits
23-
* @throws NumberFormatException if {@code bits} or {@code radix} is invalid
24-
*/
25-
publicstaticintconvertToDecimal(Strings,intradix) {
26-
intnum =0;
27-
intpow =1;
15+
/**
16+
* Convert any radix to decimal number
17+
*
18+
* @param s the string to be convert
19+
* @param radix the radix
20+
* @return decimal of bits
21+
* @throws NumberFormatException if {@code bits} or {@code radix} is invalid
22+
*/
23+
publicstaticintconvertToDecimal(Strings,intradix) {
24+
intnum =0;
25+
intpow =1;
2826

29-
for (inti =s.length() -1;i >=0;i--) {
30-
intdigit =valOfChar(s.charAt(i));
31-
if (digit >=radix) {
32-
thrownewNumberFormatException("For input string " +s);
33-
}
34-
num +=valOfChar(s.charAt(i)) *pow;
35-
pow *=radix;
36-
}
37-
returnnum;
27+
for (inti =s.length() -1;i >=0;i--) {
28+
intdigit =valOfChar(s.charAt(i));
29+
if (digit >=radix) {
30+
thrownewNumberFormatException("For input string " +s);
31+
}
32+
num +=valOfChar(s.charAt(i)) *pow;
33+
pow *=radix;
3834
}
35+
returnnum;
36+
}
3937

40-
/**
41-
* Convert character to integer
42-
*
43-
* @param c the character
44-
* @return represented digit of given character
45-
* @throws NumberFormatException if {@code ch} is not UpperCase or Digit character.
46-
*/
47-
publicstaticintvalOfChar(charc) {
48-
if (!(Character.isUpperCase(c) ||Character.isDigit(c))) {
49-
thrownewNumberFormatException("invalid character :" +c);
50-
}
51-
returnCharacter.isDigit(c) ?c -'0' :c -'A' +10;
38+
/**
39+
* Convert character to integer
40+
*
41+
* @param c the character
42+
* @return represented digit of given character
43+
* @throws NumberFormatException if {@code ch} is not UpperCase or Digit character.
44+
*/
45+
publicstaticintvalOfChar(charc) {
46+
if (!(Character.isUpperCase(c) ||Character.isDigit(c))) {
47+
thrownewNumberFormatException("invalid character :" +c);
5248
}
49+
returnCharacter.isDigit(c) ?c -'0' :c -'A' +10;
50+
}
5351
}

‎Conversions/AnytoAny.java‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
packageConversions;
22

33
importjava.util.Scanner;
4-
//given a source number , source base, destination base, this code can give you the destination number.
5-
//sn ,sb,db ---> ()dn . this is what we have to do .
4+
// given a source number , source base, destination base, this code can give you the destination
5+
// number.
6+
// sn ,sb,db ---> ()dn . this is what we have to do .
67

78
publicclassAnytoAny {
89

9-
publicstaticvoidmain(String[]args) {
10-
Scannerscn =newScanner(System.in);
11-
intsn =scn.nextInt();
12-
intsb =scn.nextInt();
13-
intdb =scn.nextInt();
14-
intm =1,dec =0,dn =0;
15-
while (sn !=0) {
16-
dec =dec + (sn %10) *m;
17-
m *=sb;
18-
sn /=10;
19-
}
20-
m =1;
21-
while (dec !=0) {
22-
dn =dn + (dec %db) *m;
23-
m *=10;
24-
dec /=db;
25-
}
26-
System.out.println(dn);
27-
scn.close();
10+
publicstaticvoidmain(String[]args) {
11+
Scannerscn =newScanner(System.in);
12+
intsn =scn.nextInt();
13+
intsb =scn.nextInt();
14+
intdb =scn.nextInt();
15+
intm =1,dec =0,dn =0;
16+
while (sn !=0) {
17+
dec =dec + (sn %10) *m;
18+
m *=sb;
19+
sn /=10;
2820
}
29-
21+
m =1;
22+
while (dec !=0) {
23+
dn =dn + (dec %db) *m;
24+
m *=10;
25+
dec /=db;
26+
}
27+
System.out.println(dn);
28+
scn.close();
29+
}
3030
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp