gpt4 book ai didi

c - C语言中的滚动文本

转载 作者:行者123 更新时间:2023-11-30 18:15:56 27 4
gpt4 key购买 nike

我想在屏幕上连续滚动文本。例如,

text = "Hello, how are you"

输出应该是:

Hello, how are you Hello, how are youHello, how are you Hello, how are you

and rotating from right to left.

So far I have compiled this:

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()

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

char *text = argv[1];
char *text = "Hello, how are you";
int text_length;
int i, max_x, max_y;

// Get text length
text_length = strlen(text);

// Initialize screen for ncurses
initscr();
// Don't show cursor
curs_set(0);
// Get terminal dimensions
// getmaxyx(stdscr, max_y, max_x);
// Clear the screen
clear();

// Scroll text back across the screen

while (1) {
getmaxyx(stdscr, max_y, max_x);

if ((max_x - text_length) <= 1)
i = max_x;
else
i = (max_x - text_length);

for (i; i > 0; i--) {
clear();

mvaddstr(0, i, text);
refresh();
usleep(20000);
}
}
// Wait for a keypress before quitting
getch();

endwin();

return 0;
}

Can anybody help to change the code and do that?

最佳答案

你的问题太有趣了,我无法停止,直到我让它“起作用”:

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()
#include <stdlib.h> // For malloc()

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

char *text = "Hello, how are you? ";
char *scroll;
int text_length;

int i, max_x, max_y;

// Get text length
text_length = strlen(text);

// Initialize screen for ncurses
initscr();
// Don't show cursor
curs_set(0);
// Get terminal dimensions
// getmaxyx(stdscr, max_y, max_x);
// Clear the screen
clear();

getmaxyx(stdscr, max_y, max_x);
scroll = malloc(2 * max_x + 1);

for (i=0; i< 2*max_x; i++) {
scroll[i] = text[i % text_length];
}

scroll[2*max_x - 1]='\0';


// Scroll text back across the screen
for (i=0; i < 10000; i++) {

mvaddnstr(0,0,&scroll[i%max_x], max_x);
refresh();
usleep(20000);
}
// Wait for a keypress before quitting
getch();

endwin();

return 0;
}

请注意,我作弊了:) (a) 我将字符串复制到足够大以始终填充屏幕(宽度的两倍) (b) 我不滚动打印位置,而是滚动 我要求打印的文本 (c) 我只是在原始输入字符串中放置了一个空格,因为它比通过其他机制放置空格更容易。

哦,是的,我删除了 clear() 调用,它使屏幕太困惑而无法真正看到。不过,我们会一遍又一遍地覆盖相同的 max_x 单元格,无需不断清除整个屏幕。

我希望这会有所帮助。

关于c - C语言中的滚动文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5139892/

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