Created
April 17, 2015 10:54
-
-
Save duycop/409aa10826d7126f312c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//create by Do Duy Cop - 7:46 17/04/2015 | |
#include <iostream.h> | |
#include <conio.h> | |
class mytime{ | |
private: | |
int h,m,s; | |
void showNum(int n){if(n<10)cout<<0;cout<<n;} | |
public: | |
mytime(int hh=0,int mm=0, int ss=0){ | |
h=hh;m=mm;s=ss; | |
} | |
void addH(){h++;h%=24;} | |
void subH(){h--;if(h<0)h=23;} | |
void addM(){m++;m%=60;} | |
void subM(){m--;if(m<0)m=59;} | |
void resetS(){s=0;} | |
void show(int shows=0){ | |
showNum(h); | |
cout<<":"; | |
showNum(m); | |
if(shows){ | |
cout<<":"; | |
showNum(s); | |
}else | |
cout<<" "; | |
cout<<endl; | |
} | |
void tick(){ | |
s++; | |
if(s>=60){ | |
s=0; | |
m++; | |
if(m>=60){ | |
m=0; | |
h++; | |
if(h>=24)h=0; | |
} | |
} | |
} | |
int equal(mytime x){ | |
return h==x.h && m==x.m && s==x.s; | |
} | |
}; | |
//---------------------------- | |
class myclock{ | |
private: | |
mytime RT, AT; | |
enum status{dislayTime, setTimeH, setTimeM, setTimeS, | |
displayAlarm, setAlarmH, setAlarmM}; | |
status st; | |
int show_msg; | |
unsigned long counter; | |
public: | |
myclock(); | |
void A(); | |
void B(); | |
void C(); | |
void run(); | |
}; | |
//---------------------------- | |
myclock::myclock(){ | |
st=dislayTime; | |
show_msg=0; | |
counter=0; | |
AT=mytime(0,1); | |
} | |
//---------------------------- | |
void myclock::A(){ | |
switch(st){ | |
case dislayTime: cout<<"Turn light on 5(s)"; show_msg=5; break; | |
case setTimeH: RT.addH();break; | |
case setTimeM: RT.addM();break; | |
case setTimeS: RT.resetS();break; | |
case displayAlarm: cout<<"Test buzz 10(s)"; show_msg=10; break; | |
case setAlarmH: AT.addH();break; | |
case setAlarmM: AT.addM();break; | |
} | |
} | |
//---------------------------- | |
void myclock::B(){ | |
switch(st){ | |
case dislayTime: st=setTimeH; break; | |
case setTimeH: st=setTimeM; break; | |
case setTimeM: st=setTimeS; break; | |
case setTimeS: st=dislayTime; break; | |
case displayAlarm: st=setAlarmH; break; | |
case setAlarmH: st=setAlarmM; break; | |
case setAlarmM: st=displayAlarm; break; | |
} | |
} | |
//---------------------------- | |
void myclock::C(){ | |
switch(st){ | |
case dislayTime: st=displayAlarm; break; | |
case setTimeH: RT.subH(); break; | |
case setTimeM: RT.subM(); break; | |
case setTimeS: RT.resetS(); break; | |
case displayAlarm: st=dislayTime; break; | |
case setAlarmH: AT.subH(); break; | |
case setAlarmM: AT.subM(); break; | |
} | |
} | |
//---------------------------- | |
int getkey() | |
{ int key=0; | |
while (kbhit()){ //lap vo tan | |
if((key=getch())==0) //phim dac biet co 2 ma la 0 va ma dac biet | |
key=-getch(); //lay ma dac biet, dao dau thanh gia tri AM | |
} | |
return key; //tra ve ket qua | |
} // end getkey | |
//---------------------------- | |
void myclock::run(){ | |
//show mytime | |
//tang 1s sau moi giay cho RT | |
//neu RT==AT: buzz | |
//nhan cac su kien bam nut A, B, C de xu ly | |
int ok=1; //press esc -> ok=0: stop run | |
while(ok){ | |
int p=getkey(); | |
gotoxy(1,1); | |
switch(st){ | |
case dislayTime: cout<<"Real Time: ";RT.show(1); break; | |
case setTimeH: cout<<"Set Hour: "; RT.show(1); break; | |
case setTimeM: cout<<"Set Minute: "; RT.show(1); break; | |
case setTimeS: cout<<"Set Second: "; RT.show(1); break; | |
case displayAlarm: cout<<"Alarm Time: ";AT.show(); break; | |
case setAlarmH: cout<<"Set Alarm Hour: "; AT.show(); break; | |
case setAlarmM: cout<<"Set Alarm Minute: "; AT.show(); break; | |
} | |
if(p=='a'||p=='A')A(); | |
else if(p=='b'||p=='B')B(); | |
else if(p=='c'||p=='C')C(); | |
else if(p==27)ok=0; | |
//nothing: | |
counter++; | |
if(counter>=3000L){ | |
counter=0; | |
RT.tick(); | |
if(RT.equal(AT)){ | |
cout<<"BUZZZZZZZZZZZ alarm 15(s)"; | |
show_msg=15; | |
} | |
if(show_msg>0){ | |
show_msg--; | |
if(show_msg==0) | |
cout<<" ";//del msg | |
} | |
} | |
} | |
cout<<"bye!"; | |
} | |
//---------------------------- | |
int main(){ | |
myclock casio; | |
casio.run(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream.h> | |
#include <conio.h> | |
class myTime{ | |
private: | |
int h,m,s; | |
void showNum(int n){if(n<10)cout<<0;cout<<n;} | |
public: | |
myTime(int hh=0,int mm=0, int ss=0){ | |
h=hh;m=mm;s=ss; | |
} | |
void addH(){h++;h%=24;} | |
void subH(){h--;if(h<0)h=23;} | |
void addM(){m++;m%=60;} | |
void subM(){m--;if(m<0)m=59;} | |
void resetS(){s=0;} | |
void show(int shows=0){ | |
showNum(h); | |
cout<<":"; | |
showNum(m); | |
if(shows){ | |
cout<<":"; | |
showNum(s); | |
}else | |
cout<<" "; | |
cout<<endl; | |
} | |
void tick(){ | |
s++; | |
if(s>=60){ | |
s=0; | |
m++; | |
if(m>=60){ | |
m=0; | |
h++; | |
if(h>=24)h=0; | |
} | |
} | |
} | |
int equal(myTime x){ | |
return h==x.h && m==x.m && s==x.s; | |
} | |
int equal(myTime*x){ | |
return equal(*x); | |
} | |
}; | |
//---------------------------- | |
class myclock{ | |
private: | |
myTime *RT, *AT; | |
enum status{dislayTime, setTimeH, setTimeM, setTimeS, | |
displayAlarm, setAlarmH, setAlarmM}; | |
status st; | |
int show_msg; | |
unsigned long counter; | |
public: | |
myclock(); | |
void A(); | |
void B(); | |
void C(); | |
void run(); | |
}; | |
//---------------------------- | |
myclock::myclock(){ | |
st=dislayTime; | |
RT=new myTime(); | |
AT=new myTime(0,1); | |
show_msg=0; | |
counter=0; | |
} | |
//---------------------------- | |
void myclock::A(){ | |
switch(st){ | |
case dislayTime: cout<<"Turn light on 5(s)"; show_msg=5; break; | |
case setTimeH: RT->addH();break; | |
case setTimeM: RT->addM();break; | |
case setTimeS: RT->resetS();break; | |
case displayAlarm: cout<<"Test buzz 10(s)"; show_msg=10; break; | |
case setAlarmH: AT->addH();break; | |
case setAlarmM: AT->addM();break; | |
} | |
} | |
//---------------------------- | |
void myclock::B(){ | |
switch(st){ | |
case dislayTime: st=setTimeH; break; | |
case setTimeH: st=setTimeM; break; | |
case setTimeM: st=setTimeS; break; | |
case setTimeS: st=dislayTime; break; | |
case displayAlarm: st=setAlarmH; break; | |
case setAlarmH: st=setAlarmM; break; | |
case setAlarmM: st=displayAlarm; break; | |
} | |
} | |
//---------------------------- | |
void myclock::C(){ | |
switch(st){ | |
case dislayTime: st=displayAlarm; break; | |
case setTimeH: RT->subH(); break; | |
case setTimeM: RT->subM(); break; | |
case setTimeS: RT->resetS(); break; | |
case displayAlarm: st=dislayTime; break; | |
case setAlarmH: AT->subH(); break; | |
case setAlarmM: AT->subM(); break; | |
} | |
} | |
//---------------------------- | |
int getkey() | |
{ int key=0; | |
while (kbhit()){ //lap vo tan | |
if((key=getch())==0) //phim dac biet co 2 ma la 0 va ma dac biet | |
key=-getch(); //lay ma dac biet, dao dau thanh gia tri AM | |
} | |
return key; //tra ve ket qua | |
} // end getkey | |
//---------------------------- | |
void myclock::run(){ | |
//show myTime | |
//tang 1s sau moi giay cho RT | |
//neu RT==AT: buzz | |
//nhan cac su kien bam nut A, B, C de xu ly | |
int ok=1; //press esc -> ok=0: stop run | |
while(ok){ | |
int p=getkey(); | |
gotoxy(1,1); | |
switch(st){ | |
case dislayTime: cout<<"Real Time: ";RT->show(1); break; | |
case setTimeH: cout<<"Set Hour: "; RT->show(1); break; | |
case setTimeM: cout<<"Set Minute: "; RT->show(1); break; | |
case setTimeS: cout<<"Set Second: "; RT->show(1); break; | |
case displayAlarm: cout<<"Alarm Time: "; AT->show(); break; | |
case setAlarmH: cout<<"Set Alarm Hour: "; AT->show(); break; | |
case setAlarmM: cout<<"Set Alarm Minute: "; AT->show(); break; | |
} | |
if(p=='a'||p=='A')A(); | |
else if(p=='b'||p=='B')B(); | |
else if(p=='c'||p=='C')C(); | |
else if(p==27)ok=0; | |
//nothing: | |
counter++; | |
if(counter>=3000L){ | |
counter=0; | |
RT->tick(); | |
if(RT->equal(AT)){ | |
cout<<"BUZZZZZZZZZZZ alarm 15(s)"; | |
show_msg=15; | |
} | |
if(show_msg>0){ | |
show_msg--; | |
if(show_msg==0) | |
cout<<" ";//del msg | |
} | |
} | |
} | |
cout<<"bye!"; | |
} | |
//---------------------------- | |
int main(){ | |
myclock *casio=new myclock(); | |
casio->run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment