1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#include <iostream> #include <chrono> #include <thread> #include <iomanip> class Stopwatch { public: Stopwatch() : startTime(), endTime(), running(false), elapsedTime(0) {} void start() { if (!running) { startTime = std::chrono::high_resolution_clock::now(); running = true; std::cout << "Stopwatch started!" << std::endl; } else { std::cout << "Stopwatch is already running!" << std::endl; } } void stop() { if (running) { endTime = std::chrono::high_resolution_clock::now(); elapsedTime += std::chrono::duration<double>(endTime - startTime).count(); running = false; std::cout << "Stopwatch stopped!" << std::endl; } else { std::cout << "Stopwatch is not running!" << std::endl; } } void reset() { if (running) { startTime = std::chrono::high_resolution_clock::now(); } else { elapsedTime = 0; } std::cout << "Stopwatch reset!" << std::endl; } void display() const { double currentTime = elapsedTime; if (running) { auto now = std::chrono::high_resolution_clock::now(); currentTime += std::chrono::duration<double>(now - startTime).count(); } int minutes = static_cast<int>(currentTime) / 60; int seconds = static_cast<int>(currentTime) % 60; int milliseconds = static_cast<int>((currentTime - static_cast<int>(currentTime)) * 1000); std::cout << "Elapsed Time: " << std::setw(2) << std::setfill('0') << minutes << ":" << std::setw(2) << std::setfill('0') << seconds << "." << std::setw(3) << std::setfill('0') << milliseconds << std::endl; } private: std::chrono::high_resolution_clock::time_point startTime; std::chrono::high_resolution_clock::time_point endTime; bool running; double elapsedTime; }; int main() { Stopwatch stopwatch; char command; while (true) { std::cout << "Enter command (s: start, t: stop, r: reset, d: display, q: quit): "; std::cin >> command; switch (command) { case 's': stopwatch.start(); break; case 't': stopwatch.stop(); break; case 'r': stopwatch.reset(); break; case 'd': stopwatch.display(); break; case 'q': std::cout << "Quitting..." << std::endl; return 0; default: std::cout << "Invalid command!" << std::endl; break; } } return 0; } |
Explanation:
- Stopwatch Class:
- Attributes:
startTime
: Marks the time when the stopwatch starts.endTime
: Marks the time when the stopwatch stops.running
: Indicates whether the stopwatch is currently running.elapsedTime
: Holds the total elapsed time in seconds.
- Methods:
start()
: Starts the stopwatch by recording the current time if it’s not already running.stop()
: Stops the stopwatch, calculates the elapsed time, and updates the total elapsed time.reset()
: Resets the stopwatch. If running, it just updates the start time; otherwise, it sets the elapsed time to zero.display() const
: Displays the elapsed time in minutes, seconds, and milliseconds. If the stopwatch is running, it adds the time elapsed since the last start.
- Attributes:
- main Function:
- Provides a command-line interface for the user to interact with the stopwatch.
- Users can start, stop, reset, display the time, or quit the program using different commands.
Compilation:
To compile the program, use:
1g++ stopwatch.cpp -o stopwatch
1./stopwatch