網課c++第一次作業,學到了iomanip庫文件裏的setw(),setfill()等函數,自己完成作業時搜着學到了Windows.h庫文件裏的sleep(),system("cls")兩個函數的用法,下面是最終實現的結果,醜到可以忽略代碼。。
代碼(含註釋)如下:
/* project:ugly clock
* auther :ugly mb
* dispaly:
* ------------
* ||xx:xx:xx||
* ------------
*/
#include <iostream>
#include <iomanip> //調用setw,setfill
#include <windows.h> //調用sleep,system
using namespace std;
int main(void)
{
/***************設定當前時間****************/
int hour=23,min=59,sec=50;
/*****************計時過程*****************/
while(1){
sec++;
if(sec==60){ //秒鐘記到60清0,分鐘進1位
sec=0;min++;
if(min==60){ //分鐘記到60清0,時鐘進3位
min=0;hour++;
if(hour==24){hour=0;}//時鐘記到24時清0
}
}
Sleep(1000);system("cls"); //sleep(x):延時x毫秒,system("cls"):清屏,這裏清上一秒的內容
/*****************顯示函數*****************/
cout<<"--------------"<<endl;
cout<<"|| "<<setfill('0')<<setw(2)<<hour<<":"<<setw(2)<<min<<":"<<setw(2)<<sec<<" ||"<<endl;
cout<<setw(14)<<setfill('-')<<"-"<<endl;
}
}
互動環節:求各位施捨我點改進建議,如有詳細方案,甚是感激