C++\编写一个时钟程序,模拟数字时钟的显示。
程序代码如下:
#include<iostream>
#include<windows.h>
using namespace std;
class Clock{
public:
Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){
}
void displayTime();
private:
short h;
short m;
short s;
};void Clock::displayTime(){
while(true){
cout<<h<<':'<<m<<':'<<s<<" ";
Sleep(1000);
cout<<'\r';
if(!(s=++s%60))
if(!(m=++m%60))
h=++h%24;
}
} int main()
{
Clock A(23,59,55);
A.displayTime();
return 0;
}
扩展资料:
定义一个日期类,包括年、月、日三个成员变量,显示日期的方法如下:
publicclassDemo{
publicstaticvoidmain(String[]args){
Datedate1=newDate(1994,5,22);
date1.showInfo();
Datedate2=newDate();
date2.year=1995;
date2.month=6;
date2.day=29;
date2.showInfo();
}
}
//日期类:
publicclassDate{
intyear;
intmonth;
intday;
//构造方法
publicDate(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;
}
publicDate(){
}
publicvoidshowInfo(){
System.out.println(year+"年"+month+"月"+day+"日");
}
}
2023-08-24 广告
推荐于2018-03-04
#include<windows.h>
using namespace std;
class Clock{
public:
Clock(short h=0,short m=0,short s=0):h(h),m(m),s(s){
}
void displayTime();
private:
short h;
short m;
short s;
};void Clock::displayTime(){
while(true){
cout<<h<<':'<<m<<':'<<s<<" ";
Sleep(1000);
cout<<'\r';
if(!(s=++s%60))
if(!(m=++m%60))
h=++h%24;
}
} int main()
{
Clock A(23,59,55);
A.displayTime();
return 0;
}