|  | 
 
 发表于 2021-9-14 21:09:49
|
显示全部楼层 
| 七段顯示器(Seven-segment display) 
 我寫了 Python 供參考,你試試自己寫寫看 C++ 吧
 Python 代碼:
 
 复制代码def SevenSegment(h, m, s):
    # 先將即將輸出符號分別以 上,中,下 三個階段儲存進字典裡,方便之後運用
    numbers = {
    0: [" _ ", "| |", "|_|"],
    1: ["   ", "  |", "  |"],
    2: [" _ ", " _|", "|_ "],
    3: [" _ ", " _|", " _|"],
    4: ["   ", "|_|", "  |"],
    5: [" _ ", "|_ ", " _|"],
    6: [" _ ", "|_ ", "|_|"],
    7: [" _ ", "  |", "  |"],
    8: [" _ ", "|_|", "|_|"],
    9: [" _ ", "|_|", " _|"],
    ":": ["   ", " . ", " . "]}
    # 對照以上字典,根據 上,中,下 正確位置輸入對應符號
    def decoder(arr, n):
        res = arr[:]
        for i in range(3):
            res[i].append(numbers[n][i])
        return res
    
    # 創建新列表包含三個子列表,分別表示 上,中,下 三層
    res = [[], [], []]
    # 輸入小時
    if h >= 10:
        a, b = divmod(h, 10)
        res = decoder(res, a)
        res = decoder(res, b)
    else:
        res = decoder(res, 0)
        res = decoder(res, h)
    
    # 輸入冒號 ":"
    res = decoder(res, ":")
    # 輸入分鐘
    if m <= 0:
        res = decoder(res, 0)
        res = decoder(res, m)
    else:
        a, b = divmod(m, 10)
        res = decoder(res, a)
        res = decoder(res, b)
    
    # 輸入冒號 ":"
    res = decoder(res, ":")
    # 輸入秒
    if s <= 0:
        res = decoder(res, 0)
        res = decoder(res, s)
    else:
        a, b = divmod(s, 10)
        res = decoder(res, a)
        res = decoder(res, b)
    return res # 返回完整七段顯示器
if __name__ == "__main__":
    timer = SevenSegment(8, 21, 37) # 範例
    for digit in timer:
        print(*digit) # 打印
复制代码 _   _       _           _   _ 
| | |_|  .   _|   |  .   _|   |
|_| |_|  .  |_    |  .   _|   |
 | 
 |