Python-W1O2-数字转换基础实例的问题

时间:2019-03-13 16:42:43

标签: python python-3.x

我有以下代码:

#!/usr/bin/env python3

__version__ = '1.1'
__authorname__ = 'Anonymous'
__author__ = 'anonymous'
__studentnummer__ = '000000000'
__opgave__ = 'w1o2.py'

import sys
import getopt

from w1o2_tbl import kAddTbl


def myAdd(a, b, base=10):
    aLen = len(a)
    bLen = len(b)
    n = max(aLen, bLen)
    r, c = "", '0'
    for i in range(1, n + 1):
        ai = a[-i] if i <= aLen else '0'
        bi = b[-i] if i <= bLen else '0'
        c1, s1 = kAddTbl[base][c, ai]
        c2, s2 = kAddTbl[base][s1, bi]
        c = c1 if c1 != '0' else c2
        r = s2 + r
    if c == '1': r = c + r
    return r


gVbs = False
if __name__ == '__main__':
    tests = [
        ("9", "12", "21", 10),
        ("64", "7", "71", 10),
        ("923", "98", "1021", 10),
    ]
    for a, b, r, base in tests:
        c = myAdd(a, b, base)
        if c != r:
            print('Error: B{}:\t{} + {} != {} [{}]'.format(base, a, b, c, r))
        else:
            print('B{}:\t{}+{}={}'.format(base, a, b, c))
    print('Tests done.')
    print('Dit programma is gemaakt door: ' + __authorname__ + " " + __studentnummer__)

但是它只会从该表中打印以10为底的正确答案:

