假定有秒表类Stopwatch,可以每秒发送消息给其它对象,实现一个闹钟类,测试你的类

//
//  main.cpp
//  闹钟闹钟
//
//  Created by ancientear on 2018/5/8.
//  Copyright © 2018年 ancientear. All rights reserved.
//
   
 //main.cpp
#include "testStopwatch.h"
#include "Stopwatch.h"
#include <iostream>
using namespace std;

int main(void)
{
    try
    {
        testStopwatch();
    }
    catch (int exceptionValue)
    {
        cin.ignore(999,'\n');
        cout << endl;
        cout << "Unhandled Instruction" << endl;
        cout << "Program terminating by exception. ";
        cout << "Press ENTER to continue...";
        cin.ignore(999,'\n');
        return exceptionValue;
    }
    cout << "Press ENTER to continue...";
    cin.ignore(999,'\n');
    return 0;
}


//  Stopwatch.cpp

#include "Stopwatch.h"
#include <time.h>
#include <iostream>
using namespace std;

// constructor
Stopwatch::Stopwatch(void)
{
    reset();
}

// stops the stopwatch (if it is running) and clears the elapsed time
void Stopwatch::reset()
{
    running = false;
    elapsedTime = 0;
}

// turns the Stopwatch on/off
void Stopwatch::toggle()
{
    if (running)
    {
        running = false;
        endTime = time(0); // time_t endTime = time(0) when stopwatch stops at a later time
        elapsedTime = endTime - startTime + elapsedTime;
    }
    else
    {
        running = true;
        startTime = time(0); // time_t startTime = time(0) when stopwatch starts
    }
}

// returns the current elapsed time, in seconds
time_t Stopwatch::split()
{
    if (running)
    {
        endTime = time(0);
        return elapsedTime + endTime - startTime;
    }
    else
    {
        return elapsedTime;
    }
}

// indicates whether or not elasped time is curently accumulating
bool Stopwatch::isRunning()
{
    return running;
}

void Stopwatch::getup()
{
    reset();
    int time;
    cout << "Please enter the time !" << endl;
    cin >> time;
    
 while(1)
 {
      toggle();
    if(elapsedTime == time)
       // cout << elapsedTime << endl;
 
    {
        cout << "aha,it's time to get up!" << endl;
        return;
    }
 }
}



//  Stopwatch.h

#include <time.h>

#ifndef STOPWATCH_LOCK
#define STOPWATCH_LOCK

class Stopwatch
{
public:
    // constructor
    Stopwatch(void);

    // modifiers
    void toggle();
    void reset();

    // status
    time_t split();
    bool isRunning();
    
    void getup();

private:
    time_t startTime;
    time_t endTime;
    long elapsedTime;
    bool running;
 
};

#endif

//  testStopwatch.cpp

#include "testStopwatch.h"
#include "Stopwatch.h"
#include <iostream>
#include <string>
using namespace std;


void testStopwatch(void)
{
    string choice;
    Stopwatch stopwatch;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    choice = getAndCheckInput();
    doChoice(choice, stopwatch);
    cout << endl;

    cout << "That's all, folks!" << endl;
}

// gets input and checks user input for "quit"
string getAndCheckInput(void)
{
    cout << "Enter \"toggle\", \"split\", \"reset\", \"status\" or \"quit\",\"clock\": ";
    string userInput;
    cin >> userInput;
    if (userInput != "quit")
    {
        return userInput;
    }
    else
    {
        exit(0);
    }
}

// executes functions based on choice
void doChoice(string choice, Stopwatch &stopwatch)
{

    if (choice == "toggle")
    {
        if (stopwatch.isRunning())
        {
            stopwatch.toggle();
            cout << "Stopwatch stopped at " << stopwatch.split() << " seconds." << endl;
        }
        else
        {
            cout << "Stopwatch started." << endl;
            stopwatch.toggle();
        }
    }
    else if (choice == "split")
    {
        cout << "Elapsed time is " << stopwatch.split() << " seconds." << endl;
    }
    else if (choice == "reset")
    {
        stopwatch.reset();
        cout << "Stopwatch reset." << endl;
    }
    else if (choice == "status")
    {
        if (stopwatch.isRunning())
        {
            cout << "Stopwatch is running." << endl;
            cout << "Elapsed time is " << stopwatch.split() << " seconds." << endl;
        }
        else
        {
            cout << "Stopwatch is not running." << endl;
            cout << "Elapsed time is " << stopwatch.split() << " seconds." << endl;
        }
    }
    else if (choice == "clock")
    {
        stopwatch.getup();
    }
    
    // catch invalid input
    else
    {
        throw 1;
    }
}

//  testStopwatch.h

#include "Stopwatch.h"
#include <string>
using namespace std;

#ifndef TEST_STOPWATCH_LOCK
#define TEST_STOPWATCH_LOCK

void testStopwatch(void);
string getAndCheckInput(void);
void doChoice(string choice, Stopwatch &stopwatch);

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Αиcíеиτеǎг

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值