C语言中的滚动文本

发布于 2024-10-19 16:08:23 字数 1315 浏览 4 评论 0原文

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

text = "Hello, how are you"

输出应该是:

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

并且从右向左旋转。

到目前为止我已经编译了这个:

#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?

I would like to scroll a text continuously in the screen. For example,

text = "Hello, how are you"

The output should be:

Hello, how are you Hello, how are you
Hello, 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?

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

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

发布评论

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

评论(3

满地尘埃落定 2024-10-26 16:08:23

你的问题很有趣,我无法停止,直到我让它“工作”:

#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 单元格,无需不断清除整个屏幕。

我希望这有帮助。

Your problem was intriguing enough I couldn't stop until I'd made it "work":

#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;
}

Note that I've cheated :) (a) I duplicate the string to more than large enough to always fill the screen (twice the width) (b) I don't scroll the print location, I scroll the text that I ask to print (c) I just put a space in the original input string because it was easier than putting in a space through another mechanism.

Oh yeah, I removed the clear() call, it made the screen too messy to really see. We're over-writing the same max_x cells over and over though, no need to keep clearing the entire screen.

I hope this helps.

刘备忘录 2024-10-26 16:08:23

只需使用数组的索引,然后使用 putchar() 在正确的屏幕位置输出一个字符。尝试一下
看看这个链接也许会对你有帮助
http://www.dreamincode.net/code/snippet1964.htm

Just use an index into the array and output one character with putchar() at the correct screen location.Try it
take a look at this link may be it will help u out
http://www.dreamincode.net/code/snippet1964.htm

装纯掩盖桑 2024-10-26 16:08:23
 1. 

    /*this one is easy to understan*/


                #include <stdio.h> 
            #include <conio.h> 
            #include <string.h> 
            //library for sleep() function 
            #include <unistd.h> 
            void main() 
            { 
                int i,j,n,k; 

            //text to be srolled 
                  char t[30]="akhil is a bad boy"; 

            n=strlen(t); 

                for(i=0;i<n;i++) 
                 { 
                    printf("\n"); 

                   //loop for printing spaces 

                      for(j=20-i;j>0;j--) 
                         { 
                                printf(" "); 
                         } 

                  /*printing text by adding a                  character every time*/ 

                 for(k=0;k<=i;k++) 
                  { 
                      printf("%c",t[k]); 
                   } 

            // clearing screen after every iteration 

            sleep(1); 
            if(i!=n-1) 
            clrscr(); 
               }   
 }
 1. 

    /*this one is easy to understan*/


                #include <stdio.h> 
            #include <conio.h> 
            #include <string.h> 
            //library for sleep() function 
            #include <unistd.h> 
            void main() 
            { 
                int i,j,n,k; 

            //text to be srolled 
                  char t[30]="akhil is a bad boy"; 

            n=strlen(t); 

                for(i=0;i<n;i++) 
                 { 
                    printf("\n"); 

                   //loop for printing spaces 

                      for(j=20-i;j>0;j--) 
                         { 
                                printf(" "); 
                         } 

                  /*printing text by adding a                  character every time*/ 

                 for(k=0;k<=i;k++) 
                  { 
                      printf("%c",t[k]); 
                   } 

            // clearing screen after every iteration 

            sleep(1); 
            if(i!=n-1) 
            clrscr(); 
               }   
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文