C++實現(xiàn)秒表功能
本文實例為大家分享了C++實現(xiàn)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
抽象出CLOCK類來制作一個電子秒表,能夠自動跳轉(zhuǎn)
代碼中有些陌生的庫函數(shù),順便介紹一下:
1.system(“cls”)函數(shù)
system函數(shù)代表執(zhí)行系統(tǒng)命令,system(“cls”)就是執(zhí)行命令”清屏“的意思。
#include <windows.h>
system("cls"); ?2.setw()與setfill()函數(shù)
在C++中,setw(int n)用來控制輸出間隔。setw()默認填充的內(nèi)容為空格,可以setfill()配合使用設置其他字符填充。注意:setw和setfill 被稱為輸出控制符,使用時需要在程序開頭寫上#include “iomanip.h”,否則無法使用。
3.Sleep()函數(shù)
功 能: 執(zhí)行掛起一段時間
用 法: unsigned sleep(unsigned n);//n為毫秒
使用時帶上頭文件#include <windows.h>
整個程序代碼如下:
#include<iostream>
#include<iomanip>
#include <windows.h>
using ?namespace ?std;
class CLOCK
{
private:
? ? int hour;
? ? int minute;
? ? int second;
public:
? ? CLOCK(int newh=0,int newm=0, int news=0);
? ? ~CLOCK();
? ? void init(int newh,int newm, int news);
? ? void run();
};
CLOCK::CLOCK(int newh,int newm, int news)
{
? ? hour=newh;
? ? minute=newm;
? ? second=news;
}
void CLOCK::init(int newh,int newm, int news)
{
? ? hour=newh;
? ? minute=newm;
? ? second=news;
}
void CLOCK::run()
{
? ? while(1)
? ? {
? ? ? ? system("cls");
? ? ? ? cout<<setw(2)<<setfill('0')<<hour<<":";
? ? ? ? cout<<setw(2)<<setfill('0')<<minute<<":";
? ? ? ? cout<<setw(2)<<setfill('0')<<second;
? ? ? ? Sleep(1000);
? ? ? ? if(++second==60)
? ? ? ? {
? ? ? ? ? ? second=0;
? ? ? ? ? ? minute=minute+1;
? ? ? ? ? ? if(minute==60)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? minute=0;
? ? ? ? ? ? ? ? hour=hour+1;
? ? ? ? ? ? ? ? if(hour==24)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? hour=0;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
CLOCK::~CLOCK()
{
}
int main()
{
? ? CLOCK c;
? ? c.init(23,59,55);
? ? c.run();
? ? system("pause");
? ? return 0;
}代碼執(zhí)行如下


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++獲取類的成員函數(shù)的函數(shù)指針詳解及實例代碼
這篇文章主要介紹了C++獲取類的成員函數(shù)的函數(shù)指針詳解及實例代碼的相關資料,需要的朋友可以參考下2017-02-02
C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法
這篇文章主要介紹了C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法,需要的朋友可以參考下2014-07-07

