代码之家  ›  专栏  ›  技术社区  ›  Ruby

如何在第三个结构中返回两个结构值之间的差?[已关闭]

  •  -1
  • Ruby  · 技术社区  · 8 年前

    #include <stdio.h>
    #include <stdlib.h>>
    #include <math.h>
    
    #include "Time.h"
    
    void elapsedTime(struct time time1, struct time time2); // Prototype
    
    int main(){
    
        struct time time1 = {3, 45, 15};
        struct time time2 = {9, 44, 03};
    
        elapsedTime(time1, time2);
    
        return 0;
    }
    
    
    void elapsedTime(struct time time1, struct time time2){
    
        int elapsedTime = ((time1.Hours * MINUTES_IN_AN_HOUR * 
        SECONDS_IN_A_MINUTE) + (time1.Minutes * SECONDS_IN_A_MINUTE) + 
        time1.Seconds) - ((time2.Hours * MINUTES_IN_AN_HOUR * 
        SECONDS_IN_A_MINUTE) + (time2.Minutes * SECONDS_IN_A_MINUTE) + 
        time2.Seconds);
    
        printf(" %d \n", abs(elapsedTime));
        return;
    }
    

    struct time{
        int Hours;
        int Seconds;
        int Minutes;
    };
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Lee Daniel Crocker    8 年前

    整数除法是你的朋友。你需要做以下事情:

    result.Hours = et / 3600;
    et -= (result.Hours * 3600);
    result.Minutes = et / 60;
    et -= (result.Minutes * 60);
    result.Seconds = et;