Chinaunix首页 | 论坛 | 博客
  • 博客访问: 918376
  • 博文数量: 132
  • 博客积分: 9976
  • 博客等级: 中将
  • 技术积分: 1781
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-30 20:40
文章分类

全部博文(132)

文章存档

2013年(1)

2011年(1)

2010年(15)

2009年(77)

2008年(36)

2007年(2)

我的朋友

分类: C/C++

2009-06-26 16:18:11

显示 -/-\|/-的程序

==================================================================================
改自:

1. 使用ncurses库

代码和编译
cjash@chjianash:~\> cat cc.c
/*
* rotate.c
*/
#include   
#define     ROW 5
#define     COL 10
void show(char *);

int
main(void)
{
    char        *sign_1 = "|";
    char        *sign_2 = "/";
    char        *sign_3 = "-";
    char        *sign_4 = "\\";
       
    initscr();          /* turn on curses */
    clear();            /* clear screen for drawing */
    while(1) {          /* in a loop */
        show(sign_1);   /* draw sign_1 */
        show(sign_2);   /* draw sign_2 */
        show(sign_3);   /* draw sign_3 */
        show(sign_4);   /* draw sign_4 */
    }
    endwin();           /* reset the tty etc */
}


void show(char *sign)
{
/*
*  draw a sign and wait 1 second
*/
    move(ROW, COL);             /* move cursor for drawing */
    addstr(sign);               /* add a string */
    move(LINES-1, 0);           /* move cursor */
    refresh();                  /* refresh window */
    usleep(8000);                   /* sleep */
}
cjash@chjianash:~\> gcc -o cc cc.c -lncurses
cjash@chjianash:~\> ./cc
cjash@chjianash:~\>


2. 使用printf

代码,编译和运行效果
cjash@chjianash:~\> cat aa.c

/******************************************************************************
 * Copyright (C) 2006 by Nie Dapeng
 *
 * File: example_1.c
 *
 * Date: 2006-12-4
 *
 * Author: Nie Dapeng,
 *
 * Version: 0.1
 *
 * Descriptor:
 *
 * Modified:
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
  System header files
------------------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include

/*-----------------------------------------------------------------------------
  Local header files
------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
  Data structures
------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
  Global variables and constants
------------------------------------------------------------------------------*/
#define STEP      100

/*-----------------------------------------------------------------------------
  Functions
------------------------------------------------------------------------------*/

int main(int argc, char *argv[])
{
        int i;
           int j;
           char var;

        for (i = 1; i <= STEP; i++) {
                putchar('\r');
                switch(i % 4) {
                case 1:
                        var = '|';
                        break;
                case 2:
                        var = '/';
                        break;
                case 3:
                        var = '-';
                        break;
                case 0:
                        var = '\\';
                        break;
                }
                putchar(var);

                for (j = 1; j <= i; j++) {
                        printf("=");
                }

                for (j = 1; j <= STEP - i; j++) {
                        printf(" ");
                }

                printf("| %d%%", i);
                fflush(stdout);
                sleep(1);
        }

        exit(EXIT_SUCCESS);
}
cjash@chjianash:~\> gcc -o aa aa.c
cjash@chjianash:~\> ./aa
|=                                                                                              /==                                                                                             -===                                                                                            \====                                                                                           |=====                                                                                          /======                                                                                         -=======                                                                                        \========                                                                                       |=========                                                                                      /==========                                                                                     -===========                                                                                    \============                                                                                   |=============                                                                                  /==============                                                                                      | 14%^C
cjash@chjianash:~\>


3. 对 方法二修改的

代码,编译和运行效果

cjash@chjianash:~\> cat bb.c

/******************************************************************************
 * Copyright (C) 2006 by Nie Dapeng
 *
 * File: example_1.c
 *
 * Date: 2006-12-4
 *
 * Author: Nie Dapeng,
 *
 * Version: 0.1
 *
 * Descriptor:
 *
 * Modified:
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
  System header files
------------------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include

/*-----------------------------------------------------------------------------
  Local header files
------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
  Data structures
------------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
  Global variables and constants
------------------------------------------------------------------------------*/
#define STEP      100

/*-----------------------------------------------------------------------------
  Functions
------------------------------------------------------------------------------*/

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

        for (i = 1; i <= STEP; i++) {
            int j;
            char var;
                putchar('\r');
                switch(i % 4) {
                case 1:
                        var = '|';
                        break;
                case 2:
                        var = '/';
                        break;
                case 3:
                        var = '-';
                        break;
                case 0:
                        var = '\\';
                        break;
                }
                putchar(var);

                for (j = 1; j <= i; j++) {
                        printf("=");
                }
                printf("| %d%%", i);
                fflush(stdout);
                sleep(1);
                for (j = 0; j <= i; j++) {
                        printf("\b");
                }
        }

        exit(EXIT_SUCCESS);
}
cjash@chjianash:~\> gcc -o bb bb.c
cjash@chjianash:~\> ./bb
|=====================================| 37%^C
cjash@chjianash:~\>

阅读(723) | 评论(0) | 转发(0) |
0

上一篇:Fedora 12 开发计划

下一篇:kermit connect log

给主人留下些什么吧!~~