This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
generated from microsoft/vscode-remote-try-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
57 deletions.
There are no files selected for viewing
120 changes: 63 additions & 57 deletions
120
src/main/java/com/utp/clsEstructuraDatos/pry2/app/RepNumericas.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,86 @@ | ||
package com.utp.clsEstructuraDatos.pry2.app; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Optional; | ||
|
||
import com.utp.utils.Cli; | ||
import com.utp.utils.io_vavrs.Validation; | ||
import com.utp.utils.Result; | ||
|
||
public class RepNumericas { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("Hello World!"); | ||
System.out.println("Representación Numéricas - Binario a Hexadecimal y Decimal\n"); | ||
(new RepNumericas()).spawn_cli(); | ||
} | ||
|
||
void spawn_cli() { | ||
// Enter data using BufferReader | ||
while (true) { | ||
var input = Cli.read_non_empty_input("Ingrese un binario"); | ||
var maybe_binario = BinaryString.from_str(input); | ||
if (maybe_binario.isInvalid()) { | ||
System.out.println(maybe_binario.getError()); | ||
continue; | ||
} | ||
input = maybe_binario.get().toString(); | ||
System.out.printf("Entrada: %s\n", input); | ||
System.out.println("Hexadecimal: 0x" + maybe_binario.get().to_hex_string()); | ||
System.out.printf("%s => Entero: ", input); | ||
Utils.pretty_print_int(maybe_binario.get().to_decimal()); | ||
System.out.println("\n"); | ||
} | ||
BufferedRead | ||
} | ||
} | ||
|
||
class Utils { | ||
static void pretty_print_int(int num) { | ||
com.utp.clsEstructuraDatos.pry1.app.Utils.pretty_print_int(num); | ||
} | ||
} | ||
record RepresentacionNumerica(BigInteger innerBigInt, RepNumerica type) { | ||
|
||
class BinaryString { | ||
public static Validation<String, BinaryString> from_str(String input) { | ||
// Strip leading zeroes because they don't affect the return value size | ||
input = input.replaceFirst("^[0]*", ""); | ||
if (input.length() > BITS_SIZE) { | ||
return Validation.invalid( | ||
String.format("`%s` is `%d` chars longer than the set character limit of `%d` chars", input, | ||
input.length() - BITS_SIZE, BITS_SIZE)); | ||
public static Result<RepresentacionNumerica, String> from_parts(String innerString, String type) { | ||
Optional<RepNumerica> maybe_rep = RepNumerica.from_str(type); | ||
if (maybe_rep.isEmpty()) { | ||
return Result.error(String.format("`%s` is not a valid representation type", type)); | ||
} | ||
if (!input.matches("[01]+")) { | ||
return Validation.invalid(String.format("`%s` is not a valid binary string", input)); | ||
var representacion = maybe_rep.get(); | ||
Result<BigInteger, String> maybe_BigInt = check_string(representacion, innerString); | ||
if (maybe_BigInt.isError()) { | ||
return Result.error(maybe_BigInt.unwrapError()); | ||
} | ||
return Validation.valid(new BinaryString(input)); | ||
return Result.ok(new RepresentacionNumerica(maybe_BigInt.unwrapOk(), representacion)); | ||
} | ||
|
||
public Integer to_decimal() { | ||
if (cached_int.isPresent()) { | ||
return cached_int.get(); | ||
// Converts, if possible, the string to a BigInteger representation of it. | ||
// otherwise it returns an error message | ||
public static Result<BigInteger, String> check_string(RepNumerica type, String innerString) { | ||
// Trims leading zeroes because they don't affect the return value | ||
// numeric representation | ||
innerString = innerString.replaceFirst("^[0]*", "").trim().toUpperCase(); | ||
int radix = 0; | ||
switch (type) { | ||
case BINARIO -> { | ||
if (!innerString.matches("[01]+")) { | ||
return Result.error("Binario solo puede contener 0s y 1s"); | ||
} | ||
radix = 2; | ||
} | ||
case ENTERO -> { | ||
if (!innerString.matches("[0-9]+")) { | ||
return Result.error("Entero solo puede contener dígitos"); | ||
} | ||
radix = 10; | ||
} | ||
case HEXADECIMAL -> { | ||
if (!innerString.matches("[0-9A-F]+")) { | ||
return Result.error("Hexadecimal solo puede contener dígitos y letras de la A a la F"); | ||
} | ||
radix = 16; | ||
} | ||
} | ||
cached_int = Optional.of(Integer.parseInt(inner, 2)); | ||
return cached_int.get(); | ||
return Result.ok(new BigInteger(innerString, radix)); | ||
} | ||
|
||
public String to_hex_string() { | ||
return Integer.toHexString(to_decimal()); | ||
public String to_string() { | ||
return switch (type) { | ||
case BINARIO -> innerBigInt.toString(2); | ||
case ENTERO -> innerBigInt.toString(10); | ||
case HEXADECIMAL -> innerBigInt.toString(16); | ||
}; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return inner; | ||
} | ||
} | ||
|
||
private BinaryString(String inner) { | ||
this.inner = inner; | ||
} | ||
enum RepNumerica { | ||
BINARIO, ENTERO, HEXADECIMAL; | ||
|
||
final static int BITS_SIZE = 31; | ||
String inner = null; | ||
Optional<Integer> cached_int = Optional.empty(); | ||
} | ||
public static Optional<RepNumerica> from_str(String input) { | ||
input = input.trim().toLowerCase(); | ||
switch (input) { | ||
case "binario": | ||
return Optional.of(BINARIO); | ||
case "entero": | ||
return Optional.of(ENTERO); | ||
case "hexadecimal": | ||
return Optional.of(HEXADECIMAL); | ||
default: | ||
return Optional.empty(); | ||
} | ||
} | ||
} |