-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcounter.cpp
More file actions
38 lines (29 loc) · 789 Bytes
/
counter.cpp
File metadata and controls
38 lines (29 loc) · 789 Bytes
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <stdio.h>
int main() {
char c = ' ';
long int count = 0;
time_t startTime;
time_t endTime;
double duration;
double ratio;
time(&startTime);
std::cout << "Tally increases on 1, decreases on 0.\n"
<< "EOF (ctrl+d) outputs final results.\n(0) ";
while((c = std::cin.get()) != EOF){
switch(c){
case '\n': std::cout << "(" << count << ") "; break;
case '1': count++; break;
case '0': count--; break;
}
}
time(&endTime);
duration = difftime(endTime, startTime) / 60; //calculate in minutes
ratio = double(count) / (duration);
std::cout << "\nCount: " << count << std::fixed
<< "\nDuration (min): " << std::setprecision(2) << duration
<< "\nRatio: " << ratio << "\n";
return 0;
}