From 5c954b63b1bdac41d8ba8268f235afa6794bea9c Mon Sep 17 00:00:00 2001 From: array-in-a-matrix Date: Tue, 27 Feb 2024 14:01:33 -0500 Subject: [PATCH] accept fractions --- src/procedures.nim | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/procedures.nim b/src/procedures.nim index fc00334..c731fa7 100644 --- a/src/procedures.nim +++ b/src/procedures.nim @@ -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) \ No newline at end of file