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
|
#include <iostream> //for cout, cin
#include <string> //for string i linje 21
using namespace std;
struct timestamp { //struct for klokkeslett
int hour, min, sec; //tallene i structen
};
// overload setter 0 foran ensiffrede tall
ostream & operator << ( ostream & stream, const timestamp & ts ) {
//hvis tallet er mindre en 10, setter 0 foran
return stream
<< (ts.hour < 10 ? "0" : "") << ts.hour << ":" //for timer
<< (ts.min < 10 ? "0" : "") << ts.min << ":" //for minutter
<< (ts.sec < 10 ? "0" : "") << ts.sec; //for sekunder
}
timestamp modify( const timestamp & ts, const std::string & name ) {
cout << name << "'s klokkeslett er: " //viser standard tid
<< ts << "\nVil du endre det? [j/n] "; //vil bruker endre?
cout.flush(); //sletter buffer
char response1; //variabel j/n
cin >> response1; //bruker skriver inn j/n
if( !( response1 == 'j' || response1 == 'J' ) )
return ts;
//hvis nei, ingen endring, hvis ja, skriv inn klokkeslett
int hour, min, sec; //variabler for timer, minutter sekunder
hour = min = sec = -1;
while( hour < 0 || hour > 23 )
while( min < 0 || min > 59 )
while( sec < 0 || sec > 59 ){
cout << "Skriv inn klokkeslett [tt mm ss]: ";
cout.flush(); //fjerner gammel verdi for klokkeslett
cin >> hour >> min >> sec; //brukreren skriver inn klokkeslett
}
return timestamp { hour, min, sec };
}
int main() {
struct diff; //for forskjellen
char response2; //for svar pa neste sporsmal
timestamp t1 { 20, 30, 45 };
timestamp t2 { 19, 25, 55 };
t1 = modify( t1, "Time1" ); //endring
t2 = modify( t2, "Time2" );
cout << "Endret:\n" << t1 << "\n" << t2 << endl;
{ //Regner ut forskjell
if (t1>t2) {
diff = (t1-t2); //hvis t1 er storre enn t2
}
else if (t2>t1) { //hvis t2 er storre enn t1
diff = (t2-t1);
}
else if (t1==t2) { //hvis t1 er lik t t2
diff = 0;}
cout << "Forskjellen mellom de to klokkeslettene er:\n" << diff << endl;
cout << "Vil du endre igjen? ";
cin >> response2;
} if( response2 != 'N' || response2 != 'n'); //hvis nei
return 0; //return int main
}
|