带宽度的计算器7段显示 [英] Calculator 7 Segment Display w/ Width

查看:109
本文介绍了带宽度的计算器7段显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来显示七个部分.它将像这样工作:

I need to write a program to make a seven segment display. It will work like this:

def numbers(number, width):
  # Code to make number

典型的输出可能看起来像这样:

A typical output could look something like this:

numbers(100, 2)

    --   -- 
 | |  | |  |
 | |  | |  |

 | |  | |  |
 | |  | |  |
    --   --

numbers(24, 1)

 -  
  | | |
 -   -
|     |
 -

numbers(1234567890, 1)

   -   -       -   -   -   -   -   - 
|   |   | | | |   |     | | | | | | |
   -   -   -   -   -       -   -     
| |     |   |   | | |   | | |   | | |
   -   -       -   -       -   -   - 

numbers(8453, 3)

 ---         ---   --- 
|   | |   | |         |
|   | |   | |         |
|   | |   | |         |
 ---   ---   ---   --- 
|   |     |     |     |
|   |     |     |     |
|   |     |     |     |
 ---         ---   --- 

无论如何,这些只是几个示例(请注意,花了相当长的时间才能输入).我知道我可以使用诸如'-' * number之类的东西,但是由于我无法弄清楚,这简直令人沮丧!我觉得我应该使用类之类的东西,但是我不能完全依靠它.

Anyway, those are a few examples (which took quite a while to type, mind you). I know I can use things such as '-' * number and such, but it is just frustrating as I cannot figure it out! I feel like I should be using classes or something, but I can't quite put my finger on it.

谢谢.

推荐答案

请勿尝试以编程方式生成细分.手动输入,但以通常的编码方式输入.此编码应允许您产生不同的输出. 执行这些程序.

Don't try to produce segments programmatically. Enter them manually, but in a generally encoded way. This encoding should allow you to produce different outputs. Do program these.

请考虑以下代码

class SSDigit(object):
    """A 7-segment digit"""
    def __init__(self):
        self.lines= []

ssdig0= SSDigit()
ssdig1.lines.append("     ")
ssdig0.lines.append("  -  ")
ssdig0.lines.append(" | | ")
ssdig0.lines.append("     ")
ssdig0.lines.append(" | | ")
ssdig0.lines.append("  -  ")
ssdig1.lines.append("     ")

ssdig1= SSDigit()
ssdig1.lines.append("     ")
ssdig1.lines.append("     ")
ssdig1.lines.append("   | ")
ssdig1.lines.append("     ")
ssdig1.lines.append("   | ")
ssdig1.lines.append("     ")
ssdig1.lines.append("     ")

.....

class LineType(object):
    """Each of the 2 line types (1 horizontal segment or 2 vertical),
       with their possible representations and segment positions"""
    def __init__(self):
        self.valueForRepr= {}
        self.segmentPos= []

class Line(object):
    """Each of the 5 lines a SSDigit has, with its LineType"""
    def __init__(self):
        self.type= None

digits= [ ssdig0, ssdig1, ssdig2, ssdig3, ssdig4, ssdig5, ssdig6, ssdig7, ssdig8, ssdig9 ]

linetype1= LineType()
linetype1.valueForRepr["     "]= [0]
linetype1.valueForRepr["  -  "]= [1]
linetype1.segmentPos= [3]

linetype2= LineType()
linetype2.valueForRepr["     "]= [0,0]
linetype2.valueForRepr["   | "]= [0,1]
linetype2.valueForRepr[" |   "]= [1,0]
linetype2.valueForRepr[" | | "]= [1,1]
linetype2.segmentPos= [2,4]

typeforline= [ linetype1, linetype2, linetype1, linetype2, linetype1 ]

# Validate error-prone typing !!!
for digit in digits :
    for linenum, linetype in enumerate(typeforline) :
        if digit.lines[linenum] not in linetype.valueForRepr :
            print("Error in digit {:d}, line {:d}".format(digit,linenum))

def printNumber(num):
    num= str(num)
    for linenum, linetype in enumerate(typeforline) :
        line= ""
        for d in num :
            line+= digits[int(d)].lines[linenum]
        print( line )

printNumber(3475649560458)

为每个SSDigit输入的字符串只是规范表示.它的结构仅是为了方便开发人员可视化.通过这些表示和其他结构对产生不同大小和形式所需的信息进行编码.

The strings entered for each SSDigit are just canonical representations. Its structure is only for easy developer visualization. The information required to produce different sizes and forms is encoded through these representations and the other structures.

例如

def printNumberDoubleSize(num):
    num= str(num)
    for linenum, linetype in enumerate(typeforline) :
        line= ""
        for d in num :
            line+= digits[int(d)].lines[linenum]
        print( line )

printNumberDoubleSize(3475649560458)

下一步是要认识到,考虑到间距,显示屏由7x5矩阵组成:

Next step is to realize that, allowing for spacing, the display consists of a 7x5 matrix:

  01234
0   
1   -
2  | |
3   -
4  | |
5   -
6

并且矩阵中的每一行和每一列都是逻辑上的,即可以由几条物理的行和/或列组成,例如:

And that each line and column in the matrix are logical ones, i.e. can consist of several physical lines and/or columns, like in:

   012   34
   00012300 
00   
10   ----
20  |    |
 1  |    |
30   ----
40  |    |
 1  |    |
50   ----
60

在这里,大多数逻辑行和列分别仅由一个物理行和列组成,除了逻辑行2和4(每个具有2条物理行)和逻辑列2(具有4个物理列)之外.

