C++實(shí)現(xiàn)秒表功能
本文實(shí)例為大家分享了C++實(shí)現(xiàn)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
抽象出CLOCK類來(lái)制作一個(gè)電子秒表,能夠自動(dòng)跳轉(zhuǎn)
代碼中有些陌生的庫(kù)函數(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)用來(lái)控制輸出間隔。setw()默認(rèn)填充的內(nèi)容為空格,可以setfill()配合使用設(shè)置其他字符填充。注意:setw和setfill 被稱為輸出控制符,使用時(shí)需要在程序開頭寫上#include “iomanip.h”,否則無(wú)法使用。
3.Sleep()函數(shù)
功 能: 執(zhí)行掛起一段時(shí)間
用 法: unsigned sleep(unsigned n);//n為毫秒
使用時(shí)帶上頭文件#include <windows.h>
整個(gè)程序代碼如下:
#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í)行如下


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++獲取類的成員函數(shù)的函數(shù)指針詳解及實(shí)例代碼
這篇文章主要介紹了C++獲取類的成員函數(shù)的函數(shù)指針詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
C語(yǔ)言編程中對(duì)目錄進(jìn)行基本的打開關(guān)閉和讀取操作詳解
這篇文章主要介紹了C語(yǔ)言編程中對(duì)目錄進(jìn)行基本的打開關(guān)閉和讀取操作,是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++實(shí)現(xiàn)線程同步的四種方式總結(jié)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)線程同步的四種方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++有一定的幫助,需要的可以參考一下2022-11-11
C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法
這篇文章主要介紹了C++操作MySQL大量數(shù)據(jù)插入效率低下的解決方法,需要的朋友可以參考下2014-07-07

