İki Zaman Dönemi Arasındaki Farkı Hesaplamak için C ++ Programı

Bu örneği anlamak için, aşağıdaki C ++ programlama konuları hakkında bilgi sahibi olmalısınız:

  • C ++ Yapıları
  • C ++ Yapısı ve İşlevi
  • Yapıya C ++ İşaretçiler

Örnek: Zaman Farkına Programlama

 // 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; ) 

Çıktı

Sırasıyla saat, dakika ve saniye girin: 11 33 52 Durma zamanını girin. Sırasıyla saat, dakika ve saniye girin: 8 12 15 SAAT FARKI: 11:33:52 - 8:12:15 = 3:21:37

Bu programda, kullanıcıdan iki zaman aralığı girmesi istenir ve bu iki dönem sırasıyla t1 ve t2 yapı değişkenlerinde saklanır.

Daha sonra computeTimeDifference()fonksiyon, zaman dilimleri arasındaki farkı hesaplar ve sonuç, main()fonksiyona geri dönmeden ekranda görüntülenir (referansla arama).

Ilginç makaleler...