简体   繁体   English

如何修复艺术 ascii ,我点击一个字母没问题,但输入一个阶段艺术出现错误

[英]how to fix art ascii , i tap one letter is fine, but type a phase the art come out wrong

I need help is this script to convert art ascii我需要帮助是这个脚本来转换艺术 ascii

if I type each letter separately, everything is fine, they come out as they should but, if I try to type a phrase, or more than one letter, the art is all wrong如果我分别输入每个字母,一切都很好,它们应该会出现,但是,如果我尝试输入一个短语或多个字母,那么艺术就错了

#define BUFF_SIZE 256
#include <stdio.h>
#include <string.h>
void print(char* string) {
    int len = strlen(string);
    int ind, ctr;
    char ptr;
    for(ind = 0; ind < 3; ++ind) {
        for(ctr = 0; ctr < len; ++ctr) {
            ptr = string[ctr];
            switch(ptr) {
            case 'A':
                switch(ind) {
                case 0:
                    printf(" /\\ ");
                    break;
                case 1:
                    printf("/~~\\ ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'B':
                switch(ind) {
                case 0:
                    printf(" __ ");
                    break;
                case 1:
                    printf("|__)");
                    break;
                case 2:
                    printf("|__)");
                    break;

                default:
                    break;
                }
                break;
            case 'C':
                switch(ind) {
                case 0:
                    printf(" __ ");
                    break;
                case 1:
                    printf("/  ` ");
                    break;
                case 2:
                    printf("\\__, ");
                    break;
                default:
                    break;
                }
                break;
            case 'D':
                switch(ind) {
                case 0:
                    printf(" __  ");
                    break;
                case 1:
                    printf("|  \\ ");
                    break;
                case 2:
                    printf("|__/ ");
                    break;
                default:
                    break;
                }
                break;
            case 'E':
                switch(ind) {
                case 0:
                    printf(" ___ ");
                    break;
                case 1:
                    printf("|__  ");
                    break;
                case 2:
                    printf("|___ ");
                    break; 

                default:
                    break;
                }
                break;
            case 'F':
                switch(ind) {
                case 0:
                    printf(" ___ ");
                    break;
                case 1:
                    printf("|__  ");
                    break;
                case 2:
                    printf("|    ");
                    break;

                default:
                    break;
                }
                break;
            case 'G':
                switch(ind) {
                case 0:
                    printf(" __   ");
                    break; 
                case 1:
                    printf("/ _`  ");
                    break;
                case 2:
                    printf("\\__> ");
                    break;

                default:
                    break;
                }
                break;
            case 'H':
                switch(ind) {
                case 0:
                    printf("|__| ");
                    break;
                case 1:
                    printf("|  | ");
                    break;
                case 2:
                    printf("     ");
                    break;
                default:
                    break;
                }
                break;
            case 'I':
                switch(ind) {
                case 0:
                    printf(" | ");
                    break;
                case 1:
                    printf(" | ");
                    break;
                case 2:
                    printf("   ");
                    break;  
                default:
                    break;
                }
                break;
            case 'J':
                switch(ind) {
                case 0:
                    printf("    | ");
                    break;
                case 1:
                    printf("\\__/ ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'K':
                switch(ind) {
                case 0:
                    printf("|__/ ");
                    break;
                case 1:
                    printf("|  \\ ");
                    break;
                case 2:
                    printf("      ");
                    break; 
                default:
                    break;
                }
                break;
            case 'L':
                switch(ind) {
                case 0:
                    printf("|    ");
                    break;
                case 1:
                    printf("|___ ");
                    break;
                case 2:
                    printf("     ");
                    break;
                default:
                    break;
                }
                break;
            case 'M':
                switch(ind) {
                case 0:
                    printf("|\\/|");
                    break;
                case 1:
                    printf("|  | ");
                    break;
               case 2:
                    printf("     ");
                    break;
                default:
                    break;
                }
                break;
            case 'N':
                switch(ind) {
                case 0: 
                    printf("|\\ | ");
                    break;
                case 1:
                    printf("| \\| ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'O':
                switch(ind) {
                case 0:
                    printf(" __  ");
                    break; 
                case 1:
                    printf("/  \\ ");
                    break;
                case 2:
                    printf("\\__/ ");
                    break;

                default:
                    break;
                }
                break;
            case 'P':
                switch(ind) {
                case 0:
                    printf(" __ ");
                    break;
                case 1:
                    printf("|__) ");
                    break;
                case 2:
                    printf("|    ");
                    break;
                default:
                    break;
                }
                break;
            case 'Q':
                switch(ind) {
                case 0:
                    printf(" __   ");
                    break;
                case 1:
                    printf("/  \\ ");
                    break;
                case 2:
                    printf("\\__X ");
                    break;

                default:
                    break;
                }
                break;
            case 'R':
                switch(ind) {
                case 0:
                    printf(" __  ");
                    break;
                case 1:
                    printf("|__) ");
                    break;
                case 2:
                    printf("|  \\ ");
                    break;

                default:
                    break;
                }
                break;
            case 'S':
                switch(ind) {
                case 0:
                    printf(" __ ");
                    break;
                case 1:
                    printf("/__` ");
                    break;
                case 2:
                    printf(".__/ ");
                    break;

                default:
                    break;
                }
                break;
            case 'T':
                switch(ind) {
                case 0:
                    printf(" ___ ");
                    break;
                case 1:
                    printf("  | ");
                    break;
                case 2:
                    printf("  | ");
                    break;

                default:
                    break;
                }
                break;
            case 'U':
                switch(ind) {
                case 0:
                    printf("|  | ");
                    break;
                case 1:
                    printf("\\__/ ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'V':
                switch(ind) {
                case 0:
                    printf("\\  / ");
                    break;
                case 1:
                    printf(" \\/  ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'W':
                switch(ind) {
                case 0:
                    printf(" |  |  ");
                    break;
                case 1:
                    printf(" |/\\| ");
                    break;
                case 2:
                    printf("       ");
                    break;
                default:
                    break;
                }
                break;
            case 'X':
                switch(ind) {
                case 0:
                    printf(" \\_/ ");
                    break;
                case 1:
                    printf(" / \\ ");
                    break;
                case 2:
                    printf("      ");
                    break;
                default:
                    break;
                }
                break;
            case 'Y':
                switch(ind) {
                case 0:
                    printf(" \\ / ");
                    break;
                case 1:
                    printf("  |  ");
                    break;
                case 2:
                    printf("     ");
                    break;
                default:
                    break;
                }
                break;
            case 'Z':
                switch(ind) {
                case 0:
                    printf("  __  ");
                    break;
                case 1:
                    printf("  /  ");
                    break;
                case 2:
                    printf("  /_  ");
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
        }
        printf("\n");
    }
    printf("\n");
}
void print(char* string);

int main(int argc, char *argv[]) {

    if (argc > 1) {

        char text[BUFF_SIZE] = {0};

        int n = 1;
        while (n < argc) {
            strcat(text, argv[n]);
            strcat(text, " ");
            n++;
        }

        n = 0;
        char ptr;
        while(text[n]) {
            ptr = text[n];
            text[n] = (toupper(ptr));
            n++;
        }

        print(text);

    } else {
        printf("\n Digite o texto a ser convertido \n");
        char text[BUFF_SIZE];
        fgets(text, sizeof(text), stdin);
        int i = 0;
        char ptr;
        while(text[i]) {
            ptr = text[i];
            text[i] = (toupper(ptr));
            ++i;
        }
        print(text);
    }
    return(0);
}

I need help is this script to convert art ascii我需要帮助是这个脚本来转换艺术 ascii

if I type each letter separately, everything is fine, they come out as they should but, if I try to type a phrase, or more than one letter, the art is all wrong gostaria de ajuda para tentar arrumar este erro que esta acontecendo如果我分别输入每个字母,一切都很好,它们应该会出现,但是,如果我尝试输入一个短语或多个字母,则艺术都是错误的 gostaria de ajuda para tentar arrumar este erro que esta acontecendo

You have two kinds of alignment issues :您有两种对齐问题:

horizontal alignment水平对齐

For example, the letter 'A' prints 4, 5 and 6 characters respectively for each of the 3 scan lines.例如,字母“A”为 3 条扫描线中的每条线分别打印 4、5 和 6 个字符。 This will cause the next character to come out garbled :这将导致下一个字符出现乱码:

 /\  __ 
/~~\ |__)
      |__)

To fix that, print the exact same amount of characters for each of the letters by appending spaces at the end of each printf as needed.要解决这个问题,请根据需要在每个printf的末尾附加空格,为每个字母打印完全相同数量的字符。

Eg.例如。 for the letter 'A', print 6 characters each :对于字母“A”,每个打印 6 个字符:

        case 'A':
            switch(ind) {
            case 0:
                printf(" /\\   ");   /* <--- added 2 spaces */
                break;
            case 1:
                printf("/~~\\  ");   /* <--- added 1 space */
                break;
            case 2:
                printf("      ");
                break;
            default:
                break;
            }
            break;

Do the same for all characters.对所有字符执行相同操作。

vertical alignment垂直对齐

For example, 'A' followed by 'B' will look weird, since for the former only the top 2 scan lines are used, while for the latter only really the bottom 2 :例如, 'A' 后跟 'B' 看起来很奇怪,因为前者只使用顶部 2 条扫描线,而后者只使用底部 2 条:

 /\    __ 
/~~\  |__)
      |__)

To fix that, choose a vertical alignment base (eg. the bottom scan line), and start all your letters there.要解决这个问题,请选择垂直对齐基准(例如底部扫描线),然后从那里开始所有字母。

Eg.例如。 for the letter 'A', use the bottom 2 scan lines instead of the top 2 :对于字母“A”,使用底部 2 条扫描线而不是顶部 2 条:

        case 'A':
            switch(ind) {
            case 0:
                printf("      ");    /* <--- moved from case 2 */
                break;
            case 1:
                printf(" /\\   ");   /* <--- moved from case 0 */
                break;
            case 2:
                printf("/~~\\  ");   /* <--- moved from case 1 */
                break;
            default:
                break;
            }
            break;

Resulting in :导致 :

       __ 
 /\   |__)
/~~\  |__)

Do the same for all characters.对所有字符执行相同操作。

In this situation you would be well advised to play with your code formatting a little.在这种情况下,建议您稍微调整一下代码格式。 It's one of those times where some creative layout will make the code clearer and help you spot some of the bugs @SanderDeDycker has already highlighted:这是一些创造性布局将使代码更清晰并帮助您发现@SanderDeDycker 已经突出显示的一些错误的时代之一:

        case 'A': switch(ind) {
            case 0: printf("      "); break;
            case 1: printf(" /\\  "); break;
            case 2: printf("/~~\\ "); break;
        } break;
        case 'B': switch(ind) {
            case 0: printf(" __  "); break;
            case 1: printf("|__) "); break;
            case 2: printf("|__) "); break;
        } break;
        ...

You might also consider handling lower-case letters in the same switch:您还可以考虑在同一个开关中处理小写字母:

...
   case 'A': case 'a':    // catch upper- and lower-case 'A'
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM