//
// 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