显示 -/-\|/-的程序
==================================================================================
改自:
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) |