[問題] 遞增遞減運算子 如何搭配時間函數
我的問題出在 執行的時候我所輸入的時間 會跑 但是不會進位
沒有回傳到函式裡面 要如何解決?
這是我的程式碼:
#include<iostream>
#include<ctime>
using namespace std;
class Time{
public:
Time(int,int,int);
void show()
{
cout<<hour<<"點"<<min<<"分"<<sec<<"秒"<<endl;
}
Time& operator++();
private:
int hour,min,sec;
};
Time::Time(int h=12,int m=0,int s=0)
{
hour=(h>=0 && h<=23) ?h:12;
min=(m>=0 && m<=59) ?m:0;
sec=(s>=0 && s<=59) ?s:0;
}
Time&Time::operator++()
{
if(++sec >= 60){sec=0;
if(++min >= 60){min=0;
if(++hour == 24)hour=0;} }
return *this;
}
int main()
{
int a,b,c;
cout<<"請輸入小時"<<endl;
cin>>a;
cout<<"請輸入分鐘"<<endl;
cin>>b;
cout<<"請輸入秒數"<<endl;
cin>>c;
clock_t starthour=clock();
clock_t startmin=clock();
clock_t startsec=clock();
Time t1(a,b,c);
(++t1).show();
while(true){
int h,m,s,q,p;
clock_t endhour=clock();
clock_t endmin=clock();
clock_t endsec=clock();
h=(int)((endhour-starthour)/CLK_TCK/3600)+a;
m=(int)((endmin-startmin)/CLK_TCK/60)+b;
s=(int)((endsec-startsec)/CLK_TCK)+c;
cout<<h<<" : "<<m<<" : "<<s<<" \r";}
system("pause");
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.134.240.14
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章