सी ++ प्रोग्राम दो समय अवधि के बीच अंतर की गणना करने के लिए

इस उदाहरण को समझने के लिए, आपको निम्नलिखित C ++ प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:

  • सी ++ संरचनाएं
  • सी ++ संरचना और कार्य
  • C ++ पॉइंटर्स टू स्ट्रक्चर

उदाहरण: प्रोग्राम टू टाइम डिफरेंस

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

आउटपुट

क्रमशः घंटे, मिनट और सेकंड दर्ज करें: 11 33 52 स्टॉप समय दर्ज करें। क्रमशः घंटे, मिनट और सेकंड दर्ज करें: 8 12 15 TIME DIFFERENCE: 11:33:52 - 8:12:15 = 3:21-28

इस कार्यक्रम में, उपयोगकर्ता को दो समयावधि दर्ज करने के लिए कहा जाता है और इन दो अवधियों को क्रमशः संरचना चर 1 और टी 2 में संग्रहीत किया जाता है।

फिर, computeTimeDifference()फ़ंक्शन समय अवधि के बीच के अंतर की गणना करता है और परिणाम स्क्रीन पर main()इसे वापस किए बिना प्रदर्शित किया जाता है (संदर्भ द्वारा कॉल)।

दिलचस्प लेख...