C++實現(xiàn)鬧鐘程序的方法
更新時間:2014年08月11日 15:29:31 投稿:shichen2014
這篇文章主要介紹了C++實現(xiàn)鬧鐘程序的方法,比較實用的功能,需要的朋友可以參考下
本文所述為C++實現(xiàn)鬧鐘程序的方法,代碼結構相對簡單,注釋也較為完善。現(xiàn)分享給大家供大家參考。
具體功能代碼如下:
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
//時間類
class Time{
private:
int hour;
int minute;
int second;
public:
//設置時間
void set(int h,int m,int s){
hour = h;
minute = m;
second = s;
}
//時間走一秒,時分秒的變化情況
void next(){
if(second<59)
second++;
else if(minute<59){
second=0;
minute++;}
else if(hour<23){
minute=0;
hour++;}
else
hour=0;
}
//得到時間
int get(){
return hour*10000+minute*100+second;
}
};
//時鐘類
class Clock{
private:
Time now;
Time ring_time;
public:
//對表,設定初始時間
void adjust_now(int h,int m,int s){
now.set(h,m,s);
cout<<"現(xiàn)在的時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl;
}
//設定鬧鈴時間
void adjust_ring(int h,int m,int s){
ring_time.set(h,m,s);
cout<<"鬧鈴時間是:"<<h<<"時"<<m<<"分"<<s<<"秒"<<endl;
}
//時間過一秒
void tick(){
long int old=time(0);
while(time(0)==old)
;
now.next();
}
//顯示當前時間
void showtime(){
cout<<now.get()<<endl;
}
//時鐘開始走時,等到了鬧鈴時間,開始響
void run(){
do{
tick();
showtime();
if(now.get()>=ring_time.get())
cout<<'\a';
}while(1);
}
};
int main(){
Clock c;
c.adjust_now(18,35,40); //起始時間
c.adjust_ring(18,35,45); //鬧鈴時間
c.run();
}
感興趣的讀者可以測試運行一下該實例代碼,功能不足之處可以根據情況加以改進和完善。希望該實例能夠對大家學習C++起到一定的幫助作用。
相關文章
vscode工程中c_cpp_properties.json文件作用詳細說明
c_cpp_properties.json是Visual Studio Code的一個配置文件,用于定義C/C++編譯器的路徑、默認包含路徑和預處理器定義,這篇文章主要給大家介紹了關于vscode工程中c_cpp_properties.json文件作用詳細說明的相關資料,需要的朋友可以參考下2024-08-08
C++異步操作future和aysnc與function和bind
這篇文章主要介紹了C++異步操作future和aysnc與function和bind,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
wchar_t,char,string,wstring之間的相互轉換
以下是對wchar_t,char,string,wstring之間的相互轉換進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-09-09

