0

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

text = "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?

4

3 回答 3

1

你的问题很有趣,直到我让它“工作”后我才停下来:

#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) 我不滚动打印位置,而是滚动我要求的文本print (c) 我只是在原始输入字符串中添加了一个空格,因为它比通过另一种机制输入空格更容易。

哦,是的,我删除了clear()电话,它使屏幕太乱而无法真正看到。不过,我们一遍又一遍地重写相同的max_x单元格,无需继续清除整个屏幕。

我希望这有帮助。

于 2011-02-28T09:27:27.030 回答
0

只需在数组中使用索引并在正确的屏幕位置使用 putchar() 输出一个字符。试试看一下这个链接可能会帮助你 http://www.dreamincode.net/code/snippet1964。 htm

于 2011-02-28T09:19:26.963 回答
-3
 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(); 
               }   
 }
于 2017-06-20T13:49:35.750 回答