我正在尝试为有趣的应用程序生成ascii艺术文本。从FIGLET,我得到了ASCII模式。我在printf
声明中使用该模式来打印字母。下面是我从FIGLET得到了模式的截图:在C生成Ascii艺术文本
这里是代码片段我用它来打印答:
printf(" _/_/\n _/ _/\n _/_/_/_/\n _/ _/\n_/ _/\n");
现在,我从用户输入文字,用ASCII艺术展示它。当我用printf,我只能垂直生成它:
但我需要做的水平ASCII艺术。怎么做 ?
我正在尝试为有趣的应用程序生成ascii艺术文本。从FIGLET,我得到了ASCII模式。我在printf
声明中使用该模式来打印字母。下面是我从FIGLET得到了模式的截图:在C生成Ascii艺术文本
这里是代码片段我用它来打印答:
printf(" _/_/\n _/ _/\n _/_/_/_/\n _/ _/\n_/ _/\n");
现在,我从用户输入文字,用ASCII艺术展示它。当我用printf,我只能垂直生成它:
但我需要做的水平ASCII艺术。怎么做 ?
是的,这是一个众所周知的问题。上次我解决这个问题的方法是为每行使用一个数组,并分别渲染每个字母。
首先,我会代表数组中的每个字母。例如您的A将是这样的:(实际上将被使用的二维阵列,其中每个字母是在不同的指数)
char* letter[8];
letter[0] = " _/_/ ";
letter[1] = " _/ _/";
etc.
实际呈现将在阵列中,以及,沿线的东西:
char * render [8];
然后使用连接来构建每一行。一个简单的嵌套for循环应该做的伎俩:
for each line, i to render (i.e the height of the letters)
for each letter, j
concatenate line i of letter j to the to char* i in the array
最后循环,虽然数组和打印每一行。实际上,你可以跳过渲染数组,只需打印每一行而不用回车。像下面的东西浮现在脑海:
for each line, i to render : // (i.e the height of the letters)
for each letter, j {
output line i of letter j
}
output a carriage return
}
(我的C是有点生疏,从那里“伪”代码不过,我觉得我的逻辑是合理的。)
而不是打印一点整字符作为一个字符串,将字符分成多个字符串 - 构成字符的每一行文本都有一个字符。然后打印每个字符的第一行,换行,然后打印每个字符的第二行等。您需要用空格填充行以确保它们都是正确的宽度。
很高兴看到你喜欢Figlet。您显示的Figlet小部件实际上包含一个生成ASCII输出的命令行工具。 http://www.figlet.org/ 它也可以很容易地与brew http://mxcl.github.com/homebrew/一起安装。
认为您可以通过popen(..)
或类似的方法轻松地从您自己的程序中捕获其输出。
您可以尝试类似如下:
注:显然,下面的很多事情,如内存重新分配,错误检查,不完整代码等我们的想法是给你一个提示缺少!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define ROW 4
#define COL 8
#define CHAR_INDEX_MAX 26
enum alph_enum {A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z};
typedef struct _alphabets {
char *line[ROW];
}alphabet;
alphabet chars[26];
init_a(enum alph_enum letter)
{
int i;
for (i=0 ; i<ROW ; i++) {
chars[letter].line[i] = (char *) malloc(COL);
if (chars[letter].line[i] == NULL) {
printf("memory allocation failed \n");
return;
}
}
switch (letter) {
/**/
case H:
strcpy(chars[letter].line[0], "| |");
strcpy(chars[letter].line[1], "|_______|");
strcpy(chars[letter].line[2], "| |");
strcpy(chars[letter].line[3], "| |");
break;
case E:
strcpy(chars[letter].line[0], "|-------");
strcpy(chars[letter].line[1], "|_______");
strcpy(chars[letter].line[2], "| ");
strcpy(chars[letter].line[3], "|_______");
break;
case L:
strcpy(chars[letter].line[0], "| ");
strcpy(chars[letter].line[1], "| ");
strcpy(chars[letter].line[2], "| ");
strcpy(chars[letter].line[3], "|_______");
break;
/* for all the other alphabets */
}
return;
}
print_str(char word[])
{
int i, j;
printf("\n");
for (i=0; i<ROW; i++) {
for (j=0 ; j<strlen(word) ; j++) {
printf("%s", chars[word[j] % 'A'].line[i]);
}
printf("\n");
}
printf("\n");
return;
}
int main(void)
{
init_a(H);
init_a(E);
init_a(L);
/* print_str("HELLO"); */
print_str("HELL");
return 0;
/* free the memory for HEL here */
}
输出将是如下:
> gcc test.c -o print
> print
| ||-------| |
|_______||_______| |
| || | |
| ||_______|_______|_______
>
TODO:
希望这有助于!
谢谢,让我试试它..我认为雅科凡告诉类似的想法。 – utsabiem
(作为一个方面,使用上述作为基础,您可以实际构建ascii艺术字体文件并根据需要将它们加载到数组中。) –
谢谢,我想我明白了...让我试试看... – utsabiem
它使用二维数组...但在空间填充面临一些问题..希望我会尽快解决它.. – utsabiem