微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 7段显示,带有printf和段的多维数组

在K.N Kings“C编程:一种现代方法”,第10章练习7中,任务是在ASCII艺术中将数字转换器从正常数字转换为7段数字,如下所示:

_   _        _   _   _   _   _   _ 
|  _|  _|  |_| |_  |_    | |_| |_| | |
| |_   _|    |  _| |_|   | |_|   | |_|

我得到了每个数字的序列,可以打开和关闭

样品:

int digit_sequence[10][7] = {
    // A,B,C,D,E,F,G
    /* 0 */ {1,1,0}

}

其中1 = ON,0 = OFF

但是我很难让process_digit(int digit,int position)函数起作用.

我很难将序列[10] [7]翻译成数字[4] [MAX_DIGITS * 4]

一个善良的灵魂能帮帮我吗?

我一直在阅读高尔夫代码七段挑战,但即使我理解这个理论,仍然很难说服我的大脑用多个阵列做我想做的事情.

忽略ASCII艺术,问题如下:

Write a program that prompts the user for a number and then displays the number using characters to simulate the effect of a seven-segment display.

Characters other than digits should be ignored. Write the program so that the maximum number of digits is controlled by a macro named MAX_DIGITS which has the value 10. If the number contains more than this number of digits,the extra digits are ignored. Hints: Use two external arrays. One is the segments array […] which stores data representing the correspondence between digits and segments. The other array,digits,will be an array of characters with 4 rows (since each segmented digit is 4 characters high) and MAX_DIGITS * 4 columns (digits are three characters wide but a spaces is needed between digits for readability). Write your program as four functions: main,[…]

06002

clear_digits_array will store blank characters into all elements of the digits array. process_digit will store the seven-segment representation of digit into a specified position in the digits array (positions range from 0 to MAX_DIGITS - 1). print_digits_array will display the rows of the digits array,each on a single line […].

解决方法

诀窍是映射段显示的位置.

每个细分的行号相同.

0:  _   _
1: |_| |_| ...
2: |_| |_|
3:

但是,列因位置而异.每个位置是一个4个字符宽度的“迷你矩阵”(3个用于段,1个用于空格:’| _ |’).因此,我们修复了分段线,并在“迷你矩阵”上将其列与(位置* 4)相加.

0123 4567 89AB
 _    _    _
|_|  |_|  |_|   ...
|_|  |_|  |_|
pos0 pos1 pos2

得到它了?代码将是这样的:

void process_digit(int digit,int position){
    int i;
    for(i=0;i<7;i++){
        if(segments[digit][i]==1) /* Has digit the i segment? */
            switch(i){
                case 0: digits[0][1+position*4]='_';break;
                case 1: digits[1][2+position*4]='|';break;
                case 2: digits[2][2+position*4]='|';break;
                case 3: digits[2][1+position*4]='_';break;
                case 4: digits[2][0+position*4]='|';break;
                case 5: digits[1][0+position*4]='|';break;
                case 6: digits[1][1+position*4]='_';break;
            }
    }
}

(您可以选择’ – ‘和’_’,或者可能更改某些行)

希望能帮助到你.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