[英]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.