如何在第三个结构中返回两个struct值之间的差异?

时间:2017-10-03 23:12:49

标签: c function struct structure

请阅读以下代码并指导我如何将int elapsedTime作为结构类型返回为第三次结构例如time3并显示它?

#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 个答案:

答案 0 :(得分:1)

整数师是你的朋友。您需要执行以下操作:

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