C ++无法在用于循环的char数组中打印字母模式 [英] C++ can't print alphabet pattern in char array use for loop

查看:107
本文介绍了C ++无法在用于循环的char数组中打印字母模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的工作是在从用户那里获取文本值之后,该应用程序以水平模式将其打印出来,但没有显示任何内容.

My app's work is after getting text value from user the app print it with pattern horizontally, but it doesn't show any thing.

为使问题更简短,我只输入了26种模式:

for make question shorter i just put 2 patterns of 26:

int main()
{

printf("Please inter a text:");
string input;
cin >> input;

char ptn[2][7][13] = {{
        {' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', ' ', 'A', ' ', 'A', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', 'A', 'A', 'A', 'A', 'A', 'A', 'A', ' ', ' ', ' '},
        {' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' '},
        {' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' '},
        {'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A'}},

        {{'B', 'B', 'B', 'B', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', 'B', 'B', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {' ', ' ', ' ', ' ', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
        {'B', 'B', 'B', 'B', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}}};

for (int y = 0; y < 7; y++) {
    for (int i = 0; input[i] != '\0'; i++) {
           cout << ptn[input[i]][y];
    }

    cout << "\n";
}
return 0;
}

输出应为:

      A       BBBB                                                                                                              
     A A      B   B                                                                                                             
    A   A     B   B                                                                                                             
   AAAAAAA    BBBB                                                                                                              
  A       A   B   B                                                                                                             
 A         A  B   B                                                                                                             
A           A BBBB 

推荐答案

  • ptn[2][7][13]很难理解.在子类型中破坏定义. glyph_set由26个字形组成. glyph由7行组成. row由5列组成(对于空终止符加1):
    • ptn[2][7][13] is difficult to understand. Break the definition in subtypes. A glyph_set is made of 26 glyphs. A glyph is made of 7 rows. A row is made of 5 columns (plus 1 for null terminator):
    •     enum
          {
            col_count = 5 + 1,
            row_count = 7,
            glyph_count = 26
          };
      
          typedef const char row_t[ col_count ];
          typedef const row_t glyph_t[ row_count ];
          typedef const glyph_t glyph_set_t[ glyph_count ];
      

      • 字形的由7行5列乘以构成该行的字符数组成.如果您的文字是"AB",则该行将由7行10列(加上字母之间的空格)组成.
        • A line of glyphs is made of 7 rows and 5 columns multiplied by the number of characters the line is made of. If your text is "AB" the line will be made of 7 rows and 10 columns (plus a space between letters).
        • 您的程序(演示):

          Your program (demo):

          #include <iostream>
          #include <string>
          #include <cctype>
          
          enum
          {
            col_count = 5 + 1,
            row_count = 7,
            glyph_count = 26
          };
          
          typedef const char row_t[ col_count ];
          typedef const row_t glyph_t[ row_count ];
          typedef const glyph_t glyph_set_t[ glyph_count ];
          typedef std::string line_t[ row_count ];
          
          glyph_set_t gs
          {
            {
              {"  A  "},
              {" A A "},
              {"A   A"},
              {"A   A"},
              {"AAAAA"},
              {"A   A"},
              {"A   A"},
            },
          
            {
              {"BBBB "},
              {"B   B"},
              {"B   B"},
              {"BBBB "},
              {"B   B"},
              {"B   B"},
              {"BBBB "},
            },
            //...
          };
          
          
          int main()
          {
            const char* s = "AB";
          
            for( int r = 0; r < row_count; ++r )
            {
              for( const char* p = s; *p; ++p )
              {
                int set_idx = std::toupper( *p ) - 'A';
                // this...
                glyph_t& g = gs[ set_idx ];
                std::cout << g[ r ] << ' ';
                // ...or this (whichever is easier for you)
                // std::cout << gs[ set_idx ][ r ] << ' ';
              }
              std::cout << std::endl;
            }
            return 0;
          }
          

          这篇关于C ++无法在用于循环的char数组中打印字母模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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