Skip to content

Commit

Permalink
mouredev#14-Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
allbertoMD committed May 22, 2024
1 parent 51652ef commit 2b57447
Showing 1 changed file with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Foundation


var hexadecimalQuotient = 0
var hexadecimalRemainder = 0
var divisionHexadecimalNumbers: [String] = []
var hexadecimalNumbersArray: [String] = []
var hexadecimalNumber = "0x"

var stepper = 0

var octalQuotient = 0
var octalRemainder = 0
var divisionOctalNumbers: [String] = []
var octalNumbersArray: [String] = []
var octalNumber = "0o"



print("\nIntroduce el número que quieres convertir en hexadecimal:")
guard let input = readLine(), var decimalToHexadecimalNumber = Int(input) else {
fatalError()
}

repeat {
hexadecimalQuotient = decimalToHexadecimalNumber / 16
hexadecimalRemainder = decimalToHexadecimalNumber % 16
divisionHexadecimalNumbers.append(String(hexadecimalRemainder))
decimalToHexadecimalNumber = hexadecimalQuotient
} while hexadecimalQuotient != 0

hexadecimalNumbersArray.append(contentsOf: divisionHexadecimalNumbers)

for number in divisionHexadecimalNumbers {
if number == "10" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("A", at: stepper)
} else if number == "11" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("B", at: stepper)
}
else if number == "12" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("C", at: stepper)
}
else if number == "13" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("D", at: stepper)
}
else if number == "14" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("D", at: stepper)
}
else if number == "15" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("E", at: stepper)
}
else if number == "16" {
hexadecimalNumbersArray.remove(at: stepper)
hexadecimalNumbersArray.insert("F", at: stepper)
}

stepper += 1
}

hexadecimalNumbersArray.reverse()

for item in hexadecimalNumbersArray {
hexadecimalNumber.append(item)
}

print(hexadecimalNumber)



print("\nIntroduce el número que quieres convertir en octal:")
guard let input = readLine(), var decimalToOctalNumber = Int(input) else {
fatalError()
}

repeat {
octalQuotient = decimalToOctalNumber / 8
octalRemainder = decimalToOctalNumber % 8
octalNumbersArray.append(String(octalRemainder))
decimalToOctalNumber = octalQuotient
} while octalQuotient != 0

octalNumbersArray.reverse()

for item in octalNumbersArray {
octalNumber.append(item)
}

print(octalNumber)

0 comments on commit 2b57447

Please sign in to comment.