[英]How do I print out a 2-D array of the alphabet using a keyword?
因此,我的任务是打印出一个 5x5 的二维数组,该数组的每个字母都位于数组的每个位置。 我应该使用关键字并将其打印在二维数组的行中,然后打印出字母表中的其余字母。 字母应该只在整个数组中打印一次,并且排除 'Z'。
不太好,但这是我到目前为止所拥有的:
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
bool correct_keyword(char);
int main()
{
ifstream fin ("infile.txt");
char array[5][5];
char i, keyword;
bool correct;
cout << "Type in the key word: " << endl;
cin >> keyword;
correct_keyword(keyword);
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
keyword= array[row][col];
}
}
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
cout << keyword << " ";
}
cout << endl;
}
return 0;
}
bool correct_keyword(char keyword)
{
bool correct = false;
if (keyword != correct)
return true;
else
return 1;
}
如果我的关键字是“鱼”,它看起来像这样:
0 1 2 3 4
0 F I S H A
1 B C D E G
2 J K L M N
3 O P Q R T
4 U V W X Y
任何帮助,将不胜感激!! 非常感谢!
如果您已经在 2D 数组 (5x5) 中声明了字母表,例如在char alphabet[5][5]
,您可以使用嵌套的 for 循环打印这些值:
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
std::cout << alphabet[i][j] << std::endl;
代码将在新行中打印所有字母,如果您需要将它们打印为 5x5 数组,那么这应该可以工作:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
std::cout << alphabet[i][j] << " "; // separate letters with a space
}
std::cout << std::endl;
}
为什么不使用std::string
来存储关键字? 这提供了.find()
来检查是否包含字符。 您不必担心 2D 数组,您只需要std::string
和一个计数器,并且您可以在每 5个字符后输出'\\n'
以在 5x5 网格中提供您的输出。
您如何处理具有重复字符的关键字? 他们只是被认为是无效的关键字吗? "wizard"
又会是一个无效的关键字,因为它包含排除的'z'
字符吗? (我想两者都必须是,否则您将无法在 5x5 网格中填充'y'
)
话虽如此,您可以简单地将关键字作为程序的第一个参数,并保留输出字符数的计数器并执行以下简单操作:
#include <iostream>
#include <string>
#define WIDTH 5
int main (int argc, char **argv) {
std::string s = argc > 1 ? argv[1] : "help";
size_t n = 0;
现在你需要检查你的关键字是否有效,没有重复的字符,没有'z'
,例如
for (size_t i = 0; i < s.size() - 1; i++) /* check for duplicate chars */
if (s.find(s.at(i), i+1) != std::string::npos) {
std::cerr << "error: '" << s.at(i) << "' duplcate char.\n";
return 1;
}
if (s.find('z') != std::string::npos) { /* check for 'z' in input */
std::cerr << "error: input contains 'z'.\n";
return 1;
}
剩下的就是输出您的关键字,然后是字母表中的其余字符(减去'z'
)。 你可以用两个循环来做到这一点,例如
for (; n < s.size(); n++) { /* output keyword, keep n as count */
if (n && n % WIDTH == 0) /* impose WIDTH no. chars per-line */
std::cout << '\n';
std::cout << " " << s.at(n);
}
for (int i = 'a'; i < 'z'; i++) { /* output alphabet, skipping chars */
if (s.find((char)i) == std::string::npos) {
std::cout << " " << (char)i;
n++;
if (n && n % WIDTH == 0)
std::cout << '\n';
}
}
(注意:以上使用小写字符,但如果需要通过交换字符,您可以简单地更改为大写)
总而言之,你可以这样做:
#include <iostream>
#include <string>
#define WIDTH 5
int main (int argc, char **argv) {
std::string s = argc > 1 ? argv[1] : "help";
size_t n = 0;
for (size_t i = 0; i < s.size() - 1; i++) /* check for duplicate chars */
if (s.find(s.at(i), i+1) != std::string::npos) {
std::cerr << "error: '" << s.at(i) << "' duplcate char.\n";
return 1;
}
if (s.find('z') != std::string::npos) { /* check for 'z' in input */
std::cerr << "error: input contains 'z'.\n";
return 1;
}
for (; n < s.size(); n++) { /* output keyword, keep n as count */
if (n && n % WIDTH == 0) /* impose WIDTH no. chars per-line */
std::cout << '\n';
std::cout << " " << s.at(n);
}
for (int i = 'a'; i < 'z'; i++) { /* output alphabet, skipping chars */
if (s.find((char)i) == std::string::npos) {
std::cout << " " << (char)i;
n++;
if (n && n % WIDTH == 0)
std::cout << '\n';
}
}
}
(注意:添加任何周围的行或列标题,例如"0 1 2 3 4"
留给您)
示例使用/输出
使用默认的"help"
作为关键字:
$ ./bin/alphabet
h e l p a
b c d f g
i j k m n
o q r s t
u v w x y
以"absent"
为关键字:
$ ./bin/alphabet absent
a b s e n
t c d f g
h i j k l
m o p q r
u v w x y
或无效关键字:
$ ./bin/alphabet hello
error: 'l' duplcate char.
您可以随意使用 2D 数组,但这似乎会使关键字的输入和存储复杂化。 仔细检查一下,让我知道这样的事情是否可行,以及您是否还有其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.