c++ - 如何使用关键字打印出字母表的二维数组?
问题描述
所以我的任务是打印出一个 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
任何帮助,将不胜感激!!非常感谢!
解决方案
为什么不使用std::string
来存储您的关键字?这提供.find()
了检查是否包含字符。您不必担心 2D 数组,您所需要的只是您的std::string
和一个计数器,您可以'\n'
在每 5个字符之后输出一个 5x5 网格中的输出。
您如何处理具有重复字符的关键字?他们只是被认为是无效的关键字吗?如果它包含被排除的字符"wizard"
,那么它同样会是一个无效的关键字吗?'z'
(我想两者都必须是,否则你将无法填写'y'
你的 5x5 网格)
话虽如此,您可以简单地将关键字作为程序的第一个参数,并保留输出字符数的计数器,然后执行以下简单操作:
#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"
留给您)
示例使用/输出
使用 default"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"
as 关键字:
$ ./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 数组,但这似乎会使关键字的输入和存储变得复杂。仔细检查一下,让我知道这样的事情是否可行,以及您是否还有其他问题。
推荐阅读
- javascript - 使用反应路由器时如何在页面上取消渲染元素
- react-native - react原生屏幕的布局如何设计?
- linux - 基础文件更改时如何同步环回设备
- linux - 从失败的脚本执行脚本
- python - ImportError: No module named future 尽管已经安装
- excel - VBA 从一个 Word Doc 复制到另一个 Excel
- reactjs - CRA 应用程序在生产构建后不运行?
- python - MySQL/Python 中的“TypeError:格式字符串的参数不足”
- php - 无法移动上传的文件
- database - 工作流数据库结构