【问题标题】:Printing Ascii Art letters in console在控制台中打印 Ascii Art 字母
【发布时间】:2018-08-22 07:53:45
【问题描述】:

我在制作这个程序时遇到了一些问题。我需要做的是我班上的一个项目。我需要做的与此类似:http://patorjk.com/software/taag/

我可以一次打印一个字母,但问题是我不能打印像“ABC”这样的东西,它将它们全部打印在同一个地方。因此,如果你们中的任何一个人可以帮助我并向我展示我应该如何进行打印,那就太好了。

#include <iostream>
#include <string.h>
#include<windows.h>
#include <stdio.h>
using namespace std;

char A[4][12]={
"    //   ",
"  // //  ",
" /////// ",
"//     //"};

char B[5][12]={
" /////   ",
" //  //  ",
" /////   ",
" //  //  ",
" /////   "};

char C[5][12]={
"  /////  ",
" //      ",
" //      ",
" //      ",
"  /////  "};

char D[5][12]={
" /////   ",
" //  //  ",
" //   // ",
" //  //  ",
" /////   "};

char E[5][12]={
" /////   ",
" //      ",
" ////    ",
" //      ",
" /////   "};

char F[5][12]={
" /////   ",
" //      ",
" ////    ",
" //      ",
" //      "};

char G[5][12]={
"  /////  ",
" //      ",
"//  ///  ",
" //   // ",
"  /////  "};

char H[5][12]={
" //   // ",
" //   // ",
" /////// ",
" //   // ",
" //   // "};

char I[5][12]={
"    **   ",
"    //   ",
"    //   ",
"    //   ",
"    //   "};

char J[5][12]={
"      // ",
"      // ",
"      // ",
"  //  // ",
"   ////  "};

char K[5][12]={
" //  //  ",
" // //   ",
" ///     ",
" // //   ",
" //  //  "};



int main()
{
    int x, k = 0;
    char a[999];
    cin.get(a, 999);
    x = strlen(a);
    while (k < 6)
    {
        for (int i = 0; i <= x; i++)
        {
            if (a[i] == 'A')
                cout << A[k] << endl;
            if (a[i] == 'B')
                cout << B[k] << endl;
            if (a[i] == 'C')
                cout << C[k] << endl;
            if (a[i] == 'D')
                cout << D[k] << endl;
            if (a[i] == 'E')
                cout << E[k] << endl;
            if (a[i] == 'F')
                cout << F[k] << endl;
            if (a[i] == 'G')
                cout << G[k] << endl;
            if (a[i] == 'H')
                cout << H[k] << endl;
            if (a[i] == 'I')
                cout << I[k] << endl;
            if (a[i] == 'J')
                cout << J[k] << endl;
            if (a[i] == 'K')
                cout << K[k] << endl;
        }
        k = k + 1;
   }

    return 0;
}

【问题讨论】:

  • Stack Overflow 的主要目标是为未来的程序员留下一个关于编程问题的问答库。为了做到这一点,这个问题需要能够与网站一样长。这意味着到场外资源的链接使用有限。一旦pastebin 垃圾收集、改组其链接或成为成人娱乐网站,您的代码就会丢失并且问题变得毫无用处。我已将您的代码粘贴到其中,但也会因缺少minimal reproducible example 而投票关闭。这是一个你应该能够用更少的代码隔离和解决的错误。
  • 您在每个输出语句之后添加换行符,而不是在 while (k &lt; 6) 循环的末尾。你期待什么?您的循环还会额外运行一次,并由于超出范围的数组访问而导致未定义的行为。您应该使用std::strings 而不是C 风格的字符串,并且您可以使用good book。此外,一个 switch case 会更好,而不是一堆 if 语句(至少使用else if)。

标签: c++ text printing char ascii


【解决方案1】:

我编写了一个非常相似的程序,通过打印散列来打印大尺寸的整数。 这是程序:

/**
Author:- Mayukh Datta
**/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define H 7
#define W 8 //one extra room in the char array is required for storing '\0'
void main()
{
    char num[11];  //here too one extra room is needed for the '\0'
    char c;  //for option
    int i, j, k;
    //declaring char 2D arrays and initializing with hash-printed digits
    char zero[H][W]={" ##### ", //H=0
                     " #   # ", //H=1
                     " #   # ", //H=2
                     " #   # ", //H=3
                     " #   # ", //H=4
                     " #   # ", //H=5
                     " ##### "},//H=6

         one[H][W]={"   #   ",
                    "  ##   ",
                    "   #   ",
                    "   #   ",
                    "   #   ",
                    "   #   ",
                    " ##### "},

         two[H][W]={" ##### ",
                    "     # ",
                    "     # ",
                    " ##### ",
                    " #     ",
                    " #     ",
                    " ##### "},

         three[H][W]={" ##### ",
                      "     # ",
                      "     # ",
                      " ##### ",
                      "     # ",
                      "     # ",
                      " ##### "},

         four[H][W]={" #     ",
                     " #   # ",
                     " #   # ",
                     " ##### ",
                     "     # ",
                     "     # ",
                     "     # "},

         five[H][W]={" ##### ",
                     " #     ",
                     " #     ",
                     " ##### ",
                     "     # ",
                     "     # ",
                     " ##### "},

         six[H][W]={" ##### ",
                    " #     ",
                    " #     ",
                    " ##### ",
                    " #   # ",
                    " #   # ",
                    " ##### "},

         seven[H][W]={" ##### ",
                      "     # ",
                      "     # ",
                      "  #### ",
                      "     # ",
                      "     # ",
                      "     # "},

         eight[H][W]={" ##### ",
                      " #   # ",
                      " #   # ",
                      " ##### ",
                      " #   # ",
                      " #   # ",
                      " ##### "},

         nine[H][W]={" ##### ",
                     " #   # ",
                     " #   # ",
                     " ##### ",
                     "     # ",
                     "     # ",
                     "     # "};

    do
    {
        printf("Enter a number upto 10 digits:- ");
        fflush(stdin);
        gets(num);
        if(strlen(num)>10)
           printf("\nYou must enter a number upto 10 digits.\nTry again!\n");
        else
        {
            printf("\n");

            k=1;
            j=0;  //controls H of each digit
            while(k<=7)  //controls height
            {
                for(i=0;i<strlen(num);i++)  //reads each digit
                {
                    if(num[i]=='0')
                        printf("%s", zero[j]);
                    else if(num[i]=='1')
                        printf("%s", one[j]);
                    else if(num[i]=='2')
                        printf("%s", two[j]);
                    else if(num[i]=='3')
                        printf("%s", three[j]);
                    else if(num[i]=='4')
                        printf("%s", four[j]);
                    else if(num[i]=='5')
                        printf("%s", five[j]);
                    else if(num[i]=='6')
                        printf("%s", six[j]);
                    else if(num[i]=='7')
                        printf("%s", seven[j]);
                    else if(num[i]=='8')
                        printf("%s", eight[j]);
                    else if(num[i]=='9')
                        printf("%s", nine[j]);
                }
                printf("\n");
                k++;
                j++;
            }
        }
        printf("\nEnter Y to continue:- ");
        fflush(stdin);
        scanf("%c", &c);
    }while(c=='Y'||c=='y');
}

了解循环在我的程序中是如何工作的。 链接:http://www.thecoducer.com/2017/08/printing-the-number-in-large-size.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2020-01-14
    • 2012-05-02
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多