C ++如何水平打印大字母?

时间:2019-03-15 02:47:26

标签: c++

例如,具有此功能的打印一个大的“ A”

void printA(){
    cout<<"0000000\n0     0\n0     0\n0000000\n0     0\n0     0\n";
}

0000000
0     0
0     0
0000000
0     0
0     0

我拥有所有字母,是否可以横向打印大字母?喜欢:

0000000  0000000  0000000
0     0  0     0  0     0
0     0  0     0  0     0
0000000  0000000  0000000
0     0  0     0  0     0
0     0  0     0  0     0

该程序的目标是使用垂直或水平方向这些大字母打印用户输入。

这是我程序的恢复版本:

void printA(){
    cout<<"0000000\n0     0\n0     0\n0000000\n0     0\n0     0\n"<<endl;
}

void printChar(char c){
    c = toupper(c);
    if (c=='A')
    printA();

}
int main(){

string sentence;
cout<<"enter sentence: ";
cin>> sentence;

for(unsigned int i = 0; i<sentence.length(); i++) {
    char c = sentence[i];
    if(c=='a')
    printA();
}
return 0;

}

我用“ \ n”使字母函数保存行,另一种选择是使它们像这样:

void printA(){
    cout << "0000000" << endl;
    cout << "0     0" << endl;
    cout << "0     0" << endl;
    cout << "0000000" << endl;
    cout << "0     0" << endl;
    cout << "0     0" << endl;
}

如果无法以我拥有的方式将它们彼此相邻打印,我将重写它们,但是请告诉我如何以水平形式打印它们:( 谢谢!

我有以下警报:

main.cpp:29:23: warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11
         for (char c : text)
                       ^
main.cpp:31:37: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
             std::cout << alphabet[c - 'A'][line] << "  ";

1 个答案:

答案 0 :(得分:3)

如果您可以像这样以其他格式(此处为2D数组)存储字符行:

const int LINES_PER_CHAR = 6;
const char* alphabet[][LINES_PER_CHAR] = 
{
    /* A */ { "0000000", "0     0", "0     0", "0000000", "0     0", "0     0" },
    /* B */ { "000 ", "0  0", "0  0", "000 ", "0  0", "0000" }
    ...
};

…,那么您可以打印如下字符:

void PrintBigString(const std::string& text)
{
    for (int line = 0; line < LINES_PER_CHAR; ++line)
    {
        for (char c : text)
        {
            std::cout << alphabet[c - 'A'][line] << "  ";
        }
        std::cout << std::endl;
    }
}

理想情况下,您应该添加一个健全性检查,以确保您具有字符串中当前字符的模板。

非C ++ 11编译器的变体:

void PrintBigString(const std::string& text)
{
    for (int line = 0; line < LINES_PER_CHAR; ++line)
    {
        for (int i = 0; i < text.size(); ++i)
        {
            std::cout << alphabet[text[i] - 'A'][line] << "  ";
        }
        std::cout << std::endl;
    }
}