JavaScript:我们可以使用 javascript/angularjs 将由字符(_ & |) 组成的 7 段数字转换为解析数字吗 [英] JavaScript: Can we convert 7-segment numbers made of characters(_ & |) into parsed number using javascript/angularjs

查看:16
本文介绍了JavaScript:我们可以使用 javascript/angularjs 将由字符(_ & |) 组成的 7 段数字转换为解析数字吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript:我们能否使用 javascript/angularjs 将由字符(_ & |)组成的 7 段数字转换为解析后的数字

输入:

<前>_ _ _ _ _ _ _|_|_||_||_ |_ ||_||_|||_ _||_||_|||_|_|

输出:

123456789

解决方案

您可以使用 七段显示及以下

  • 按换行符分割 ' '
  • 用单个 ASCII 数字构建一个数组
  • 将单个 ASCII 数字加入字符串
  • 映射 ASCII 值
  • 将结果加入字符串

ASCII 值与一个数字的对象一起使用,单个段用该段的值加权.

段的值为 2n.

<块引用>

<代码> _0_|5 1|_6_|4 2|_3_

点作为段

<块引用>

<代码> 0561432

这将生成字符串

<块引用>

'909561432'^ ^^^^^^ 表示上面数字的段^ ^ 无段

<小时><块引用>

举个例子

<代码> 01201 |2 |

ASCII 字符串 1,和上面的段号,然后你得到段 1 的值 212值 22.结果是2 + 4 = 6.

在位对象中查找后,

<代码>{63:0,6: 1,//<----91:2,/* ... */}

你得到数字1.

function get7segment(ascii) {返回 ASCII.拆分('
').减少(函数(r,a,i){a.match(/.../g).forEach(function (b, j) {r[j] = r[j] ||[];r[j][i] = b;});返回 r;}, []).地图(功能(一){返回 a.join('');}).地图(功能(一){var 位 = { 63: 0, 6: 1, 91: 2, 79: 3, 102: 4, 109: 5, 125: 6, 7: 7, 127: 8, 111: 9, 0: ' ' },v = '909561432'.split('').reduce(function (r, v, i) {返回 r + ((a[i] !== ' ') << v);}, 0);以位为单位返回 v ?位[v]:'*';//* 是非法字符}).加入('');}功能打印(ASCII){var pre = document.createElement('pre');pre.innerHTML = ascii + '

' + get7segment(ascii);document.body.appendChild(pre);}打印(' _ _ _ _ _ _ _ 
| | | _| _||_||_ |_ ||_||_| 
|_| ||_ _| | _||_|||_| _|');打印(' _ _ _ ...
 | _| _| | ...
 | _| _| | ...');print(' _ _ _ _ _ 
|_||_|| ||_||_ |
 | _||_||_||_| |');

JavaScript: Can we convert 7-segment numbers made of characters(_ & |) into parsed number using javascript/angularjs

Input :

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

Output :

123456789

解决方案

You could use the encoding of a seven-segment display and the following

  • split by linefeed ' '
  • build an array with single ASCII digits
  • join a single ASCII digit to a string
  • map the ASCII value
  • join the result to a string

The ASCII value is taken with an object for a number, and the single segments are weighted with a value for the segment.

Value of segments is 2n.

  _0_    
|5   1|
  _6_  
|4   2|
  _3_

Dots as segments

 0
561
432

This generates the string

'909561432'
  ^ ^^^^^^  denoted segments with the number above
 ^ ^        no segments


For example, take

 012
0
1  |
2  |

the ASCII string of 1, and the above segment numbers, then you get for the segment 1 the value 21 and for 2 the value 22. The result is 2 + 4 = 6.

After a lookup in the bits object,

{
    63: 0,
    6: 1, // <----
    91: 2, 
    /* ... */
}

you get the digit 1.

function get7segment(ascii) {
    return ascii.
        split('
').
        reduce(function (r, a, i) {
            a.match(/.../g).forEach(function (b, j) {
                r[j] = r[j] || [];
                r[j][i] = b;
            });
            return r;
        }, []).
        map(function (a) {
            return a.join('');
        }).
        map(function (a) {
            var bits = { 63: 0, 6: 1, 91: 2, 79: 3, 102: 4, 109: 5, 125: 6, 7: 7, 127: 8, 111: 9, 0: ' ' },
                v = '909561432'.split('').reduce(function (r, v, i) {
                    return r + ((a[i] !== ' ') << v);
                }, 0);
            return v in bits ? bits[v] : '*'; // * is an illegal character
        }).
        join('');
}

function print(ascii) {
    var pre = document.createElement('pre');
    pre.innerHTML = ascii + '

' + get7segment(ascii);
    document.body.appendChild(pre);
}

print(' _     _  _     _  _  _  _  _ 
| |  | _| _||_||_ |_   ||_||_| 
|_|  ||_  _|  | _||_|  ||_| _|');
print('    _  _  _    ...
  | _| _|  |   ...
  | _| _|  |   ...');
print('    _  _  _  _  _ 
|_||_|| ||_||_   |
  | _||_||_||_|  |');

这篇关于JavaScript:我们可以使用 javascript/angularjs 将由字符(_ &amp; |) 组成的 7 段数字转换为解析数字吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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