mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Enhance docs, remove main, add tests in AnytoAny (#5916)
This commit is contained in:
@ -1,35 +1,68 @@
|
||||
package com.thealgorithms.conversions;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// given a source number , source base, destination base, this code can give you the destination
|
||||
// number.
|
||||
// sn ,sb,db ---> ()dn . this is what we have to do .
|
||||
|
||||
/**
|
||||
* A utility class for converting numbers from any base to any other base.
|
||||
*
|
||||
* This class provides a method to convert a source number from a given base
|
||||
* to a destination number in another base. Valid bases range from 2 to 10.
|
||||
*/
|
||||
public final class AnytoAny {
|
||||
private AnytoAny() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scn = new Scanner(System.in);
|
||||
int sn = scn.nextInt();
|
||||
int sb = scn.nextInt();
|
||||
int db = scn.nextInt();
|
||||
int m = 1;
|
||||
int dec = 0;
|
||||
int dn = 0;
|
||||
while (sn != 0) {
|
||||
dec = dec + (sn % 10) * m;
|
||||
m *= sb;
|
||||
sn /= 10;
|
||||
/**
|
||||
* Converts a number from a source base to a destination base.
|
||||
*
|
||||
* @param sourceNumber The number in the source base (as an integer).
|
||||
* @param sourceBase The base of the source number (between 2 and 10).
|
||||
* @param destBase The base to which the number should be converted (between 2 and 10).
|
||||
* @throws IllegalArgumentException if the bases are not between 2 and 10.
|
||||
* @return The converted number in the destination base (as an integer).
|
||||
*/
|
||||
public static int convertBase(int sourceNumber, int sourceBase, int destBase) {
|
||||
if (sourceBase < 2 || sourceBase > 10 || destBase < 2 || destBase > 10) {
|
||||
throw new IllegalArgumentException("Bases must be between 2 and 10.");
|
||||
}
|
||||
m = 1;
|
||||
while (dec != 0) {
|
||||
dn = dn + (dec % db) * m;
|
||||
m *= 10;
|
||||
dec /= db;
|
||||
|
||||
int decimalValue = toDecimal(sourceNumber, sourceBase);
|
||||
return fromDecimal(decimalValue, destBase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a number from a given base to its decimal representation (base 10).
|
||||
*
|
||||
* @param number The number in the original base.
|
||||
* @param base The base of the given number.
|
||||
* @return The decimal representation of the number.
|
||||
*/
|
||||
private static int toDecimal(int number, int base) {
|
||||
int decimalValue = 0;
|
||||
int multiplier = 1;
|
||||
|
||||
while (number != 0) {
|
||||
decimalValue += (number % 10) * multiplier;
|
||||
multiplier *= base;
|
||||
number /= 10;
|
||||
}
|
||||
System.out.println(dn);
|
||||
scn.close();
|
||||
return decimalValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a decimal (base 10) number to a specified base.
|
||||
*
|
||||
* @param decimal The decimal number to convert.
|
||||
* @param base The destination base for conversion.
|
||||
* @return The number in the specified base.
|
||||
*/
|
||||
private static int fromDecimal(int decimal, int base) {
|
||||
int result = 0;
|
||||
int multiplier = 1;
|
||||
|
||||
while (decimal != 0) {
|
||||
result += (decimal % base) * multiplier;
|
||||
multiplier *= 10;
|
||||
decimal /= base;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user