# Generated by w1o2_gen.py
kAddTbl={
   2: {
     ('0', '0'): ('0', '0'), ('0', '1'): ('0', '1'),
     ('1', '0'): ('0', '1'), ('1', '1'): ('1', '0'),
  },
   4: {
     ('0', '0'): ('0', '0'), ('0', '1'): ('0', '1'), ('0', '2'): ('0', '2'), ('0', '3'): ('0', '3'),
     ('1', '0'): ('0', '1'), ('1', '1'): ('0', '2'), ('1', '2'): ('0', '3'), ('1', '3'): ('1', '0'),
     ('2', '0'): ('0', '2'), ('2', '1'): ('0', '3'), ('2', '2'): ('1', '0'), ('2', '3'): ('1', '1'),
     ('3', '0'): ('0', '3'), ('3', '1'): ('1', '0'), ('3', '2'): ('1', '1'), ('3', '3'): ('1', '2'),
  },
   8: {
     ('0', '0'): ('0', '0'), ('0', '1'): ('0', '1'), ('0', '2'): ('0', '2'), ('0', '3'): ('0', '3'),
     ('0', '4'): ('0', '4'), ('0', '5'): ('0', '5'), ('0', '6'): ('0', '6'), ('0', '7'): ('0', '7'),
     ('1', '0'): ('0', '1'), ('1', '1'): ('0', '2'), ('1', '2'): ('0', '3'), ('1', '3'): ('0', '4'),
     ('1', '4'): ('0', '5'), ('1', '5'): ('0', '6'), ('1', '6'): ('0', '7'), ('1', '7'): ('1', '0'),
     ('2', '0'): ('0', '2'), ('2', '1'): ('0', '3'), ('2', '2'): ('0', '4'), ('2', '3'): ('0', '5'),
     ('2', '4'): ('0', '6'), ('2', '5'): ('0', '7'), ('2', '6'): ('1', '0'), ('2', '7'): ('1', '1'),
     ('3', '0'): ('0', '3'), ('3', '1'): ('0', '4'), ('3', '2'): ('0', '5'), ('3', '3'): ('0', '6'),
     ('3', '4'): ('0', '7'), ('3', '5'): ('1', '0'), ('3', '6'): ('1', '1'), ('3', '7'): ('1', '2'),
     ('4', '0'): ('0', '4'), ('4', '1'): ('0', '5'), ('4', '2'): ('0', '6'), ('4', '3'): ('0', '7'),
     ('4', '4'): ('1', '0'), ('4', '5'): ('1', '1'), ('4', '6'): ('1', '2'), ('4', '7'): ('1', '3'),
     ('5', '0'): ('0', '5'), ('5', '1'): ('0', '6'), ('5', '2'): ('0', '7'), ('5', '3'): ('1', '0'),
     ('5', '4'): ('1', '1'), ('5', '5'): ('1', '2'), ('5', '6'): ('1', '3'), ('5', '7'): ('1', '4'),
     ('6', '0'): ('0', '6'), ('6', '1'): ('0', '7'), ('6', '2'): ('1', '0'), ('6', '3'): ('1', '1'),
     ('6', '4'): ('1', '2'), ('6', '5'): ('1', '3'), ('6', '6'): ('1', '4'), ('6', '7'): ('1', '5'),
     ('7', '0'): ('0', '7'), ('7', '1'): ('1', '0'), ('7', '2'): ('1', '1'), ('7', '3'): ('1', '2'),
     ('7', '4'): ('1', '3'), ('7', '5'): ('1', '4'), ('7', '6'): ('1', '5'), ('7', '7'): ('1', '6'),
  },
  10: {
     ('0', '0'): ('0', '0'), ('0', '1'): ('0', '1'), ('0', '2'): ('0', '2'), ('0', '3'): ('0', '3'), ('0', '4'): ('0', '4'),
     ('0', '5'): ('0', '5'), ('0', '6'): ('0', '6'), ('0', '7'): ('0', '7'), ('0', '8'): ('0', '8'), ('0', '9'): ('0', '9'),
     ('1', '0'): ('0', '1'), ('1', '1'): ('0', '2'), ('1', '2'): ('0', '3'), ('1', '3'): ('0', '4'), ('1', '4'): ('0', '5'),
     ('1', '5'): ('0', '6'), ('1', '6'): ('0', '7'), ('1', '7'): ('0', '8'), ('1', '8'): ('0', '9'), ('1', '9'): ('1', '0'),
     ('2', '0'): ('0', '2'), ('2', '1'): ('0', '3'), ('2', '2'): ('0', '4'), ('2', '3'): ('0', '5'), ('2', '4'): ('0', '6'),
     ('2', '5'): ('0', '7'), ('2', '6'): ('0', '8'), ('2', '7'): ('0', '9'), ('2', '8'): ('1', '0'), ('2', '9'): ('1', '1'),
     ('3', '0'): ('0', '3'), ('3', '1'): ('0', '4'), ('3', '2'): ('0', '5'), ('3', '3'): ('0', '6'), ('3', '4'): ('0', '7'),
     ('3', '5'): ('0', '8'), ('3', '6'): ('0', '9'), ('3', '7'): ('1', '0'), ('3', '8'): ('1', '1'), ('3', '9'): ('1', '2'),
     ('4', '0'): ('0', '4'), ('4', '1'): ('0', '5'), ('4', '2'): ('0', '6'), ('4', '3'): ('0', '7'), ('4', '4'): ('0', '8'),
     ('4', '5'): ('0', '9'), ('4', '6'): ('1', '0'), ('4', '7'): ('1', '1'), ('4', '8'): ('1', '2'), ('4', '9'): ('1', '3'),
     ('5', '0'): ('0', '5'), ('5', '1'): ('0', '6'), ('5', '2'): ('0', '7'), ('5', '3'): ('0', '8'), ('5', '4'): ('0', '9'),
     ('5', '5'): ('1', '0'), ('5', '6'): ('1', '1'), ('5', '7'): ('1', '2'), ('5', '8'): ('1', '3'), ('5', '9'): ('1', '4'),
     ('6', '0'): ('0', '6'), ('6', '1'): ('0', '7'), ('6', '2'): ('0', '8'), ('6', '3'): ('0', '9'), ('6', '4'): ('1', '0'),
     ('6', '5'): ('1', '1'), ('6', '6'): ('1', '2'), ('6', '7'): ('1', '3'), ('6', '8'): ('1', '4'), ('6', '9'): ('1', '5'),
     ('7', '0'): ('0', '7'), ('7', '1'): ('0', '8'), ('7', '2'): ('0', '9'), ('7', '3'): ('1', '0'), ('7', '4'): ('1', '1'),
     ('7', '5'): ('1', '2'), ('7', '6'): ('1', '3'), ('7', '7'): ('1', '4'), ('7', '8'): ('1', '5'), ('7', '9'): ('1', '6'),
     ('8', '0'): ('0', '8'), ('8', '1'): ('0', '9'), ('8', '2'): ('1', '0'), ('8', '3'): ('1', '1'), ('8', '4'): ('1', '2'),
     ('8', '5'): ('1', '3'), ('8', '6'): ('1', '4'), ('8', '7'): ('1', '5'), ('8', '8'): ('1', '6'), ('8', '9'): ('1', '7'),
     ('9', '0'): ('0', '9'), ('9', '1'): ('1', '0'), ('9', '2'): ('1', '1'), ('9', '3'): ('1', '2'), ('9', '4'): ('1', '3'),
     ('9', '5'): ('1', '4'), ('9', '6'): ('1', '5'), ('9', '7'): ('1', '6'), ('9', '8'): ('1', '7'), ('9', '9'): ('1', '8'),
  },
  16: {
     ('0', '0'): ('0', '0'), ('0', '1'): ('0', '1'), ('0', '2'): ('0', '2'), ('0', '3'): ('0', '3'),
     ('0', '4'): ('0', '4'), ('0', '5'): ('0', '5'), ('0', '6'): ('0', '6'), ('0', '7'): ('0', '7'),
     ('0', '8'): ('0', '8'), ('0', '9'): ('0', '9'), ('0', 'A'): ('0', 'A'), ('0', 'B'): ('0', 'B'),
     ('0', 'C'): ('0', 'C'), ('0', 'D'): ('0', 'D'), ('0', 'E'): ('0', 'E'), ('0', 'F'): ('0', 'F'),
     ('1', '0'): ('0', '1'), ('1', '1'): ('0', '2'), ('1', '2'): ('0', '3'), ('1', '3'): ('0', '4'),
     ('1', '4'): ('0', '5'), ('1', '5'): ('0', '6'), ('1', '6'): ('0', '7'), ('1', '7'): ('0', '8'),
     ('1', '8'): ('0', '9'), ('1', '9'): ('0', 'A'), ('1', 'A'): ('0', 'B'), ('1', 'B'): ('0', 'C'),
     ('1', 'C'): ('0', 'D'), ('1', 'D'): ('0', 'E'), ('1', 'E'): ('0', 'F'), ('1', 'F'): ('1', '0'),
     ('2', '0'): ('0', '2'), ('2', '1'): ('0', '3'), ('2', '2'): ('0', '4'), ('2', '3'): ('0', '5'),
     ('2', '4'): ('0', '6'), ('2', '5'): ('0', '7'), ('2', '6'): ('0', '8'), ('2', '7'): ('0', '9'),
     ('2', '8'): ('0', 'A'), ('2', '9'): ('0', 'B'), ('2', 'A'): ('0', 'C'), ('2', 'B'): ('0', 'D'),
     ('2', 'C'): ('0', 'E'), ('2', 'D'): ('0', 'F'), ('2', 'E'): ('1', '0'), ('2', 'F'): ('1', '1'),
     ('3', '0'): ('0', '3'), ('3', '1'): ('0', '4'), ('3', '2'): ('0', '5'), ('3', '3'): ('0', '6'),
     ('3', '4'): ('0', '7'), ('3', '5'): ('0', '8'), ('3', '6'): ('0', '9'), ('3', '7'): ('0', 'A'),
     ('3', '8'): ('0', 'B'), ('3', '9'): ('0', 'C'), ('3', 'A'): ('0', 'D'), ('3', 'B'): ('0', 'E'),
     ('3', 'C'): ('0', 'F'), ('3', 'D'): ('1', '0'), ('3', 'E'): ('1', '1'), ('3', 'F'): ('1', '2'),
     ('4', '0'): ('0', '4'), ('4', '1'): ('0', '5'), ('4', '2'): ('0', '6'), ('4', '3'): ('0', '7'),
     ('4', '4'): ('0', '8'), ('4', '5'): ('0', '9'), ('4', '6'): ('0', 'A'), ('4', '7'): ('0', 'B'),
     ('4', '8'): ('0', 'C'), ('4', '9'): ('0', 'D'), ('4', 'A'): ('0', 'E'), ('4', 'B'): ('0', 'F'),
     ('4', 'C'): ('1', '0'), ('4', 'D'): ('1', '1'), ('4', 'E'): ('1', '2'), ('4', 'F'): ('1', '3'),
     ('5', '0'): ('0', '5'), ('5', '1'): ('0', '6'), ('5', '2'): ('0', '7'), ('5', '3'): ('0', '8'),
     ('5', '4'): ('0', '9'), ('5', '5'): ('0', 'A'), ('5', '6'): ('0', 'B'), ('5', '7'): ('0', 'C'),
     ('5', '8'): ('0', 'D'), ('5', '9'): ('0', 'E'), ('5', 'A'): ('0', 'F'), ('5', 'B'): ('1', '0'),
     ('5', 'C'): ('1', '1'), ('5', 'D'): ('1', '2'), ('5', 'E'): ('1', '3'), ('5', 'F'): ('1', '4'),
     ('6', '0'): ('0', '6'), ('6', '1'): ('0', '7'), ('6', '2'): ('0', '8'), ('6', '3'): ('0', '9'),
     ('6', '4'): ('0', 'A'), ('6', '5'): ('0', 'B'), ('6', '6'): ('0', 'C'), ('6', '7'): ('0', 'D'),
     ('6', '8'): ('0', 'E'), ('6', '9'): ('0', 'F'), ('6', 'A'): ('1', '0'), ('6', 'B'): ('1', '1'),
     ('6', 'C'): ('1', '2'), ('6', 'D'): ('1', '3'), ('6', 'E'): ('1', '4'), ('6', 'F'): ('1', '5'),
     ('7', '0'): ('0', '7'), ('7', '1'): ('0', '8'), ('7', '2'): ('0', '9'), ('7', '3'): ('0', 'A'),
     ('7', '4'): ('0', 'B'), ('7', '5'): ('0', 'C'), ('7', '6'): ('0', 'D'), ('7', '7'): ('0', 'E'),
     ('7', '8'): ('0', 'F'), ('7', '9'): ('1', '0'), ('7', 'A'): ('1', '1'), ('7', 'B'): ('1', '2'),
     ('7', 'C'): ('1', '3'), ('7', 'D'): ('1', '4'), ('7', 'E'): ('1', '5'), ('7', 'F'): ('1', '6'),
     ('8', '0'): ('0', '8'), ('8', '1'): ('0', '9'), ('8', '2'): ('0', 'A'), ('8', '3'): ('0', 'B'),
     ('8', '4'): ('0', 'C'), ('8', '5'): ('0', 'D'), ('8', '6'): ('0', 'E'), ('8', '7'): ('0', 'F'),
     ('8', '8'): ('1', '0'), ('8', '9'): ('1', '1'), ('8', 'A'): ('1', '2'), ('8', 'B'): ('1', '3'),
     ('8', 'C'): ('1', '4'), ('8', 'D'): ('1', '5'), ('8', 'E'): ('1', '6'), ('8', 'F'): ('1', '7'),
     ('9', '0'): ('0', '9'), ('9', '1'): ('0', 'A'), ('9', '2'): ('0', 'B'), ('9', '3'): ('0', 'C'),
     ('9', '4'): ('0', 'D'), ('9', '5'): ('0', 'E'), ('9', '6'): ('0', 'F'), ('9', '7'): ('1', '0'),
     ('9', '8'): ('1', '1'), ('9', '9'): ('1', '2'), ('9', 'A'): ('1', '3'), ('9', 'B'): ('1', '4'),
     ('9', 'C'): ('1', '5'), ('9', 'D'): ('1', '6'), ('9', 'E'): ('1', '7'), ('9', 'F'): ('1', '8'),
     ('A', '0'): ('0', 'A'), ('A', '1'): ('0', 'B'), ('A', '2'): ('0', 'C'), ('A', '3'): ('0', 'D'),
     ('A', '4'): ('0', 'E'), ('A', '5'): ('0', 'F'), ('A', '6'): ('1', '0'), ('A', '7'): ('1', '1'),
     ('A', '8'): ('1', '2'), ('A', '9'): ('1', '3'), ('A', 'A'): ('1', '4'), ('A', 'B'): ('1', '5'),
     ('A', 'C'): ('1', '6'), ('A', 'D'): ('1', '7'), ('A', 'E'): ('1', '8'), ('A', 'F'): ('1', '9'),
     ('B', '0'): ('0', 'B'), ('B', '1'): ('0', 'C'), ('B', '2'): ('0', 'D'), ('B', '3'): ('0', 'E'),
     ('B', '4'): ('0', 'F'), ('B', '5'): ('1', '0'), ('B', '6'): ('1', '1'), ('B', '7'): ('1', '2'),
     ('B', '8'): ('1', '3'), ('B', '9'): ('1', '4'), ('B', 'A'): ('1', '5'), ('B', 'B'): ('1', '6'),
     ('B', 'C'): ('1', '7'), ('B', 'D'): ('1', '8'), ('B', 'E'): ('1', '9'), ('B', 'F'): ('1', 'A'),
     ('C', '0'): ('0', 'C'), ('C', '1'): ('0', 'D'), ('C', '2'): ('0', 'E'), ('C', '3'): ('0', 'F'),
     ('C', '4'): ('1', '0'), ('C', '5'): ('1', '1'), ('C', '6'): ('1', '2'), ('C', '7'): ('1', '3'),
     ('C', '8'): ('1', '4'), ('C', '9'): ('1', '5'), ('C', 'A'): ('1', '6'), ('C', 'B'): ('1', '7'),
     ('C', 'C'): ('1', '8'), ('C', 'D'): ('1', '9'), ('C', 'E'): ('1', 'A'), ('C', 'F'): ('1', 'B'),
     ('D', '0'): ('0', 'D'), ('D', '1'): ('0', 'E'), ('D', '2'): ('0', 'F'), ('D', '3'): ('1', '0'),
     ('D', '4'): ('1', '1'), ('D', '5'): ('1', '2'), ('D', '6'): ('1', '3'), ('D', '7'): ('1', '4'),
     ('D', '8'): ('1', '5'), ('D', '9'): ('1', '6'), ('D', 'A'): ('1', '7'), ('D', 'B'): ('1', '8'),
     ('D', 'C'): ('1', '9'), ('D', 'D'): ('1', 'A'), ('D', 'E'): ('1', 'B'), ('D', 'F'): ('1', 'C'),
     ('E', '0'): ('0', 'E'), ('E', '1'): ('0', 'F'), ('E', '2'): ('1', '0'), ('E', '3'): ('1', '1'),
     ('E', '4'): ('1', '2'), ('E', '5'): ('1', '3'), ('E', '6'): ('1', '4'), ('E', '7'): ('1', '5'),
     ('E', '8'): ('1', '6'), ('E', '9'): ('1', '7'), ('E', 'A'): ('1', '8'), ('E', 'B'): ('1', '9'),
     ('E', 'C'): ('1', 'A'), ('E', 'D'): ('1', 'B'), ('E', 'E'): ('1', 'C'), ('E', 'F'): ('1', 'D'),
     ('F', '0'): ('0', 'F'), ('F', '1'): ('1', '0'), ('F', '2'): ('1', '1'), ('F', '3'): ('1', '2'),
     ('F', '4'): ('1', '3'), ('F', '5'): ('1', '4'), ('F', '6'): ('1', '5'), ('F', '7'): ('1', '6'),
     ('F', '8'): ('1', '7'), ('F', '9'): ('1', '8'), ('F', 'A'): ('1', '9'), ('F', 'B'): ('1', 'A'),
     ('F', 'C'): ('1', 'B'), ('F', 'D'): ('1', 'C'), ('F', 'E'): ('1', 'D'), ('F', 'F'): ('1', 'E'),
  },
}

这就是他们想要的:

实现一个add函数myAdd,该函数有两个字符串参数,它们是一个数字,表示一个特定的基数。目的是借助加法表kAddTbl 添加加法。如果您将两个数字(字符)相加,则会得到总和(s)和进位(c)。这个 此外,您必须对两个字符串中的所有数字都执行智能操作。注意如果底数是16 字母(A..F)。

现在的问题是,基数10可以正常工作,尽管它不会产生任何错误。但是,如果我将Base更改为表中现有的其他内容,则会出现错误。

有人可以帮我吗?

0 个答案:

没有答案