Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C คำนวณความต่างของเวลา (Time Difference)

คำนวณความต่างระหว่างเวลาเริ่มต้นและเวลาสิ้นสุด ในรูปแบบ ชั่วโมง:นาที:วินาที

หลักการคำนวณ

  1. ถ้าวินาทีปลายทาง > วินาทีต้นทาง → ยืมนาที 1 (เพิ่ม 60 วินาที)
  2. ถ้านาทีปลายทาง > นาทีต้นทาง → ยืมชั่วโมง 1 (เพิ่ม 60 นาที)
  3. ลบชั่วโมง นาที วินาที ตามลำดับ

โครงสร้าง Struct Time

struct Time {
    int hours;
    int minutes;
    int seconds;
};

ฟังก์ชันคำนวณความต่าง

void timeDiff(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;
}

ตัวอย่างโปรแกรมครบถ้วน

#include <stdio.h>

struct Time {
    int hours;
    int minutes;
    int seconds;
};

void timeDiff(struct Time start, struct Time stop, struct Time *diff) {
    // Copy เพื่อไม่แก้ไขต้นฉบับ
    struct Time s = start;
    
    while(stop.seconds > s.seconds) {
        s.minutes--;
        s.seconds += 60;
    }
    diff->seconds = s.seconds - stop.seconds;
    
    while(stop.minutes > s.minutes) {
        s.hours--;
        s.minutes += 60;
    }
    diff->minutes = s.minutes - stop.minutes;
    
    diff->hours = s.hours - stop.hours;
}

int main() {
    struct Time start, stop, diff;
    
    printf("Enter START time (hh mm ss): ");
    scanf("%d %d %d", &start.hours, &start.minutes, &start.seconds);
    
    printf("Enter STOP time (hh mm ss): ");
    scanf("%d %d %d", &stop.hours, &stop.minutes, &stop.seconds);
    
    timeDiff(start, stop, &diff);
    
    printf("\nTime Difference: ");
    printf("%02d:%02d:%02d - ", start.hours, start.minutes, start.seconds);
    printf("%02d:%02d:%02d = ", stop.hours, stop.minutes, stop.seconds);
    printf("%02d:%02d:%02d\n", diff.hours, diff.minutes, diff.seconds);
    
    return 0;
}

ผลลัพธ์

Enter START time (hh mm ss): 12 45 57
Enter STOP time (hh mm ss): 20 35 20

Time Difference: 12:45:57 - 20:35:20 = -08:10:37

หมายเหตุ: ผลลัพธ์เป็นลบ

ตัวอย่าง Start=12:45, Stop=20:35 → Stop มากกว่า Start → Diff เป็นลบ
หากต้องการค่าสัมบูรณ์ ให้สลับ Start/Stop หรือเอาค่าสัมบูรณ์

เวอร์ชันปรับปรุง (Handle Negative & Normalize)

void timeDiffAbsolute(struct Time t1, struct Time t2, struct Time *diff) {
    // แปลงเป็น Total Seconds
    long sec1 = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;
    long sec2 = t2.hours * 3600 + t2.minutes * 60 + t2.seconds;
    
    long diffSec = sec2 - sec1;
    if(diffSec < 0) diffSec = -diffSec;  // Absolute
    
    diff->hours = diffSec / 3600;
    diffSec %= 3600;
    diff->minutes = diffSec / 60;
    diff->seconds = diffSec % 60;
}

การใช้งานจริง

  • Stopwatch / Timer
  • Log Analysis (Duration between events)
  • Scheduling Systems
  • Embedded Systems (RTC)