有cprintf的替代品吗?

发布于 2024-12-23 01:24:55 字数 1822 浏览 1 评论 0原文

我试图用星号“*”画一棵圣诞树,最后我能够画一棵。

问题是当我使用 textattr(130) 给它着色时“这个颜色是闪烁的绿色”& cprintf 函数中的树分散在整个屏幕上。

我画了一条垂直线来看看cprintf的效果。

除了 cprintf 之外,我可以使用任何其他方法,我只是希望树能够正确显示并着色。

我的代码是:

#include<stdio.h>
#include<conio.h>

int main()
 {
   clrscr();


   textattr(130);
   cprintf("*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*");

   cprintf(
 "\n                                                      *"
 "\n                                                     * *"
 "\n                                                    *   *"
 "\n                                                   *     *"
 "\n                                                  *       *"
 "\n                                                   **   **"
 "\n                                                  *       *"
 "\n                                                 *         *"
 "\n                                                *           *"
 "\n                                                 **       **"
 "\n                                                   *      *"
 "\n                                                  *        *"
 "\n                                                 *          *"
 "\n                                                *            *"
 "\n                                               ******   ******"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     ***");









 getch();
 return 0;
 }

我希望有人愿意帮助我解决这个问题。

I am trying to draw a Christmas tree using astrics "*" and at last I was able to draw one.

The problem is when I colored it using textattr(130) "this color is GREEN with blinking" & cprintf functions the tree was scattered all over the screen.

I drew a vertical line to see the effect of cprintf.

I am ok with any other methods other than cprintf, I just hope for the tree to be displayed right and colored.

My code is:

#include<stdio.h>
#include<conio.h>

int main()
 {
   clrscr();


   textattr(130);
   cprintf("*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*"
       "\n*");

   cprintf(
 "\n                                                      *"
 "\n                                                     * *"
 "\n                                                    *   *"
 "\n                                                   *     *"
 "\n                                                  *       *"
 "\n                                                   **   **"
 "\n                                                  *       *"
 "\n                                                 *         *"
 "\n                                                *           *"
 "\n                                                 **       **"
 "\n                                                   *      *"
 "\n                                                  *        *"
 "\n                                                 *          *"
 "\n                                                *            *"
 "\n                                               ******   ******"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     * *"
 "\n                                                     ***");









 getch();
 return 0;
 }

I hope someone would like to help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三寸金莲 2024-12-30 01:24:55

我拿了你的程序并参考了这个并提出了下面的程序。

#include <stdio.h>

#define RESET       0
#define BRIGHT      1
#define DIM         2
#define UNDERLINE   3
#define BLINK       4
#define REVERSE     7
#define HIDDEN      8

#define BLACK       0
#define RED         1
#define GREEN       2
#define YELLOW      3
#define BLUE        4
#define MAGENTA     5
#define CYAN        6
#define WHITE       7

void textcolor(int attr, int fg, int bg);
void reset_screen(void);
void print_tree(void);

int main()
{   
    textcolor(BRIGHT, RED, BLACK);  
    print_tree();
    getchar();
    reset_screen();
    return 0;
}

void textcolor(int attr, int fg, int bg)
{   
    char command[13];
    sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    printf("%s", command);
}

void reset_screen(void)
{
    system("reset");
    return;
}

void print_tree(void)
{
    printf("                                \n");
    printf("                                \n");
    printf("               *                \n");
    printf("               *                \n");
    printf("              * *               \n");
    printf("             *   *              \n");
    printf("            *     *             \n");
    printf("           *       *            \n");
    printf("            **   **             \n");
    printf("           *       *            \n");
    printf("          *         *           \n");
    printf("         *           *          \n");
    printf("          **       **           \n");
    printf("            *      *            \n");
    printf("           *        *           \n");
    printf("          *          *          \n");
    printf("         *            *         \n");
    printf("        ******   ******         \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("                                \n");
    printf("                                \n");
    printf("                                \n");
    printf("  M E R R Y  C H R I S T M A S  \n");
    printf("                                \n");
    printf("                                \n");

    return;
}

I took your program and referred to this and came up with the below program.

#include <stdio.h>

#define RESET       0
#define BRIGHT      1
#define DIM         2
#define UNDERLINE   3
#define BLINK       4
#define REVERSE     7
#define HIDDEN      8

#define BLACK       0
#define RED         1
#define GREEN       2
#define YELLOW      3
#define BLUE        4
#define MAGENTA     5
#define CYAN        6
#define WHITE       7

void textcolor(int attr, int fg, int bg);
void reset_screen(void);
void print_tree(void);

int main()
{   
    textcolor(BRIGHT, RED, BLACK);  
    print_tree();
    getchar();
    reset_screen();
    return 0;
}

void textcolor(int attr, int fg, int bg)
{   
    char command[13];
    sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
    printf("%s", command);
}

void reset_screen(void)
{
    system("reset");
    return;
}

void print_tree(void)
{
    printf("                                \n");
    printf("                                \n");
    printf("               *                \n");
    printf("               *                \n");
    printf("              * *               \n");
    printf("             *   *              \n");
    printf("            *     *             \n");
    printf("           *       *            \n");
    printf("            **   **             \n");
    printf("           *       *            \n");
    printf("          *         *           \n");
    printf("         *           *          \n");
    printf("          **       **           \n");
    printf("            *      *            \n");
    printf("           *        *           \n");
    printf("          *          *          \n");
    printf("         *            *         \n");
    printf("        ******   ******         \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("              * *               \n");
    printf("                                \n");
    printf("                                \n");
    printf("                                \n");
    printf("  M E R R Y  C H R I S T M A S  \n");
    printf("                                \n");
    printf("                                \n");

    return;
}
落在眉间の轻吻 2024-12-30 01:24:55

您可以使用 setcolor() 函数来更改文本的颜色。下面的示例程序解释了它的用法。

#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int color, midx, midy;
   char colname[35];

/* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

/* read result of initialization */
   errorcode = graphresult();
/* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
/* terminate with an error code */
      exit(1);
   }

   midx = getmaxx() / 2;
   midy = getmaxy() / 2;
   setcolor(getmaxcolor());

/* for centering text on the display */
   settextjustify(CENTER_TEXT, CENTER_TEXT);

/* get the current drawing color */
   color = getcolor();

/* convert color value into a string */
   itoa(color, colname, 10);
   strcat(colname, " is the current drawing color.");

/* display a message */
   outtextxy(midx, midy, colname);

/* clean up */
   getch();
   closegraph();
   return 0;
}

You can use the setcolor() function in order to change the color of your text. The following sample program explains its usage.

#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int color, midx, midy;
   char colname[35];

/* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");

/* read result of initialization */
   errorcode = graphresult();
/* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
/* terminate with an error code */
      exit(1);
   }

   midx = getmaxx() / 2;
   midy = getmaxy() / 2;
   setcolor(getmaxcolor());

/* for centering text on the display */
   settextjustify(CENTER_TEXT, CENTER_TEXT);

/* get the current drawing color */
   color = getcolor();

/* convert color value into a string */
   itoa(color, colname, 10);
   strcat(colname, " is the current drawing color.");

/* display a message */
   outtextxy(midx, midy, colname);

/* clean up */
   getch();
   closegraph();
   return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文