如何 return 第三个结构中两个结构值之间的差异?
How to return the difference between two struct values in a third struct?
请阅读以下代码并指导我如何 return 将 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;
};
整数除法是你的朋友。您需要执行以下操作:
result.Hours = et / 3600;
et -= (result.Hours * 3600);
result.Minutes = et / 60;
et -= (result.Minutes * 60);
result.Seconds = et;
请阅读以下代码并指导我如何 return 将 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;
};
整数除法是你的朋友。您需要执行以下操作:
result.Hours = et / 3600;
et -= (result.Hours * 3600);
result.Minutes = et / 60;
et -= (result.Minutes * 60);
result.Seconds = et;