【问题标题】:Is it possible a function that takes the two structures as argument in C?是否有可能在 C 中将两个结构作为参数的函数?
【发布时间】:2019-06-13 17:33:32
【问题描述】:

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

我自己尝试过以这种方式进行操作,但我没有成功,如果我无法将两个结构传递给一个函数,我将不胜感激......

我的代码:

#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);
}

我会很感激一个例子,以便我可以研究它。

【问题讨论】:

  • 你可以写一个像int timeDifference (struct time startTime, struct time endTime)这样的函数,除非我误解了你的问题。你问的是这个吗?
  • 你的意思是int timeCount(struct time t1, struct time t2) { /* pseudocode */ return countSeconds(t2) - countSeconds(t1); }

标签: c function structure


【解决方案1】:

有没有可能用一个函数来完成这两个函数 结构

是的,是的。如果我可以建议,您可以使用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);
}

您甚至可以在这个在线游乐场中运行这些更改: https://code.sololearn.com/cx4w4AxDg4Lc

【讨论】:

  • 所以秘诀就是使用 typedef。我试图通过传递两个结构来做到这一点,类似于上面显示的 Kamils 选项,但它根本不起作用。谢谢大家的建议和帮助,不胜感激!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多