time函数的运用,输出是没输换行,在流中,就什么的输不出,可以用清流函数,fflush(stdout)
代码
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<ctime>
#include<cstdlib>
using namespace std;
class show_time
{
public:
show_time()
{
while(1)
{
now_time=time(0);
strftime(buf, sizeof(buf), "%X",localtime(&now_time) );
if(str!=buf)
{
/*system("clear");
cout<<buf<<endl;*/
//cout<<" 33[2A"<<endl;//吧光标置于上一行
fflush(stdout);//清流没有输换行在流中,输不出
cout<<buf<<"
";//把光标置于行首
cout<<" 33[?25l";隐藏光标
}
str=buf;
}
}
private:
char buf[32];
string str;
time_t now_time;
};
int main(void)
{
show_time t;//每创建一个对象系统自动调用构造函数
return 0;
}
其中要用到几个关于光标移动的东西,和时间函数
#include <stdio.h>
#include <unistd.h>
// 清除屏幕
#define CLEAR() printf(" 33[2J")
// 上移光标
#define MOVEUP(x) printf(" 33[%dA", (x))
// 下移光标
#define MOVEDOWN(x) printf(" 33[%dB", (x))
// 左移光标
#define MOVELEFT(y) printf(" 33[%dD", (y))
// 右移光标
#define MOVERIGHT(y) printf(" 33[%dC",(y))
// 定位光标
#define MOVETO(x,y) printf(" 33[%d;%dH", (x), (y))
// 光标复位
#define RESET_CURSOR() printf(" 33[H")
// 隐藏光标
#define HIDE_CURSOR() printf(" 33[?25l")
// 显示光标
#define SHOW_CURSOR() printf(" 33[?25h")
//反显
#define HIGHT_LIGHT() printf(" 33[7m")
#define UN_HIGHT_LIGHT() printf(" 33[27m")
int main(int argc,char **argv)
{
printf(" 33[31mThe color,%s! 33[1m
","haha");
printf(" 33[31mThe color,%s! 33[4m
","haha");
printf(" 33[31mThe color,%s! 33[5m
","haha");
printf(" 33[31mThe color,%s! 33[7m
","haha");
printf(" 33[31mThe color,%s! 33[8m
","haha");
printf(" 33[31mThe color,%s! 33[0m
","haha");
printf(" 33[47;31mThe color,%s! 33[0m
","haha");
printf(" 33[47mThe color,%s! 33[0m
","haha");
sleep(2);
printf(" 33[47m%s! 33[5A
","up 5");
sleep(2);
printf(" 33[47m%s! 33[9B
","down 9");
sleep(2);
printf(" 33[47m%s! 33[19C
","right 19");
printf("right19");
sleep(2);
printf(" 33[47m%s! 33[10D
","left 10");
printf("left 10");
sleep(2);
printf(" 33[47m%s! 33[50;20H
","move to y:50,x 20");
printf("y50 x 20");
sleep(2);
printf(" 33[47m%s! 33[?25l
","hide cursor");
sleep(2);
printf(" 33[47m%s! 33[?25h
","sow cursor");
sleep(2);
printf(" 33[47m%s! 33[2J
","clear scleen");
sleep(2);
return 0;
}
printf(" 33[47;31mhello world 33[5m");
47是字背景颜色, 31是字体的颜色, hello world是字符串. 后面的 33[5m是控制码
.颜色代码:QUOTE:字背景颜色范围:
40--49 字颜色: 30--39
40: 黑 30: 黑
41: 红 31: 红
42: 绿 32: 绿
43: 黄 33: 黄
44: 蓝 34: 蓝
45: 紫 35: 紫
46: 深绿 36: 深绿
47: 白色 37: 白色
ANSI控制码:
QUOTE: