gpt4 book ai didi

c - C 中是否有可能有一个函数将这两个结构作为参数?

转载 作者:行者123 更新时间:2023-11-30 16:22:49 27 4
gpt4 key购买 nike

我已经完成了这段代码,但我不喜欢它,我正在尝试学习结构,我想让它变得更简单,是否可以用一个采用两种结构的函数来完成它?

我尝试自己以这种方式做到这一点,但我没有成功,我将不胜感激,因为我无法将两个结构传递到一个函数中......

我的代码:

#include <stdio.h>
int timeDifference (int startTime, int endTime);
int countSeconds (struct time time1);
int timeCount (int inputSeconds);
int abs(int number);

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

struct time time1 = {3,45,15};
struct time time2 = {9,44,03};


int main(int argc, char const *argv[])
{


printf("\nStart Time in seconds = %d", countSeconds(time1));
printf("\nEnd Time in seconds = %d", countSeconds(time2));
printf("\nThe difference in seconds = %d",timeDifference(countSeconds(time2),countSeconds(time1)));
timeCount(timeDifference(countSeconds(time2),countSeconds(time1)));


return 0;
}

//The duration is 5 hours, 58 minutes and 48 seconds

int timeDifference (int startTime, int endTime)
{
int diff;


diff = abs(endTime - startTime); //diff in seconds

return diff;

}

// function to change the time to seconds
int countSeconds (struct time time_)
{
int count;

count = time_.hours*60*60 + time_.minutes*60 + time_.seconds;


return count;

}

//https://stackoverflow.com/questions/30829923/i-want-to-multiply-two-different-structures-in-c-language

int abs(int number)
{
if (number < 0)
number = number * -1;

return number;
}

//program to count hours. min, seconds

int timeCount (int inputSeconds)
{
int h,m,s; //hours, seconds, minutes
int remainingSeconds, secondsInHour = 3600, secondsInMinute = 60;

h = inputSeconds/secondsInHour;
remainingSeconds = inputSeconds - (h * secondsInHour);
m = remainingSeconds/secondsInMinute;
remainingSeconds = remainingSeconds - (m*secondsInMinute);
s = remainingSeconds;

printf("\n%d hour, %d minutes and %d seconds",h,m,s);
}

我希望有一个例子,以便我可以继续研究。

最佳答案

is it possible to do it with one function that takes the two structures

是的,确实如此。如果我可以建议,您可以使用 typedef 来简化结构的使用。

根据我的更改,timeDifference 的函数将作为参数接收两个结构。

#include <stdio.h>

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

typedef struct time TimeStructure;

int timeDifference (TimeStructure startTime, TimeStructure endTime);
int countSeconds (TimeStructure time1);
int timeCount (int inputSeconds);
int abs(int number);

TimeStructure time1 = {3,45,15};
TimeStructure time2 = {9,44,03};

int main(int argc, char const *argv[])
{
printf("\nStart Time in seconds = %d", countSeconds(time1));
printf("\nEnd Time in seconds = %d", countSeconds(time2));
printf("\nThe difference in seconds = %d", timeDifference(time2, time1));
timeCount(timeDifference(time2, time1));
return 0;
}

//The duration is 5 hours, 58 minutes and 48 seconds

int timeDifference(TimeStructure startTime, TimeStructure endTime)
{
return abs(countSeconds(endTime) - countSeconds(startTime));
}

// function to change the time to seconds
int countSeconds(TimeStructure time)
{
return (time.hours * 60 * 60)
+ (time.minutes * 60)
+ time.seconds;
}

//https://stackoverflow.com/questions/30829923/i-want-to-multiply-two-different-structures-in-c-language

int abs(int number)
{
if (number < 0)
number = number * (-1);
return number;
}

//program to count hours. min, seconds

int timeCount(int inputSeconds)
{
int h,m,s; //hours, seconds, minutes
int remainingSeconds, secondsInHour = 3600, secondsInMinute = 60;

h = inputSeconds/secondsInHour;
remainingSeconds = inputSeconds - (h * secondsInHour);
m = remainingSeconds/secondsInMinute;
remainingSeconds = remainingSeconds - (m*secondsInMinute);
s = remainingSeconds;

printf("\n%d hour, %d minutes and %d seconds",h,m,s);
}

您甚至可以在此在线 Playground 中运行这些更改: https://code.sololearn.com/cx4w4AxDg4Lc

关于c - C 中是否有可能有一个函数将这两个结构作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271138/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com