練習用です。いろんなものがごちゃまぜです。
(empty log message)
| @@ -4,41 +4,43 @@ | ||
| 4 | 4 | |
| 5 | 5 | class stopwatch { |
| 6 | 6 | clock_t startTime; |
| 7 | - int timespan; | |
| 8 | - bool switch_on; | |
| 7 | + clock_t endTime; | |
| 9 | 8 | public: |
| 10 | 9 | stopwatch(); |
| 10 | + stopwatch(clock_t starttime); | |
| 11 | 11 | void stop(); |
| 12 | 12 | void start(); |
| 13 | + void show(); | |
| 13 | 14 | ~stopwatch(); |
| 14 | 15 | }; |
| 15 | 16 | |
| 16 | 17 | stopwatch::stopwatch() |
| 17 | 18 | { |
| 18 | - timespan = 0; | |
| 19 | - switch_on = false; | |
| 19 | + startTime = endTime = 0; | |
| 20 | 20 | } |
| 21 | - | |
| 21 | +stopwatch::stopwatch(clock_t starttime) | |
| 22 | +{ | |
| 23 | + this->startTime = starttime; | |
| 24 | + this->endTime = 0; | |
| 25 | +} | |
| 26 | + | |
| 22 | 27 | void stopwatch::start() |
| 23 | 28 | { |
| 24 | - startTime = clock(); | |
| 25 | - switch_on = true; | |
| 29 | + this->startTime = clock(); | |
| 26 | 30 | } |
| 27 | 31 | |
| 28 | 32 | void stopwatch::stop() |
| 29 | 33 | { |
| 30 | - if (!switch_on){ | |
| 31 | - return; | |
| 32 | - } | |
| 33 | - switch_on = false; | |
| 34 | - clock_t endTime = clock(); | |
| 35 | - timespan += (endTime - startTime) / CLOCKS_PER_SEC; | |
| 34 | + this->endTime = clock(); | |
| 36 | 35 | } |
| 36 | +void stopwatch::show() | |
| 37 | +{ | |
| 38 | + int timespan = (endTime - startTime) / CLOCKS_PER_SEC; | |
| 39 | + cout << "経過時間: " << timespan << "\n"; | |
| 40 | +} | |
| 37 | 41 | |
| 38 | 42 | stopwatch::~stopwatch() |
| 39 | 43 | { |
| 40 | - stop(); | |
| 41 | - cout << "経過時間: " << timespan << "\n"; | |
| 42 | 44 | } |
| 43 | 45 | |
| 44 | 46 | int main() |
| @@ -48,15 +50,16 @@ | ||
| 48 | 50 | |
| 49 | 51 | //遅延... |
| 50 | 52 | sw.start(); |
| 51 | - cout << "on なにか入力: "; | |
| 53 | + cout << "なにか入力: "; | |
| 52 | 54 | cin >> c; |
| 53 | 55 | sw.stop(); |
| 54 | - cout << "off なにか入力。: "; | |
| 56 | + sw.show(); | |
| 57 | + | |
| 58 | + stopwatch sw2(clock()); | |
| 59 | + cout << "なにか入力。: "; | |
| 55 | 60 | cin >> c; |
| 56 | - sw.start(); | |
| 57 | - cout << "on なにか入力。: "; | |
| 58 | - cin >> c; | |
| 59 | - sw.stop(); | |
| 61 | + sw2.stop(); | |
| 62 | + sw2.show(); | |
| 60 | 63 | |
| 61 | 64 | return 0; |
| 62 | 65 | } |
| \ No newline at end of file |