Fix regular expression.
This commit is contained in:
parent
6bef44a9c4
commit
5f8f327c01
2 changed files with 41 additions and 14 deletions
|
@ -23,6 +23,7 @@ dependencies:
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
- regex-pcre
|
- regex-pcre
|
||||||
- lens
|
- lens
|
||||||
|
- split
|
||||||
|
|
||||||
library:
|
library:
|
||||||
source-dirs: src
|
source-dirs: src
|
||||||
|
|
|
@ -8,6 +8,9 @@ import Control.Arrow ((>>>))
|
||||||
import Text.Regex.PCRE ((=~))
|
import Text.Regex.PCRE ((=~))
|
||||||
import qualified Text.Regex.PCRE as PCRE
|
import qualified Text.Regex.PCRE as PCRE
|
||||||
import Control.Lens.Operators
|
import Control.Lens.Operators
|
||||||
|
import qualified Data.List as List
|
||||||
|
import qualified Data.List.Split as Split
|
||||||
|
import qualified Debug.Trace as Trace
|
||||||
|
|
||||||
if' :: Bool -> a -> a -> a
|
if' :: Bool -> a -> a -> a
|
||||||
if' True x _ = x
|
if' True x _ = x
|
||||||
|
@ -21,7 +24,8 @@ prompt loopCondition text = line >>= breakorcontinue
|
||||||
(SIO.stdout & SIO.hFlush) >>
|
(SIO.stdout & SIO.hFlush) >>
|
||||||
getLine
|
getLine
|
||||||
|
|
||||||
complexNumber = "^\\s*\\+?(-?\\s*[0-9\\.]+)?\\s*(\\+?((-\\s*)?[0-9\\.]*)i)?\\s*$"
|
complexNumber = "^\\s*(()()([\\-\\+])?\\s*(([0-9\\.]*)i)|([\\-\\+])?\\s*([0-9\\.]+)()(())|([\\-\\+])?\\s*([0-9\\.]+)\\s*([\\-\\+])\\s*(([0-9\\.]*)i))\\s*$"
|
||||||
|
|
||||||
|
|
||||||
data ComplexNumber = ComplexNumber {
|
data ComplexNumber = ComplexNumber {
|
||||||
complexNumberReal :: Float,
|
complexNumberReal :: Float,
|
||||||
|
@ -34,32 +38,54 @@ instance Show ComplexNumber where
|
||||||
stringToComplex :: String -> ComplexNumber
|
stringToComplex :: String -> ComplexNumber
|
||||||
stringToComplex input = ComplexNumber realValue imaginaryValue
|
stringToComplex input = ComplexNumber realValue imaginaryValue
|
||||||
where
|
where
|
||||||
realValue = if' (null realDigits) 0 (realDigits & read)
|
realValue = if' (null realDigits) 0 (read realDigits * handleSign realSign)
|
||||||
imaginaryValue =
|
imaginaryValue = handleImaginary (handleSign imaginarySign) imaginaryPart imaginaryDigits
|
||||||
if' (null imaginaryDigits)
|
handleSign "+" = 1 :: Float
|
||||||
(if' (null imaginaryPart)
|
handleSign "-" = -1
|
||||||
0
|
handleSign "" = 1
|
||||||
1
|
handleImaginary sign part digits
|
||||||
)
|
| null part = 0
|
||||||
(imaginaryDigits & read)
|
| null digits = 1 * sign
|
||||||
_:realDigits:imaginaryPart:imaginaryDigits:_ = match
|
| otherwise = read digits * sign
|
||||||
|
realSign:realDigits:imaginarySign:imaginaryPart:imaginaryDigits:_ = match & drop 2 & Split.chunksOf 5 & dropWhile(\a -> (a & filter(=="") & length) == 5) & head
|
||||||
match:_ = input =~ complexNumber :: [[String]]
|
match:_ = input =~ complexNumber :: [[String]]
|
||||||
|
|
||||||
addComplex :: ComplexNumber -> ComplexNumber -> ComplexNumber
|
addComplex :: ComplexNumber -> ComplexNumber -> ComplexNumber
|
||||||
addComplex a b = ComplexNumber (complexNumberReal a + complexNumberReal b) (complexNumberImaginary a + complexNumberImaginary b)
|
addComplex a b = ComplexNumber (complexNumberReal a + complexNumberReal b) (complexNumberImaginary a + complexNumberImaginary b)
|
||||||
|
|
||||||
multiplyComplex :: ComplexNumber -> ComplexNumber -> ComplexNumber
|
multiplyComplex :: ComplexNumber -> ComplexNumber -> ComplexNumber
|
||||||
multiplyComplex a b = ComplexNumber (complexNumberReal a * complexNumberReal b - complexNumberImaginary a * complexNumberImaginary b) (complexNumberReal a * complexNumberImaginary b + complexNumberReal b * complexNumberImaginary a)
|
multiplyComplex a b = ComplexNumber
|
||||||
|
(complexNumberReal a * complexNumberReal b - complexNumberImaginary a * complexNumberImaginary b)
|
||||||
|
(complexNumberReal a * complexNumberImaginary b + complexNumberReal b * complexNumberImaginary a)
|
||||||
|
|
||||||
|
divideComplex :: ComplexNumber -> ComplexNumber -> ComplexNumber
|
||||||
|
divideComplex num dem = getdem & getfinal
|
||||||
|
where
|
||||||
|
getfinal newdem = ComplexNumber
|
||||||
|
(
|
||||||
|
(complexNumberReal num * complexNumberReal dem + complexNumberImaginary num * complexNumberImaginary dem)
|
||||||
|
/ newdem
|
||||||
|
)
|
||||||
|
(
|
||||||
|
(complexNumberReal dem * complexNumberImaginary num - complexNumberReal num * complexNumberImaginary dem)
|
||||||
|
/ newdem
|
||||||
|
)
|
||||||
|
getdem = complexNumberReal dem ** 2 + complexNumberImaginary dem ** 2
|
||||||
|
|
||||||
someFunc :: IO ()
|
someFunc :: IO ()
|
||||||
someFunc = showResults <$> firstNumber <*> secondNumber >>= putStrLn
|
someFunc = showResults <$> firstNumber <*> secondNumber >>= putStrLn
|
||||||
where
|
where
|
||||||
showResults f s =
|
showResults f s =
|
||||||
[
|
[
|
||||||
|
"Input: ",
|
||||||
|
f & show,
|
||||||
|
s & show,
|
||||||
"Added: ",
|
"Added: ",
|
||||||
addComplex f s & show,
|
addComplex f s & show,
|
||||||
"\nMultiplied: ",
|
"Multiplied: ",
|
||||||
multiplyComplex f s & show
|
multiplyComplex f s & show,
|
||||||
] & concat
|
"Divided: ",
|
||||||
|
divideComplex f s & show
|
||||||
|
] & List.intercalate "\n"
|
||||||
firstNumber = ("Enter a complex number in the form of a + bi: " & prompt(=~ complexNumber)) <&> stringToComplex
|
firstNumber = ("Enter a complex number in the form of a + bi: " & prompt(=~ complexNumber)) <&> stringToComplex
|
||||||
secondNumber = ("Enter a second complex number in the form of a + bi: " & prompt(=~ complexNumber)) <&> stringToComplex
|
secondNumber = ("Enter a second complex number in the form of a + bi: " & prompt(=~ complexNumber)) <&> stringToComplex
|
||||||
|
|
Loading…
Add table
Reference in a new issue