Here, most logical rows and columns consist of only one physical row and column, respectively, except logical rows 2 and 4 (with 2 physical lines each), and logical column 2, with 4 physical columns.

这可以表示为一系列字符串.这样就可以非常方便地为每个元素表达我们希望如何在断断续续的状态下看到它.在以下定义中,仅出于示例的目的,我沉迷于某些艺术自由:

This can be represented as a series of strings. And it can result very convenient to express for each element how we would like to see it in an off and on state. In the following definitions I indulged in some artistic liberty just for the sake of the example:

phyLineN= []
phyLineN.append([])
phyLineN[0]= []
phyLineN[0].append([ "....", ".", "....",".", "...." ])
phyLineN.append([])
phyLineN[1]= []
phyLineN[1].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[2]= []
phyLineN[2].append([ ".   ", ".", "    ",".", "    " ])
phyLineN[2].append([ ".   ", " ", "    "," ", "    " ])
phyLineN.append([])
phyLineN[3]= []
phyLineN[3].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[4]= []
phyLineN[4].append([ ".   ", ".", "    ",".", "    " ])
phyLineN[4].append([ ".   ", " ", "    "," ", "    " ])
phyLineN.append([])
phyLineN[5]= []
phyLineN[5].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[6]= []
phyLineN[6].append([ "....", ".", "....",".", "...." ])

phyLineY= []
phyLineY.append([])
phyLineY[0]= []
phyLineY[0].append([ "    ", " ", "    "," ", "    " ])
phyLineY.append([])
phyLineY[1]= []
phyLineY[1].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[2]= []
phyLineY[2].append([ "    ", "H", "    ","H", "    " ])
phyLineY[2].append([ "    ", "H", "    ","H", "    " ])
phyLineY.append([])
phyLineY[3]= []
phyLineY[3].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[4]= []
phyLineY[4].append([ "    ", "H", "    ","H", "    " ])
phyLineY[4].append([ "    ", "H", "    ","H", "    " ])
phyLineY.append([])
phyLineY[5]= []
phyLineY[5].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[6]= []
phyLineY[6].append([ "    ", " ", "    "," ", "    " ])

def printNumberNY(num,structN,structY):

    phyRowH= [ len(structN[0]), len(structN[1]), len(structN[2]), len(structN[3]), len(structN[4]), len(structN[5]), len(structN[6]) ]

    # Validate structure and compute phyColW
    # This could be moved to an object constructor so is computed only once
    first= 1
    for line in structN :
        for phyLine in line :
            if first :
                phyColW= [ len(phyLine[0]), len(phyLine[1]), len(phyLine[2]), len(phyLine[3]), len(phyLine[4]) ] 
                first= 0
            else:
                for i, _ in enumerate(phyLine) :
                    if len(phyLine[i]) != phyColW[i] : raise "Inconsistent physical column width"

    # Real rendering of the (full) number in 7-segment form
    num= str(num)
    for linenum, linetype in enumerate(typeforline) :
        for phyLine in range(phyRowH[linenum]) :
            line= ""
            for d in num :
                for col, qq in enumerate(phyColW) :
                    if digits[int(d)].lines[linenum][col] != " " :
                        line+= structY[linenum][phyLine][col]
                    else:
                        line+= structN[linenum][phyLine][col]
            print( line )

printNumberNY(3475649560458,phyLineN,phyLineY)

printNumberNY的代码并不比简单的宽度* n的代码难得多.

The code for printNumberNY is not much more difficult than the one for the simple case of width*n.

宽度* n的情况实际上是此设置的一种特殊情况,可以用以下方式构造:

The case of width*n is, in fact, a particular case of this setup, and can be constructed with:

def sizeVH(vSegHeight,hSegWidth,vSep,hSep):

    hSepStr= " " *hSep
    hSegN= " "* hSegWidth 
    hSegY= "-"* hSegWidth

    phyLineN= []
    phyLineN.append([])
    phyLineN[0]= []
    phyLineN.append([])
    phyLineN[1]= []
    phyLineN[1].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[2]= []
    for i in range(vSegHeight) :
        phyLineN[2].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[3]= []
    phyLineN[3].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[4]= []
    for i in range(vSegHeight) :
        phyLineN[4].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[5]= []
    phyLineN[5].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[6]= []
    for i in range(vSep) :
        phyLineN[6].append([ "", " ", hSegN," ", hSepStr ])

    phyLineY= []
    phyLineY.append([])
    phyLineY[0]= []
    phyLineY.append([])
    phyLineY[1]= []
    phyLineY[1].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[2]= []
    for i in range(vSegHeight) :
        phyLineY[2].append([ "", "|", hSegN,"|", hSepStr ])
    phyLineY.append([])
    phyLineY[3]= []
    phyLineY[3].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[4]= []
    for i in range(vSegHeight) :
        phyLineY[4].append([ "", "|", hSegN,"|", hSepStr ])
    phyLineY.append([])
    phyLineY[5]= []
    phyLineY[5].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[6]= []
    for i in range(vSep) :
        phyLineY[6].append([ "", " ", hSegN," ", hSepStr ])

    return (phyLineN,phyLineY)

phyLineN, phyLineY= sizeVH(4,6,1,3)
printNumberNY(3475649560458,phyLineN,phyLineY)

我知道我没有使用一开始定义的某些元素,但这就是我要建模的方式.如果我们继续扩展解决方案,它们可能会很有用.

I know that I didn't use some of the elements I defined at the beginning, but this is how I would have modeled it. They could be useful if we continue extending the solution.

这篇关于带宽度的计算器7段显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