forked from mouredev/retos-programacion-2023
-
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
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/go/qwik-zgheib.go
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type NumberConverter interface { | ||
ToOctal(decimal int) string | ||
ToHexadecimal(decimal int) string | ||
} | ||
|
||
type CustomNumberConverter struct{} | ||
|
||
func NewCustomNumberConverter() *CustomNumberConverter { | ||
return &CustomNumberConverter{} | ||
} | ||
|
||
func (c *CustomNumberConverter) ToOctal(decimal int) string { | ||
if decimal == 0 { | ||
return "0" | ||
} | ||
|
||
isNegative := false | ||
if decimal < 0 { | ||
isNegative = true | ||
decimal = -decimal | ||
} | ||
|
||
octal := "" | ||
for decimal > 0 { | ||
remainder := decimal % 8 | ||
octal = fmt.Sprintf("%d%s", remainder, octal) | ||
decimal = decimal / 8 | ||
} | ||
|
||
if isNegative { | ||
octal = "-" + octal | ||
} | ||
|
||
return octal | ||
} | ||
|
||
func (c *CustomNumberConverter) ToHexadecimal(decimal int) string { | ||
if decimal == 0 { | ||
return "0" | ||
} | ||
|
||
isNegative := false | ||
if decimal < 0 { | ||
isNegative = true | ||
decimal = -decimal | ||
} | ||
|
||
hexChars := "0123456789ABCDEF" | ||
hexadecimal := "" | ||
for decimal > 0 { | ||
remainder := decimal % 16 | ||
hexadecimal = fmt.Sprintf("%c%s", hexChars[remainder], hexadecimal) | ||
decimal = decimal / 16 | ||
} | ||
|
||
if isNegative { | ||
hexadecimal = "-" + hexadecimal | ||
} | ||
|
||
return hexadecimal | ||
} | ||
|
||
func readInput(prompt string) string { | ||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Print(prompt) | ||
input, _ := reader.ReadString('\n') | ||
return strings.TrimSpace(input) | ||
} | ||
|
||
func getValidNumber() int { | ||
for { | ||
input := readInput("Enter a valid decimal number: ") | ||
number, err := strconv.Atoi(input) | ||
if err == nil { | ||
return number | ||
} | ||
fmt.Println("Invalid input. Please enter a valid decimal number.") | ||
} | ||
} | ||
|
||
func main() { | ||
converter := NewCustomNumberConverter() | ||
|
||
decimalNumber := getValidNumber() | ||
fmt.Printf("Decimal: %d\n", decimalNumber) | ||
fmt.Printf("Octal: %s\n", converter.ToOctal(decimalNumber)) | ||
fmt.Printf("Hexadecimal: %s\n", converter.ToHexadecimal(decimalNumber)) | ||
} |