Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483524
  • 博文数量: 23
  • 博客积分: 398
  • 博客等级: 一等列兵
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-03 22:18
文章分类

全部博文(23)

文章存档

2013年(9)

2012年(14)

分类: C/C++

2013-07-20 20:33:58

    实现这么一个函数:传入一个int,在屏幕输出类似LED显示屏效果的字母拼图,例如:

 输入1234567890,输出:


 请注意每个字符的固定宽度和高度,两个数字间保留一个空格

 函数名:void LEDprint(int num);


点击(此处)折叠或打开

  1. /* numberToLED.cpp */
  2. // 打印数字的LED形式
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>

  6. using namespace std;

  7. // LED模子
  8. string LEDarray[][7] = {
  9.    { " --- ", // 0
  10.      "|   |",
  11.      "|   |",
  12.      "     ",
  13.      "|   |",
  14.      "|   |",
  15.      " --- "},

  16.    {"     ", // 1
  17.     "    |",
  18.     "    |",
  19.     "     ",
  20.     "    |",
  21.     "    |",
  22.     "     "},

  23.    {" --- ", // 2
  24.     "    |",
  25.     "    |",
  26.     " --- ",
  27.     "|    ",
  28.     "|    ",
  29.     " --- "},

  30.    {" --- ",
  31.     "    |",
  32.     "    |",
  33.     " --- ",
  34.     "    |",
  35.     "    |",
  36.     " --- "},

  37.    {"     ",
  38.     "|   |",
  39.     "|   |",
  40.     " --- ",
  41.     "    |",
  42.     "    |",
  43.     "     "},

  44.    {" --- ",
  45.     "|    ",
  46.     "|    ",
  47.     " --- ",
  48.     "     |",
  49.     "     |",
  50.     " --- "},

  51.    {" --- ",
  52.     "|    ",
  53.     "|    ",
  54.     " --- ",
  55.     "|   |",
  56.     "|   |",
  57.     " --- "},

  58.    {" --- ",
  59.     "    |",
  60.     "    |",
  61.     "     ",
  62.     "    |",
  63.     "    |",
  64.     "     "},

  65.    {" --- ",
  66.     "|   |",
  67.     "|   |",
  68.     " --- ",
  69.     "|   |",
  70.     "|   |",
  71.     " --- "},

  72.    {" --- ",
  73.     "|   |",
  74.     "|   |",
  75.     " --- ",
  76.     "    |",
  77.     "    |",
  78.     " --- "}
  79. };


  80. void LEDprint(int num)
  81. {
  82.     if (num < 0)
  83.     {
  84.         return;
  85.     }
  86.     char str[12] = {'\0'};
  87.     itoa(num, str, 10);
  88.     
  89.     int len = strlen(str);
  90.     
  91.     string (*LED)[7] = new string[len][7];
  92.     // 拼接到LED[][]中 (其实可以更简化,只拼接索引index即可)
  93.     for (int i = 0; i < len; i++)
  94.     {
  95.         int index = str[i] - '0';
  96.         for (int j = 0; j < 7; j++)
  97.         {
  98.             LED[i][j] = LEDarray[index][j];
  99.         }
  100.         
  101.     }

  102.     // print
  103.     for (int j = 0; j < 7; j++)
  104.     {
  105.         for (int i = 0; i < len; i++)
  106.         {
  107.             cout << LED[i][j] << " ";
  108.         }
  109.         cout << endl;
  110.     }
  111.     delete [] LED;
  112. }

  113. void main()
  114. {
  115.     int num;

  116.     cout << "input a number:";
  117.     cin >> num;

  118.     if (cin.good())
  119.     {
  120.         LEDprint(num);
  121.     }
  122.     
  123.     cout << "... End ..." << endl;
  124. }
 效果如下:

阅读(5025) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

shuang_lin_lei2013-09-09 09:39:16

好帅气的想法