Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - Ввод полинома
- inputPoly :: String -> Bool -> Int -> Int -> Int -> [(Int,Int)]
- inputPoly [] _ s c p = [(s*c,p)]
- inputPoly (x:xs) f s c p | f && (x == 'x') = inputPoly xs True s (if c == 0 then 1 else c) 1
- | x == '-' = (c * s, p) : (inputPoly xs True (-1) 0 0)
- | x == '+' = (c * s, p) : (inputPoly xs True 1 0 0)
- | f && (x == '^') = inputPoly xs False s c 0
- | f = inputPoly xs f s (c * 10 + (read [x] :: Int)) p
- | otherwise = inputPoly xs f s c (p * 10 + (read [x] :: Int))
- -- Упрощение
- redex :: [(Int, Int)] -> [(Int, Int)]
- redex x = filter ((/= 0) . fst) rlist
- where plist = sort $ nub $ map snd x
- rlist = map (\ n -> (sum (map fst (filter ((== n) . snd) x) ) , n) ) plist
- -- Вывод полинома
- showPoly :: [(Int, Int)] -> String
- showPoly [] = ""
- showPoly ((c, p):xs) | p == 0 = show(c) ++ showPoly xs
- | (c == 1) && (p == 1) = "+x" ++ showPoly xs
- | (c == -1) && (p == 1) = "-x" ++ showPoly xs
- | (c == 1) && (p /= 1) = "+x^" ++ show(p) ++ showPoly xs
- | (c == -1) && (p /= 1) = "-x^" ++ show(p) ++ showPoly xs
- | p /= 1 = (if c >0 then "+" else "") ++ show(c)++"x^" ++ show(p) ++ showPoly xs
- | otherwise = (if c >0 then "+" else "") ++ show(c)++"x" ++ showPoly xs
- -- Производная
- derivPoly :: [(Int, Int)] -> [(Int, Int)]
- derivPoly = redex . map (\ (c, p) -> (p * c, p-1))
- -- Сложение полиномов
- addPoly :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
- addPoly x y = redex $ x ++ y
- -- Умножение полиномов
- multPoly :: [(Int, Int)] -> [(Int, Int)] -> [(Int, Int)]
- multPoly x y = redex $ foldl (\ acc c -> map (\ yy -> ( (fst yy) * (fst c), (snd yy)+ (snd c) ) ) y ++ acc) [] x
- -- Умножение с вводом в естественном виде
- multInput :: String -> String -> String
- multInput x y = showPoly $ multPoly (inputPoly x True 1 0 0) (inputPoly y True 1 0 0)
- -- Сложение с вводом в естественном виде
- addInput :: String -> String -> String
- addInput x y = showPoly $ addPoly (inputPoly x True 1 0 0) (inputPoly y True 1 0 0)
- -- Дифференцирование с вводом в ест. виде
- diffp :: String -> String
- diffp x = showPoly $ derivPoly $ inputPoly x True 1 0 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement