Skip to content

Commit

Permalink
feat: reto mouredev#14 - go >> [E]
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-empty committed Jun 6, 2024
1 parent 5218af6 commit 48555b1
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/go/qwik-zgheib.go
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))
}

0 comments on commit 48555b1

Please sign in to comment.