那个、还想请教你一个问题、就是“整点报时”跟“闹钟”两个功能用C++语言怎么弄?

那个、还想请教你一个问题、就是“整点报时”跟“闹钟”两个功能用C++语言怎么弄?我们老师要求弄四面钟的程序、可我们队对Ctime都不懂、如果这位高手可以的话就实在是麻烦了、

CTime是MFC的类, 你的意思是说你们要用MFC, 不是纯正的C++?
我给你一个例子,包含闹钟、整点报时、秒表功能,没有用CTime,你可以看看,花费了我不少时间:
#include <windows.h>
#include <iostream>
#include <ctime>

using namespace std;

//-----------------------------------------------------------------------------

// 互斥量用于避免线程之间文字输出交叉
// 本程序中仅对重要的地方做了保护

HANDLE outMtx;

void initOutMtx()
{
outMtx = CreateMutex(NULL, FALSE, NULL);
}

void destroyOutMtx()
{
CloseHandle(outMtx);
}

void LockOut()
{
WaitForSingleObject(outMtx, INFINITE);
}

void UnlockOut()
{
ReleaseMutex(outMtx);
}

//-----------------------------------------------------------------------------

void openHourChime();
void closeHourChime();
void openAlarm();
void closeAlarm();
void setupAlarm();
void openStopWatch();

// 主菜单
void mainMenu()
{
int op = 10;

while (op != 0) {
LockOut();
cout << endl
<< "请选择功能:" << endl
<< "1. 打开整点报时" << endl
<< "2. 关闭整点报时" << endl
<< "3. 设置闹钟" << endl
<< "4. 打开闹钟" << endl
<< "5. 关闭闹钟" << endl
<< "6. 打开秒表" << endl
<< "0. 退出" << endl << endl;
UnlockOut();

cin >> op;

switch (op) {
case 1:
openHourChime();
break;
case 2:
closeHourChime();
break;
case 3:
setupAlarm();
break;
case 4:
openAlarm();
break;
case 5:
closeAlarm();
break;
case 6:
openStopWatch();
break;
case 0:
break;
default:
cout << "功能代码错误,请重新选择!" << endl;
break;
}
}
}

//-----------------------------------------------------------------------------

#define HOUR_IN_DAY 24
#define SECONDS_IN_MIN 60
#define SECONDS_IN_HOUR (SECONDS_IN_MIN * SECONDS_IN_MIN)
#define SECONDS_IN_DAY (HOUR_IN_DAY * SECONDS_IN_HOUR)

// 计算从输入时间到0点0分的秒数
UINT countTimeSeconds(int hour, int min, int sec = 0)
{
return hour * SECONDS_IN_HOUR + min * SECONDS_IN_MIN + sec;
}

// 计算当前时间到目标时间的秒数
UINT countWaitSeconds(int targetHour, int targetMin, int targetSec)
{
time_t currentTime;
struct tm *currentLocalTime;
UINT currentTimeSeconds, targetTimeSeconds;

currentTime = time(NULL);
currentLocalTime = localtime(¤tTime);

currentTimeSeconds = countTimeSeconds(
currentLocalTime->tm_hour,
currentLocalTime->tm_min,
currentLocalTime->tm_sec);
targetTimeSeconds = countTimeSeconds(targetHour, targetMin, targetSec);

if (currentTimeSeconds < targetTimeSeconds)
return targetTimeSeconds - currentTimeSeconds;
else
return SECONDS_IN_DAY + targetTimeSeconds - currentTimeSeconds;
}

//-----------------------------------------------------------------------------

int CurrentTimeHour;

HANDLE ChimeThread = NULL;

bool isChimeRunning()
{
return ChimeThread != NULL;
}

// 取得当前时间的小时数
int getCurrentHour()
{
time_t currentTime;
struct tm *currentLocalTime;

currentTime = time(NULL);
currentLocalTime = localtime(¤tTime);

return currentLocalTime->tm_hour;
}

// 整点报时监测线程
DWORD WINAPI chimeThreadProc(LPVOID param)
{
int chimeHour;
UINT waitSeconds;

// 保存当前小时数,以后计时就不需要再获取当前时间
CurrentTimeHour = getCurrentHour();

// 计算下一个整点
if (CurrentTimeHour == 23)
chimeHour = 0;
else
chimeHour = CurrentTimeHour + 1;

// 整点报时监测线程每次启动时都要计算需要等待的时间
waitSeconds = countWaitSeconds(chimeHour, 0, 0);

while (true) {
LockOut();
cout << endl << "距离下一个整点时间还有:" << waitSeconds << " 秒!" << endl;
UnlockOut();

Sleep((UINT)waitSeconds * 1000);

CurrentTimeHour++;
if (CurrentTimeHour == HOUR_IN_DAY)
CurrentTimeHour = 0;

// 此时不需要再次计算等待时间,必定为1小时
// 为了更精确,这里可以对时间进行校正
waitSeconds = SECONDS_IN_HOUR;

cout << endl << "整点时间已到:" << CurrentTimeHour << " 点!" << endl;
}
}

// 打开整点报时功能,即打开整点报时监测线程
void openHourChime()
{
if (!isChimeRunning())
ChimeThread = CreateThread(NULL, 0, chimeThreadProc, NULL, 0, 0);

if (isChimeRunning())
cout << endl << "整点报时打开成功!" << endl;
else
cout << endl << "整点报时打开失败!" << endl;
}

