【问题标题】:Am I calling this function wrong?我调用这个函数错了吗?
【发布时间】:2016-07-21 18:35:19
【问题描述】:

我正在编写一个程序来计算两个给定时间之间经过的时间。

由于某种原因,我收到错误:在我的主函数之前我的 elapsedTime 函数原型的预期标识符或“C”。

我已经尝试在程序中移动它,如果在声明 t1 和 t2 之后找到它并没有什么不同。有什么问题?

谢谢

#include <stdio.h>

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

struct elapsedTime(struct time t1, struct time t2);

int main(void)
{

    struct time t1, t2;

    printf("Enter start time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d:%d:%d", &t1.hours, &t1.minutes, &t1.seconds);

    printf("Enter stop time: \n");
    printf("Enter hours, minutes and seconds respectively: ");
    scanf("%d:%d:%d", &t2.hours, &t2.minutes, &t2.seconds);

    elapsedTime(t1, t2);

    printf("\nTIME DIFFERENCE: %d:%d:%d -> ", t1.hours, t1.minutes, t1.seconds);
    printf("%d:%d:%d ", t2.hours, t2.minutes, t2.seconds);
    printf("= %d:%d:%d\n", differ.hours, differ.minutes, differ.seconds);

    return 0;
}

struct elapsedTime(struct time t1, struct time t2)
{
    struct time differ;

    if(t2.seconds > t1.seconds)
    {
        --t1.minutes;
        t1.seconds += 60;
    }

    differ.seconds = t2.seconds - t1.seconds;

    if(t2.minutes > t1.minutes)
    {
        --t1.hours;
        t1.minutes += 60;
    }

    differ.minutes = t2.minutes - t1.minutes;
    differ.hours = t2.hours - t1.hours;

    return differ;
}

【问题讨论】:

  • 那是什么:struct elapsedTime(struct time t1, struct time t2);struct 不是有效类型。
  • @EugeneSh.:这是一个函数声明。 struct 关键字后面的标签time 缺失。
  • 为了将来参考,您应该将确切的错误消息复制并粘贴到您的问题中,包括它所引用的行号。
  • 会做的,谢谢大家

标签: c function structure


【解决方案1】:

您的函数没有正确定义返回类型:

struct elapsedTime(struct time t1, struct time t2);

struct 本身不足以定义返回类型。您还需要结构名称:

struct time elapsedTime(struct time t1, struct time t2);

你还需要将函数的返回值赋给某物:

struct time differ = elapsedTime(t1, t2);

有了这个工作,你在做差异时“借用”的逻辑是倒退的:

if(t1.seconds > t2.seconds)     // switched condition
{
    --t2.minutes;               // modify t2 instead of t1
    t2.seconds += 60;
}

differ.seconds = t2.seconds - t1.seconds;

if(t1.minutes > t2.minutes)     // switched condition
{
    --t2.hours;                 // modify t2 instead of t1
    t2.minutes += 60;
}

照原样,如果t1t2 之后,则小时数将为负数。如果您假设这意味着结束时间是第二天,那么您将 24 添加到小时:

if(t1.hours > t2.hours)
{
    t2.hours+= 24;
}

differ.hours= t2.hours - t1.hours;

【讨论】:

  • 抱歉,借钱?我的代码现在可以工作了(在函数声明和调用修复之后),谢谢...只要第二次输入的时间不超过午夜,否则它认为它回到了时间并输出一个负值。
  • @MattCleary 借用是当两个数字(或数字组)相减导致负值时,您需要添加下一个数字的值。这就是你在加 60 和减 1 时所做的事情。因为你是从 t2 中减去 t1,所以你需要检查 t1 的值是否更大,如果是,请在 t2 上借用,不像你现在做的那样。
  • 我有点困惑。我将if(t2.seconds &gt; t1.seconds) 切换为if(t1.seconds &gt; t2.seconds) 然后重新编译然后运行程序。现在如果 t1 是 '17:52:13' 而 t2 是 '17:52:11',则输出是 TIME DIFFERENCE: 17:52:13 -&gt; 17:52:11 = 0:1:-62 如果我保持原样,我会遇到问题,但反过来,当 t2 > t1
  • @MattCleary 除了切换条件,还需要修改t2而不是t1。然后你提到的输入输出是-1:59:58
  • @MattCleary 如果您的开始时间在结束时间之后并且您不考虑日期环绕,您会得到类似的结果。如果您假设 start > end 表示环绕,则将 24 添加到 t2.hours,您将得到 23:59:58
猜你喜欢
  • 2019-11-11
  • 2014-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多