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
1 parent
51652ef
commit 2b57447
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
Retos/Reto #14 - OCTAL Y HEXADECIMAL [Fácil]/swift/allbertoMD.swift
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,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) | ||
|