// 关闭整点报时功能,即关闭整点报时监测线程
void closeHourChime()
{
if (isChimeRunning()) {
TerminateThread(ChimeThread, 0);
ChimeThread = NULL;
cout << endl << "整点报时已关闭!" << endl;
} else
cout << endl << "整点报时未打开!" << endl;
}

//-----------------------------------------------------------------------------

int AlarmTimeHour = -1;
int AlarmTimeMin = -1;

HANDLE AlarmThread = NULL;

bool isAlarmSetup()
{
return AlarmTimeHour != -1;
}

bool isAlarmRunning()
{
return AlarmThread != NULL;
}

// 闹钟监测线程
DWORD WINAPI alarmThreadProc(LPVOID param)
{
// 闹钟监测线程每次启动时都要计算需要等待的时间
UINT waitSeconds = countWaitSeconds(AlarmTimeHour, AlarmTimeMin, 0);

while (true) {
LockOut();
cout << endl << "距离目标时间还有:" << waitSeconds << " 秒!" << endl;
UnlockOut();

Sleep((UINT)waitSeconds * 1000);

// 此时不需要再次计算等待时间,必定为1天
// 为了更精确,这里可以对时间进行校正
waitSeconds = SECONDS_IN_DAY;

cout << endl << "当前时间已到:"
<< AlarmTimeHour << " 点 "
<< AlarmTimeMin << " 分!" << endl;
}
}

// 打开闹钟功能,即打开闹钟监测线程
void openAlarm()
{
if (!isAlarmSetup()) {
cout << "请先设置闹钟!" << endl;
return;
}

// 每次都重启闹钟监测线程,避免线程之间的通信
TerminateThread(AlarmThread, 0);
AlarmThread = CreateThread(NULL, 0, alarmThreadProc, NULL, 0, 0);

if (isAlarmRunning())
cout << endl << "闹钟定时成功!" << endl;
else
cout << endl << "闹钟定时失败!" << endl;
}

// 关闭闹钟功能,即关闭闹钟监测线程
void closeAlarm()
{
if (isAlarmRunning()) {
TerminateThread(AlarmThread, 0);
AlarmThread = NULL;
cout << endl << "闹钟已关闭!" << endl;
} else
cout << endl << "闹钟未打开!" << endl;
}

// 设置闹钟预约时间
void setupAlarm()
{
bool oldRunStatus;
int hour, minute;

oldRunStatus = isAlarmRunning();

// 在设置过程中暂时关闭闹钟
if (oldRunStatus) {
cout << endl << "关闭当前闹钟..." << endl;
closeAlarm();
}

cout << endl << "闹钟设置,请输入预约时间:" << endl;

// 取得预约小时数
AlarmTimeHour = -1;
while (true) {
cout << endl << "请输入小时(0-23):";
cin >> AlarmTimeHour;
if (AlarmTimeHour >= 0 && AlarmTimeHour < HOUR_IN_DAY)
break;
else
cout << endl << "范围错误,请重新输入!" << endl;
}

// 取得预约分钟数
AlarmTimeMin = -1;
while (true) {
cout << "请输入分钟(0-59):";
cin >> AlarmTimeMin;
if (AlarmTimeMin >= 0 && AlarmTimeMin < SECONDS_IN_MIN)
break;
else
cout << endl << "范围错误,请重新输入!" << endl;
}

cout << endl << "闹钟设置为:" << AlarmTimeHour << " 点 " << AlarmTimeMin << " 分!" << endl;

// 如果闹钟原本处于打开状态,则启动闹钟
if (oldRunStatus)
openAlarm();
else
cout << endl << "当前闹钟未打开!" << endl;
}

//-----------------------------------------------------------------------------

// 打开秒表功能
void openStopWatch()
{
char op, wait;
time_t past_time, now_time;

cout << endl
<< "秒表功能:" << endl
<< "按\"s\"键开始计时" << endl
<< "按\"p\"键停止计时" << endl
<< "按\"e\"键退出秒表" << endl << endl;

wait = 's';

do {
cin >> op;

if (wait != op && op != 'e') {
cout << endl << "无效操作,请重新输入!" << endl;
continue;
}

switch (op) {
case 's':
past_time = time(NULL);
wait = 'p';
cout << endl << "计时开始,按\"p\"键停止计时,按\"e\"键退出秒表..." << endl;
break;

case 'p':
now_time = time(NULL);
wait = 's';
cout << endl
<< "计时停止,持续时间:" << difftime(now_time, past_time) << " 秒!" << endl << endl
<< "按\"s\"键开始计时,按\"e\"键退出秒表..." << endl;
break;
}
} while (op != 'e');
}

int main()
{
initOutMtx();
mainMenu();
destroyOutMtx();

return 0;
}来自:求助得到的回答
温馨提示:内容为网友见解,仅供参考
无其他回答

...整点报时”跟“闹钟”两个功能用C++语言怎么弄?
<< "1. 打开整点报时" << endl << "2. 关闭整点报时" << endl << "3. 设置闹钟" << endl << "4. 打开闹钟" << endl << "5. 关闭闹钟" << endl << "6. 打开秒表" << endl << "0. 退出" << endl << endl; UnlockOut(); cin >> op; switch (op) { case 1: openHourChi...

相似回答