帮忙看看这个时钟程序哪里有问题

taoanran 2009-03-30 10:20:35
#include <iostream>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include<stdlib.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
#include <MMsystem.h>
using namespace std;
int N;
class Clock
{
public:
Clock(int year, int mouth,int day,int hour,int minute,int second)
{
Hour=hour;
Minute=minute;
Second=second;
Year=year;
Mouth=mouth;
Day=day;
};
void time(); //时钟的时间
void adjust(char c[]);
~Clock(){}
private:
int Hour,Minute,Second,Day,Mouth,Year;
};


void Clock::adjust(char c[])
{

int n=0,h=0,m=0,s=0;//h,m,s代表hour,minute,second
for(int i=0;c[i]!='\0';i++)
{
if(c[i]!=':' && n==0)
h=h*10+(int)(c[i])-48;
else if(c[i]!=':' && n==1)
m=m*10+(int)(c[i])-48;
else if(c[i]!=':' && n==2)
s=s*10+(int)(c[i])-48;
if(c[i]==':')
n++;
}
if(Hour==h && Minute==m && Second==s)
PlaySound("F:\\闹铃.wav", NULL, SND_FILENAME | SND_SYNC ); //SND_SYNC为同步播放,既播完才返回
}



void Clock::time()
{
cout<<"需要定时码??\n1.yes 2.no"<<endl;
cin>>N;
char c[10];
if(N==1)
{
cout<<"请输入时间:如:1:12:12"<<endl;
getchar();
gets(c);
}
while(1)
{
if(N==1)
adjust(c);//问题在这,在这里设置时间
//时间会暂停,既比正常时间慢,应该是Second
//在这里没有继续记时,怎么解决这个问题
Sleep(1000);
Second++;
if(Second==60)
{

Minute++;
Second=0;
}
if(Minute==60)
{
Hour++;
PlaySound("F:\\整点报时.wav", NULL, SND_FILENAME | SND_SYNC ); //SND_SYNC为同步播放,既播完才返回
Minute=0;
}
if(Hour==24)
{
Hour=0;
Day++;
}
if((Year%4==0 && Year%100!=0) || Year%400==0)
{

switch(Mouth)
{
case 1:if(Day==31) Mouth++;break;
case 2:if(Day==29) Mouth++;break;
case 3:if(Day==31)Mouth++;break;
case 4:if(Day==30)Mouth++;break;
case 5:if(Day==31)Mouth++;break;
case 6:if(Day==30)Mouth++;break;
case 7:if(Day==31)Mouth++;break;
case 8:if(Day==31)Mouth++;break;
case 9:if(Day==30)Mouth++;break;
case 10:if(Day==31)Mouth++;break;
case 11:if(Day==30)Mouth++;break;
case 12:if(Day==31)Mouth++;break;
}

}
else
{

switch(Mouth)
{
case 1:if(Day==31)Mouth++;break;
case 2:if(Day==28)Mouth++;break;
case 3:if(Day==31)Mouth++;break;
case 4:if(Day==30)Mouth++;break;
case 5:if(Day==31)Mouth++;break;
case 6:if(Day==30)Mouth++;break;
case 7:if(Day==31)Mouth++;break;
case 8:if(Day==31)Mouth++;break;
case 9:if(Day==30)Mouth++;break;
case 10:if(Day==31)Mouth++;break;
case 11:if(Day==30)Mouth++;break;
case 12:if(Day==31)Mouth++;break;
}


}
if(Mouth>12) {Year++;Mouth=1;}
system("cls");
cout<<Year<<"年"<<Mouth<<"月"<<Day<<"日"<<endl;
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
}

int main()
{
SYSTEMTIME sys; //这是一个提取系统时间的类
GetLocalTime( &sys ); //提取系统时间
Clock shijian(sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond);
shijian.time();
return 0;
}
//问题出现的地方,在程序中已标出,
//时间会暂停,既比正常时间慢,应该是Second
//在这里没有继续记时,怎么解决这个问题
...全文
125 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
la_feng 2009-04-04
  • 打赏
  • 举报
回复
Sleep(1000); 的意思是将进程挂起大约1秒的时间,在这个时间内这个线程会暂停,时间到再执行下面的代码,要想精确计时建议用定时器SetTimer
liliangbao 2009-04-04
  • 打赏
  • 举报
回复
学习~
帮顶~
品茶 2009-03-31
  • 打赏
  • 举报
回复
Mark
「已注销」 2009-03-31
  • 打赏
  • 举报
回复
「已注销」 2009-03-31
  • 打赏
  • 举报
回复
Sleep(1000); 是不精确的。
精确计时可以这样:http://www.vckbase.com/document/viewdoc/?id=1795
ltc_mouse 2009-03-30
  • 打赏
  • 举报
回复
时间慢了多少呢?

跟PlaySound的同步,有没有关系?是不是因为播放声音让时间慢了?

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