我正在编写一个程序,它将十进制数、字符、字符串转换为二进制数并使用它们。但我被卡住了,因为我想把 Bin 分开。像这样的东西:
11010110110000
/ 10011
--------------
= 01001110110000
所以新数字将是 1001110110000 / 10011 ......直到最后一个结果。
这是我的代码:
import Data.Char (ord)
import Data.List
toBinary :: Int -> [Int]
toBinary 0 = []
toBinary x = reverse (kisegf x)
kisegf 0 = []
kisegf x | x `mod` 2 == 1 = 1 : kisegf (x `div` 2)
| x `mod` 2 == 0 = 0 : kisegf (x `div` 2)
chrToBinary :: Char -> [Int]
chrToBinary x
|length (toBinary (ord x)) == 8 = (toBinary (ord x))
|otherwise = take (8-(length (toBinary (ord x))))[0,0..] ++ (toBinary (ord x))
strToBinary :: String -> [Int]
strToBinary [] = []
strToBinary (x:xs) = [l | l <- chrToBinary x] ++ strToBinary xs
bxor :: [Int] -> [Int] -> [Int]
bxor [] [] = []
bxor (x:xs) (y:ys)
|length (x:xs) == length (y:ys) && (x /= y) = 1 : bxor xs ys
|length (x:xs) == length (y:ys) && (x == y) = 0 : bxor xs ys
|length (x:xs) < length (y:ys) && (x /= y) = 1 : bxor (take (length (y:ys)-(length (x:xs)))[0,0..] ++ xs) ys
|length (x:xs) < length (y:ys) && (x == y) = 0 : bxor (take (length (y:ys)-(length (x:xs)))[0,0..] ++ xs) ys
|length (x:xs) > length (y:ys) && (x /= y) = 1 : bxor xs (take (length (x:xs)-(length (y:ys)))[0,0..] ++ ys)
|length (x:xs) > length (y:ys) && (x == y) = 0 : bxor xs (take (length (x:xs)-(length (y:ys)))[0,0..] ++ ys)
{-this will compare 2 bin if a bigger than true else false-}
(%>=%) :: [Int] -> [Int] -> Bool
(%>=%)[] [] = True
(%>=%)[] _ = False
(%>=%)_ [] = True
(%>=%) (x:xs) (y:ys) = x==1 && y==1 && elemIndex 1 (x:xs) == elemIndex 1 (y:ys)
bmod :: [Int]{-number-} -> [Int]{-div-} -> [Int]{-result-}
bmod (x:xs) (y:ys)
|length(x:xs) >= length(y:ys) && (take (length (y:ys)) (x:xs)) %>=% (y:ys) = ???
|length(x:xs) >= length(y:ys) = ???
|otherwise = (x:xs)
我应该在“???”的地方写什么
另一个更大的例如:
Példa: bmod 11010110110000 10011.
_______________
10011 ) 11010110110000
10011,,.,,....
-----,,.,,....
10011,.,,....
10011,.,,....
-----,.,,....
00001.,,....
00000.,,....
-----.,,....
00010,,....
00000,,....
-----,,....
00101,....
00000,....
-----,....
01011....
00000....
-----....
10110...
10011...
-----...
01010..
00000..
-----..
10100.
10011.
-----.
01110
10011 <- bigger so cant div again
-----
1110 = what i want