C++ C++;can';字符数组中的t打印字母模式用于循环
我的应用程序的工作是在从用户那里获取文本值后,应用程序以水平模式打印它,但它不显示任何内容 为了缩短问题,我只放了两个26的模式:C++ C++;can';字符数组中的t打印字母模式用于循环,c++,arrays,C++,Arrays,我的应用程序的工作是在从用户那里获取文本值后,应用程序以水平模式打印它,但它不显示任何内容 为了缩短问题,我只放了两个26的模式: int main() { printf("Please inter a text:"); string input; cin >> input; char ptn[2][7][13] = {{ {' ', ' ', ' ', ' ', ' ', ' ', 'A', ' ', ' ', ' ', ' ', ' ', ' '},
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;
}
首先,您试图以一种不起作用的方式迭代std::string
:
使用,此循环将运行正常。根据input.size()
检查i
,或者简单地:
其次,您的字符到索引的映射是关闭的:如果我输入一个
A
,那么ptn[input[I]]
就是ptn['A']
,这又是ptn[0x41]
,超出了ptn
的范围
一个A
应该映射到索引0
,一个B
映射到1
,等等。所以要么做正确的“ASCII数学”,要么使用数组以外的东西,例如std::map
和“手动”映射每个字符
第三,现在可能不那么重要,您没有检查输入是否只包含
ptn
中的字母
因为至少可以输入这样的字母,所以您应该跳过它们或失败,并向用户发送某种错误消息。
很难理解打破子类型中的定义。ptn[2][7][13]
字形集由26个字形组成。
字形由7行组成。
由5列组成(加1表示空终止符):行
- 字形的行由7行5列乘以该行的字符数组成。如果文本为
,则该行将由7行10列组成(加上字母之间的空格)“AB”
#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;
}
#包括
#包括
#包括
枚举
{
列数=5+1,
行数=7,
字形计数=26
};
typedef const char row_t[列计数];
typedef const row_t glyph_t[行计数];
typedef const glyph_t glyph_set_t[glyph_count];
typedef std::字符串行[row_count];
字形集
{
{
{“A”},
{“A”},
{“A”},
{“A”},
{“AAAAA”},
{“A”},
{“A”},
},
{
{“BBBB”},
{“B”},
{“B”},
{“BBBB”},
{“B”},
{“B”},
{“BBBB”},
},
//...
};
int main()
{
常量char*s=“AB”;
对于(int r=0;r std::cout尝试以内部运算符[]的身份传递您的输入。检查is字母表可以确保您的情况下我们不会超出范围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;
}
intmain()
{
最大长度=7;
const int PATTERN_MAX_HEIGHT=13;
const int MAX_PATTERNS=1;
常量字符模式[最大模式][模式最大长度][模式最大高度]=
{
{
{'','','','','','','','',A','','','','','','','','','',''等等,
{'','','','','','',A','','','','','','','},
{'','','','','',A','','','',A','','','','','','','','',''等等,
{'','','A','A','A','A','A','A','A','A','A','A',',
{'','','','','','','','','','','','','','',A','','','',''等等,
{'','A','','','','','','','','','','','','',A',''等等,
{'A','','','','','','','','','','','',A'}
}
};
煤焦产量;
cin>>chInput;
如果(!isalpha(chInput))
{
//不是字母表
返回false;
}
chInput=tolower(chInput);
int index=chInput-'a';
//循环打印
对于(int i=0;i coutptn
是一个三维数组,但你只通过了2个索引。你希望这里会发生什么?为什么'a'
比'B'
宽250%?这是故意的吗?astd::string
不是以null结尾的。@David C.兰金:我知道,但这对我来说并不重要me@MikeBorkland我研究了一下,自从C++11输入[input.size()]
,所以循环应该是正常的。很难看,但还行。你能给我举个例子吗?对不起,我不明白。@Fazel举个什么例子吗?这个应用程序应该只对数组和for循环工作,不能再工作了。而且“A”必须有13列而不是5列“不再”-你应该在问题中指定限制。“A”必须有13列而不是5列。”您可以很容易地更改列数
和行数
@Fazel Jus以明确说明:您不允许使用函数,或者您不允许在期间使用?“您不允许使用函数,或者您不允许在期间使用?”是的!
for (auto c : input)
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 ];
#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;
}
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;
}