accept fractions

This commit is contained in:
array-in-a-matrix 2024-02-27 14:01:33 -05:00
parent 8ebea26bdd
commit 5c954b63b1
1 changed files with 13 additions and 3 deletions

View File

@ -1,4 +1,4 @@
import strutils, sequtils, terminal
import strutils, sequtils, terminal, re
#? validate if user input is of correct type
proc getInt: int =
@ -12,7 +12,15 @@ proc getInt: int =
proc getFloat: float =
while(true):
try:
return parseFloat(readline(stdin))
let number: string = readline(stdin).replace(" ","").replace("\t", "")
#? if the number given is a fraction convert it into decimal
if match(number, re"[0-9]+/[0-9]+"):
let
numerator: float = number.split('/')[0].parseFloat
denominator: float = number.split('/')[1].parseFloat
return numerator / denominator
return parseFloat(number)
except:
styledEcho resetStyle, "Please enter a ", styleBright, "number, " , resetStyle, "try again."
@ -167,5 +175,7 @@ proc scalar* =
let M = calcScalar(f, m)
echo "\nResult matrix is:"
echo "\nInitial matrix is:"
printMatrix(m)
echo "\nScaled matrix is:"
printMatrix(M)