gpt4 book ai didi

c - 如何在运行基于 graphics.h 的 C 程序时在终端中打印一些信息?

转载 作者:太空宇宙 更新时间:2023-11-04 03:13:05 25 4
gpt4 key购买 nike

这是我在 StackOverflow 中的第一个问题,请原谅我在提问时的错误。我正在努力学习使用 graphics.h C 编程语言库作为类(class)的一部分,我在使用 libgraph 时无法将一些信息打印到 Linux 终端。 . printf()函数打印 libgraph 中的给定信息窗口而不是终端,而我希望它将信息打印到 Linux 终端。这是我的代码和此代码屏幕截图的输出屏幕截图:

printf的DDA算法截图问题: DDA algorithm screenshot of printf problem

#include<stdio.h> 
#include<graphics.h>

//Function for finding absolute value
int abs (int n)
{
return ( (n>0) ? n : ( n * (-1)));
}

//DDA Function for line generation
void DDA(int X0, int Y0, int X1, int Y1)
{
// calculate dx & dy
int dx = X1 - X0;
int dy = Y1 - Y0;

// calculate steps required for generating pixels
int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

// calculate increment in x & y for each steps
float Xinc = dx / (float) steps;
float Yinc = dy / (float) steps;

// Put pixel for each step
float X = X0;
float Y = Y0;
for (int i = 0; i <= steps; i++)
{
printf("(%f,%f)",X,Y);
putpixel (X,Y,RED); // put pixel at (X,Y)
X += Xinc; // increment in x at each step
Y += Yinc; // increment in y at each step
delay(100); // for visualization of line-
// generation step by step
}
}

// Driver program
int main()
{
int gd = DETECT, gm;

// Initialize graphics function
initgraph (&gd, &gm, "");

int X0 = 2, Y0 = 2, X1 = 14, Y1 = 16;
DDA(2, 2, 100, 100);
getch();
return 0;
}

我想要的是printf在 Linux 终端中打印而不是 libgraph window 。

最佳答案

libgraph 的某些(如果不是全部)实现在其中一个头文件中包含以下行:

#define printf grprintf

所以,他们重新定义了printf使用宏,您不能使用它在 linux 终端中打印。但是因为他们没有重新定义其他输出函数,所以你可以使用 e. g.

    fprintf(stdout, "(%f,%f)", X, Y), fflush(stdout);    // or stderr instead of stdout

puts对于常量字符串。

或者,更简单,您可以 #undef printf#include<graphics.h>之后恢复正常行为。

关于c - 如何在运行基于 graphics.h 的 C 程序时在终端中打印一些信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54506294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com