FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/DataStructures/Stacks/DecimalToAnyUsingStack.java at master · charleyr0/Java · GitHub
charleyr0
Java
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
DataStructures
/
Stacks
/
DecimalToAnyUsingStack.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (40 loc) · 1.36 KB
Breadcrumbs
Java
/
DataStructures
/
Stacks
/
DecimalToAnyUsingStack.java
Copy path
File metadata and controls
44 lines (40 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package
DataStructures
.
Stacks
;
import
java
.
util
.
Stack
;
public
class
DecimalToAnyUsingStack
{
public
static
void
main
(
String
[]
args
) {
assert
convert
(
0
,
2
).
equals
(
"0"
);
assert
convert
(
30
,
2
).
equals
(
"11110"
);
assert
convert
(
30
,
8
).
equals
(
"36"
);
assert
convert
(
30
,
10
).
equals
(
"30"
);
assert
convert
(
30
,
16
).
equals
(
"1E"
);
}
/**
* Convert decimal number to another radix
*
* @param number the number to be converted
* @param radix the radix
* @return another radix
* @throws ArithmeticException if <tt>number</tt> or <tt>radius</tt> is invalid
*/
private
static
String
convert
(
int
number
,
int
radix
) {
if
(
radix
<
2
||
radix
>
16
) {
throw
new
ArithmeticException
(
String
.
format
(
"Invalid input -> number:%d,radius:%d"
,
number
,
radix
));
}
char
[]
tables
= {
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
};
Stack
<
Character
>
bits
=
new
Stack
<>();
do
{
bits
.
push
(
tables
[
number
%
radix
]);
number
=
number
/
radix
;
}
while
(
number
!=
0
);
StringBuilder
result
=
new
StringBuilder
();
while
(!
bits
.
isEmpty
()) {
result
.
append
(
bits
.
pop
());
}
return
result
.
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL