English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا المثال، ستتعلم كيفية استخدام الوظائف المحددة من قبل المستخدم لحساب الفارق بين فترتين زمنيتين.
لفهم هذا المثال، يجب أن تكون على علم بما يليبرمجة Cالموضوع:
#include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2; struct TIME *diff); int main() { struct TIME startTime, stopTime, diff; printf("إدخل الوقت البدء. "); printf("إدخل الساعة、الدقيقة والثانية: "); scanf("%d %d %d", &startTime.hours,) &startTime.minutes, &startTime.seconds); printf("ادخل وقت التوقف. "); printf("ادخل الساعة، الدقيقة والثانية: "); scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // الفرق بين وقت البداية ووقت التوقف differenceBetweenTimePeriod(startTime, stopTime, &diff); printf("\nالفرق الزمني: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds); printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds); printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds); return 0; } // حساب الفرق بين فترات الوقت void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff) { while (stop.seconds > start.seconds) { --start.minutes; start.seconds += 60; } diff->seconds = start.seconds - stop.seconds; while (stop.minutes > start.minutes) { --start.hours; start.minutes += 60; } diff->minutes = start.minutes - stop.minutes; diff->hours = start.hours - stop.hours; }
نتيجة الخروج
ادخل وقت البداية. ادخل الساعة، الدقيقة والثانية: 12 34 55 ادخل وقت التوقف. ادخل الساعة، الدقيقة والثانية: 8 12 15 الفرق الزمني: 12:34:55 - 8:12:15 = 4:22:40
في هذا البرنامج، يتم طلب إدخال فترتين من الوقت وتم تخزين هاتين الفترتين على التوالي في متغيرات البنية startTime وstopTime.
ثم، يحدد الدالة differenceBetweenTimePeriod() الفرق بين فترات الوقت. يعرض النتائج من الدالة main() دون العودة إليها (استخدامالاستدعاءات المقدرةالتكنولوجيا).