C++ 无法在字符数组中使用 for 循环打印字母模式
C++ can't print alphabet pattern in char array use for loop
我的应用程序的工作是从用户那里获取文本值后,应用程序以水平模式打印它,但它没有显示任何内容。
为了使问题更短,我只放置 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] != ' '; 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 列表示空终止符(:
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 列组成(加上字母之间的空格(。
您的程序(演示(:
#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;
}
std::string
:for (int i = 0; input[i] != ' '; i++)
i
与input.size()
,要么简单地:for (auto c : input)
以上可能不是真的。
其次,你的字符到索引映射是关闭的:如果我输入一个A
,那么ptn[input[i]]
是ptn['A']
,而ptn[0x41]
又是,这超出了ptn
的范围。
一个A
应该映射到索引0
,一个B
映射到1
,等等。因此,要么做正确的"ASCII数学",要么使用数组以外的其他东西,例如std::map<std::vector<std::string> >
和"手动"映射每个字符。
第三,也许现在并不那么重要,您没有检查输入是否仅包含您在ptn
中的字母。
由于至少可以输入此类字母,因此您应该跳过它们或失败,并向用户显示某种错误消息。
尝试将输入作为内部运算符[]传递。检查是字母表可以确保我们不会在您的情况下超出范围 26。
int main()
{
const int PATTERN_MAX_LENGHT = 7;
const int PATTERN_MAX_HEIGHT = 13;
const int MAX_PATTERNS = 1;
const char pattern [MAX_PATTERNS][PATTERN_MAX_LENGHT][PATTERN_MAX_HEIGHT] =
{
{
{' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', ' ', 'A', ' ', 'A', ' ', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' '},
{' ', ' ', ' ', 'A', 'A', 'A', 'A', 'A', 'A', 'A', ' ', ' ', ' '},
{' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' '},
{' ', 'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A', ' '},
{'A', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'A'}
}
};
char chInput;
cin>>chInput;
if( ! isalpha ( chInput ) )
{
//Not an Alphabet
return false;
}
chInput = tolower( chInput );
int index = chInput - 'a';
//Print in loop
for(int i =0;i < PATTERN_MAX_LENGHT ; i++ )
{
for(int j=0;j < PATTERN_MAX_HEIGHT ; j++)
{
cout<<pattern[index][i][j];
}
cout<<endl;
}
return 0;
}
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么我的for循环不能正确获取argv
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形
- 如何在for循环中包含两个索引值的测试条件
- 带有多个独立参数的C++For循环
- C++ Python 循环"for i, num in enumerate(list):"版本
- C 多循环 for () 基础知识
- 没有条件值的 FOR 循环"for (int i = 1; ; i++)"无法正常工作
- 为用户提供循环for循环的选项
- 打破循环for循环