我怎样才能在gcc中实现gotoxy()
我在ubuntu中使用gcc。so,我在终端中编译并执行。但在在线编程竞赛中,他们需要如图所示的输出。
为此,如果我使用 TURBOC,我可以使用 conio 获取它。 h 使用 gotoxy() 获得输出的螺旋格式。 但是在 Ubuntu 中,我怎样才能实现这一目标?
I am using gcc in ubuntu.so, I compile and execute in terminal. But In a online programming contest, they require the output as shown in diagram.
For that, if I use TURBOC I can get it using conio.h using gotoxy() to get spiral format of output. But in Ubuntu , How could I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
假设因为这是一场竞赛并且他们不想要像 ncurses 这样的依赖项,你可以尝试在内存中进行。
设置二维字符数组 - 行和列 - 比如说 24x80。编写您自己的
gotoxy()
版本,它将值分配到正确的单元格中。完成绘图后,打印出线条数组。Assuming because it is a contest and they don't want dependencies like ncurses you could try to do it in memory.
Set up 2 dimensional array of char - lines and columns - say 24x80. Write your own version of
gotoxy()
which assigns values into the proper cells. When done plotting, print out the array of lines.使用 ncurses 库。
这是一个示例,改编自 http://www.paulgriffiths.net/program/c /srcs/curhellosrc.html
更多信息请参见:http://invisible-island.net/ncurses/ncurses-intro.html#stdscr
Use the ncurses library.
Here's an example, adapted from http://www.paulgriffiths.net/program/c/srcs/curhellosrc.html
Further information is available here: http://invisible-island.net/ncurses/ncurses-intro.html#stdscr
除了 ANSI 转义序列之外,您可能还希望研究 ncurses:
http://www.gnu.org/s/ncurses/
这最终取决于运行程序的终端的功能,而不是实际的主机、语言或 图书馆。考虑将程序输出重定向到文件或打印机会发生什么情况。
conio.h
API 更多地与固定控制台有关,在类 Unix 系统中,您通常处理的终端可能更加多样化,例如可调整大小的 X 终端。Aside of ANSI escape sequences you might wish to investigate ncurses:
http://www.gnu.org/s/ncurses/
It all ultimately depends upon the capabilities of the terminal running the program, not the actual host, language, or library. Consider what happens redirecting program output to a file or to a printer.
conio.h
API is more to do with a fixed console, with Unix like systems you usual deal with terminals which can be more varied such as resizable X-Terminals.确定您需要多少行输出。分配一个“char *”数组,每行输出需要一个条目。当您放置数字时,使用“realloc()”来增加行的大小,并从旧端到新端填充空格(如果需要);然后将您的号码放在该行的正确位置(在内存中)。
在内存中构建字符串数组后;执行一个 for 循环来打印每一行(并释放您分配的内存)。
您不需要“gotoxy()”或任何东西来控制光标位置。
Determine how many lines of output you need. Allocate an array of "char *" with one entry per line of output needed. When you place a number use "realloc()" to increase the size of the line and fill from the old end to the new end with spaces (if necessary); then put your number at the right place in that line (in memory).
After you've build an array of string in memory; do a for loop that prints each line (and frees the memory you allocated).
You don't need "gotoxy()" or anything to control cursor position.
由于它还没有出现,我只想说一个使用 ANSI 转义序列作为 Steve-o 的示例提及。
我从此处获取它。
0x1B 是十进制 27 的十六进制,是 ESC 的 ASCII。转义序列以 it 开头
%m;%nf
将光标移动到第 n 行,第 m 列。使用 ANSI 转义序列“控制视频文本终端上的格式、颜色和其他输出选项"
Since it isn't here yet, I just wanted to say about an example using ANSI escape sequences as Steve-o mentioned.
I got it from here.
0x1B
is hexadecimal for 27 in decimal and is the ASCII for ESC. The escape sequences start with it%m;%nf
moves the cursor to row n, column m.The ANSI escape sequences are used "to control the formatting, color, and other output options on video text terminals"