Program C ++ pentru a calcula diferența dintre două perioade de timp

Pentru a înțelege acest exemplu, ar trebui să aveți cunoștințele următoarelor subiecte de programare C ++:

  • Structuri C ++
  • Structura și funcția C ++
  • C ++ Indicații către structură

Exemplu: Programează diferența de timp

 // Computes time difference of two time period // Time periods are entered by the user #include using namespace std; struct TIME ( int seconds; int minutes; int hours; ); void computeTimeDifference(struct TIME, struct TIME, struct TIME *); int main() ( struct TIME t1, t2, difference; cout << "Enter start time." << endl; cout <> t1.hours>> t1.minutes>> t1.seconds; cout << "Enter stop time." << endl; cout <> t2.hours>> t2.minutes>> t2.seconds; computeTimeDifference(t1, t2, &difference); cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds; cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds; cout << " = " << difference.hours << ":" << difference.minutes << ":" < t1.seconds) ( --t1.minutes; t1.seconds += 60; ) difference->seconds = t1.seconds - t2.seconds; if(t2.minutes> t1.minutes) ( --t1.hours; t1.minutes += 60; ) difference->minutes = t1.minutes-t2.minutes; difference->hours = t1.hours-t2.hours; ) 

Ieșire

Introduceți ore, minute și respectiv secunde: 11 33 52 Introduceți ora de oprire. Introduceți ore, minute și secunde, respectiv: 8 12 15 DIFERENȚA ORARĂ: 11:33:52 - 8:12:15 = 3:21:37

În acest program, utilizatorul este rugat să introducă două perioade de timp, iar aceste două perioade sunt stocate în variabilele structurale t1 și respectiv t2.

Apoi, computeTimeDifference()funcția calculează diferența dintre perioadele de timp și rezultatul este afișat pe ecran din main()funcție fără a o returna (apel prin referință).

Articole interesante...