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 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
//=========== timeDate and related functions ===========================
struct timeDate { int hours, minutes; };
timeDate getTime()
{
int hours, minutes;
string line;
bool ok = false;
while ( !ok )
{
cout << "Enter hours (0-23) and minutes (0-59) as hh:mm ... ";
getline( cin, line );
int pos = line.find( ':' );
hours = stoi( line.substr( 0, pos ) );
minutes = stoi( line.substr( pos + 1 ) );
ok = hours >= 0 && hours <= 23 && minutes >=0 && minutes <= 59;
if ( !ok ) cout << "Invalid time; try again." << endl;
}
return { hours, minutes };
}
timeDate operator+( timeDate a, timeDate b )
{
int minutes = 60 * ( a.hours + b.hours ) + a.minutes + b.minutes;
return { ( minutes / 60 ) % 24, minutes % 60 };
}
ostream &operator << ( ostream &strm, timeDate t )
{
return strm << setfill( '0' ) << setw( 2 ) << t.hours << ':' << setw( 2 ) << t.minutes;
}
//======================================
struct Pixel { int i, j; };
Pixel getPixel( int i0, int j0, double r, double theta )
{
return { i0 - r * cos( theta ) + 0.5, j0 + r * sin( theta ) + 0.5 };
}
void clockFace( timeDate t )
{
const double PI = 3.141592653589793;
const int SIZE = 37, HOUR_RADIUS = 10, MINUTE_RADIUS = 15, DIGIT_RADIUS = 17, NUM_SPOTS = MINUTE_RADIUS;
const char SPOT = '*';
int i0 = ( SIZE - 1 ) / 2, j0 = i0;
vector<string> grid( SIZE, string( SIZE, ' ' ) );
Pixel p;
for ( int hour = 1; hour <= 12; hour++ )
{
double angle = 2.0 * PI * hour / 12;
p = getPixel( i0, j0, DIGIT_RADIUS, angle );
grid[p.i][p.j] = (char) ( '0' + hour % 10 );
if ( hour >= 10 ) grid[p.i][p.j-1] = '1';
}
double angleHours = 2.0 * PI * ( t.hours % 12 + t.minutes / 60.0 ) / 12.0;
double angleMinutes = 2.0 * PI * t.minutes / 60;
for ( int r = 1; r <= NUM_SPOTS; r++ )
{
double frac = ( r - 0.5 ) / NUM_SPOTS;
p = getPixel( i0, j0, HOUR_RADIUS * frac, angleHours );
grid[p.i][p.j] = SPOT;
p = getPixel( i0, j0, MINUTE_RADIUS * frac, angleMinutes );
grid[p.i][p.j] = SPOT;
}
for ( string s : grid ) cout << s << '\n';
}
//======================================
int main()
{
cout << "\nSTART\n";
timeDate start = getTime();
cout << "\nDURATION\n";
timeDate duration = getTime();
timeDate result = start + duration;
cout << "\nFINAL time is " << result << "\n\n";
clockFace( result );
}
|