以前学习c语言对各种输出格式都很熟悉,但是对c++确是一知半解。看了一些资料后发现挺有收获的。代码分享给大家。互相学习。
#include<iostream>
#include<iomanip>
#include<Windows.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
int a = 12345;
float b =4.567;
//左对齐宽是8个字节
cout <<setiosflags(ios::left)<< setw(8)<<a << endl;
//输出格式
cout<<setiosflags(ios::left)<<setw(10)<<setprecision(2)<<setiosflags(ios::fixed)<< b << endl;
//输出的进制数
cout << hex << a << endl;
cout << oct << a << endl;
cout << a << endl;
cout << hex << a << endl;
//设置填充符
cout << setfill('x') << setw(8) << a << endl;
//做时钟
int hour = 0, min = 0, sec = 0;
cout << setfill('0') << setw(2) << hour << ":" << setw(2)
<< min << ":" << setw(2) << sec << endl;
//Sleep(1000);//1秒延时
while (1)
{
Sleep(1000);
if (sec == 59)
{
sec = 0;
min += 1;
if (min == 59)
{
min = 0;
hour+= 1;
if (hour == 23)
{
hour = 0;
}
}
}
sec++;
//清屏函数
system("cls");
cout << setfill('0') << setw(2) << hour << ":" << setw(2)
<< min << ":" << setw(2) << sec << endl;
}
return 0;
}
学习小的知识也可以有一定的成就感,继续加油。。。。