编程利用利用curses库编程开始

时间紧张,先记一笔,后续优化与完善。

curses库常用函数:

编程利用利用curses库编程开始

注意编译时要用这样的格式:gcc xxx.c -l curses -o xxx

第一个小例子:

include <stdio.h>
#include <curses.h> int main()
{
initscr(); clear();
move(10,20);
addstr("Hello, world");
move(LINES-1, 0);
refresh();
getch();
endwin();
return 0;
}

运行效果如下:

编程利用利用curses库编程开始

第二个小例子:

#include <stdio.h>
#include <curses.h> int main()
{
int i; initscr();
clear();
for (i = 0; i < LINES; i++)
{
move(i, i+1);
if (i%2 == 1)
standout(); //反白显示
addstr("Hello, world");
if (i%2 == 1)
standend(); //关闭反白显示
}
refresh();
getch();
endwin();
return 0;
}

运行效果:

编程利用利用curses库编程开始

第三个小例子:

    每日一道理
古人云:“海纳百川,有容乃大。”人世间,不可能没有矛盾和争吵,我们要以磊落的胸怀和宽容的微笑去面对它 。哈伯德也曾说过:“宽恕和受宽恕的难以言喻的快乐,是连神明都会为之羡慕的极大乐事。”让我们从宽容中享受快乐,从谅解中体会幸福吧!
#include <stdio.h>
#include <curses.h> int main()
{
int i; initscr();
clear();
for (i = 0; i < LINES; i++)
{
move(i, i+1);
if (i%2 == 1)
standout();
addstr("Hello, world");
if (i%2 == 1)
standend();
refresh();
sleep(1);
move(i, i+1); //move back
addstr(" "); //erase the line appear before
}
endwin();
return 0;
}

运行效果:字符串沿着对角线一行一下行的向下挪动

第四个小例子:

#include <stdio.h>
#include <curses.h> #define LEFTEDGE 10 /* 左边界 */
#define RIGHTEDGE 30 /* 右边界 */
#define ROW 10 int main()
{
char *message = "Hello";
char *blank = " ";
int dir = 1;
int pos = LEFTEDGE; initscr();
clear();
while (1)
{
move(ROW, pos);
addstr(message); /* draw string */
// move(LINES-1, COLS-1);
refresh(); /* show string */
sleep(1);
move(ROW, pos); /* move back */
addstr(blank); /* erase string */
pos += dir; /* advance position */
if (pos >= RIGHTEDGE) /* check for bounce */
dir = -1;
if (pos <= LEFTEDGE)
dir = 1;
}
return 0;
}

运行效果:在(ROW,LEFTEDGE)----(ROW,RIGHTEDGE)间往返挪动字符串

文章结束给大家分享下程序员的一些笑话语录: 警告
有一个小伙子在一个办公大楼的门口抽着烟,一个妇女路过他身边,并对他 说, “你知道不知道这个东西会危害你的健康?我是说, 你有没有注意到香烟 盒上的那个警告(Warning)?”
小伙子说,“没事儿,我是一个程序员”。
那妇女说,“这又怎样?”
程序员说,“我们从来不关心 Warning,只关心 Error”

---------------------------------
原创文章 By
编程和利用
---------------------------------

上一篇:.net开发笔记(十八) winform中的等待框


下一篇:Python: 如何写一个异常