注册 登录
编程论坛 C++教室

倒计时

xiaxin 发布于 2007-11-10 23:18, 874 次点击


#include<iostream>
#include<cstdlib>
#include<Windows.h>
using namespace std;
class Timer
{
private:
int day,hour,minute,second;
public:
Timer(int aDay,int aHour,int aMinute,int aSecond)//系统自动调用
{
day=aDay;//初始化的特殊性
hour=aHour;
minute=aMinute;
second=aSecond;
}

void coutDown();
void printTime();


};
int main()
{
Timer opening(1,0,0,2);
while(1)
{ system("cls");//数字被抹掉
opening.printTime();
Sleep(1000);//显示的数字停留1秒钟
system("cls");
opening.coutDown();
opening.printTime();
}
system("pause");
return 0;
}
void Timer::coutDown()
{
second--;

if(second<0)
{
minute--;
if(minute<0)
{ hour--;
minute=59;
if(hour<0)
{
day--;

hour=23;}}
second=60;
second--;

}


}
void Timer::printTime()
{

cout<<"距离奥运会还有:"<<day<<"天"<<endl;
cout<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}
这个程序有个问题,天数会出现负数,
请问怎样才能使其天数为负时跳出循环呢?

5 回复
#2
拉风2007-11-10 23:33

我把你的改了下,这样就行了
#include<iostream>
#include<cstdlib>
#include<Windows.h>
using namespace std;
class Timer
{
private:
int day,hour,minute,second;
public:
Timer(int aDay,int aHour,int aMinute,int aSecond)//系统自动调用
{
day=aDay;//初始化的特殊性
hour=aHour;
minute=aMinute;
second=aSecond;
}

void coutDown();
void printTime();
bool cut();


};
bool Timer::cut(){
if(day==0&&hour==0&&minute==0&&second==0)
return true;
else
return false;
}
int main()
{
Timer opening(1,0,0,2);
for(;;)
{ system("cls");//数字被抹掉
opening.printTime();
Sleep(1000);//显示的数字停留1秒钟
system("cls");
opening.coutDown();
opening.printTime();
if(opening.cut())break;
}
system("pause");
return 0;
}
void Timer::coutDown()
{
second--;

if(second<0)
{
minute--;
if(minute<0)
{ hour--;
minute=59;
if(hour<0)
{
day--;

hour=23;}}
second=60;
second--;

}


}
void Timer::printTime()
{

cout<<"距离奥运会还有:"<<day<<"天"<<endl;
cout<<" "<<hour<<":"<<minute<<":"<<second<<endl;
}

#3
拉风2007-11-10 23:36

只是多了个中断函数而已,自己想想也能想出来的啊,不知道还有没有更好的方法

#4
xiaxin2007-11-10 23:48

谢谢,看来我还要多练练了

#5
liyingwei2007-11-13 23:54
怎么运行不了啊!编译时有3个错误!
#6
拉风2007-11-15 22:30

晕啊,楼上的你用什么编译器啊,我用VC6.0运行过了啊

1