0

我正在编写一个程序,它将十进制数、字符、字符串转换为二进制数并使用它们。但我被卡住了,因为我想把 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
4

2 回答 2

1

您所写的功能不是您想要的。

bmod xs ys | not (xs %>=% ys) = xs
           | otherwise = ????

可能会更好。在 ???? 中,您希望从 xs 的开头获取连续数量的数字,直到找到 xs 的前缀大于 ys,然后使用递归

bmod ((xsPrefix %-% ys)++xsSuffix) ys

要获得 xs 的前缀,inits结合 withfilter几乎是您所需要的。显然,您还需要实现一些更多的二进制函数。

您的设计的问题是,在第二种情况下,您没有什么可以递归的——您希望最终以某种方式使用第一种情况的代码,但是除了复制代码。

此外,您的kisegf功能可以稍微清理一下 - 为什么不

kisegf 0 = []
kisegf x = (x `mod` 2) : kisegf (x `div` 2)
于 2012-05-09T03:37:33.477 回答
1

虽然不是您的问题的答案,但我会先保留位串 LSB,而不是 MSB 在前(即不要reverse在 中toBinary)。这样,列表索引对应于位的重要性,因此您不必担心添加前导零来对齐操作数。例如,bxor函数变得更加简单:

bxor [] bs = bs
bxor as [] = as
bxor (a:as) (b:bs) = (a `xor` b) : bxor as bs where
  a `xor` b | a /= b    = 1
            | otherwise = 0

由于进位从 LSB 传播到 MSB,因此按此顺序排列位也会使加法/减法更简单:

badd :: [Int] {- a -} -> [Int] {- b -} -> Int {- carry-in -} -> [Int]
badd []     []     0 = []  -- no carry-out
badd []     []     1 = [1] -- carry-out
badd []     (b:bs) c = s : badd [] bs c' where (c', s) = add 0 b c -- zero-extend as
badd (a:as) []     c = s : badd as [] c' where (c', s) = add a 0 c -- zero-extend bs
badd (a:as) (b:bs) c = s : badd as bs c' where (c', s) = add a b c

add a b c = (s `div` 2, s `mod` 2) where s = a+b+c

左移和右移也更简单,因为它们会影响 LSB:

as `rsh` n = drop n as
as `lsh` n = replicate n 0 ++ as

对于有符号数,您隐含地假设最后一位无限重复。

于 2012-05-09T20:51:29.780 回答