0

我有一个结构数组 ( Training data[10]),其中包含一些我想传递给函数的数据。

int convertTime(Time time)
{

    minutes = time.seconds * 60;
    // Takes data from data[0].time.seconds and converts them to minutes. 
    // My only problem is that I am being asked send a struct to this function, but I have to send the array because that's where all my data is stored

    return minutes;
}

typedef struct
{
    int seconds;
} Time;

typedef struct
{
    Time time;
    double distance;
 } Training;

 Training data[10];

 Training input;

 scanf("%d %lf", input.time.seconds, input.distance);

 data[0].time.seconds = input.time.seconds;
 data[0].distance = input.distance;

data[0].time.seconds所以现在data[0].distance包含我需要的所有数据。我只需要传递data[0].time.seconds给函数,但在我的作业中,我被提示将结构发送Time给函数,我不明白,因为Time它只是存储临时数据?这是我要发送给函数的存储数据。

如何将秒转换为小时、分钟和秒?

time.hours = seconds / 3600;
time.minutes = (seconds - time.hours * 3600) / 60;
time.seconds = seconds - 3600 * time.hours - 60 * time.minutes;

这在我看来似乎是正确的,但它失败了。小时是正确计算的,但不是分钟和秒:(

4

2 回答 2

1

要传递结构,请在调用中为其命名:

 some_function(data[0].time);   // By value
 other_function(&data[0].time); // By address

这两个函数都传递了结构数组元素中Time包含的值。data[0]Training

于 2013-11-12T22:34:54.003 回答
0

假设您有一个值,它是自午夜以来的秒数。并且假设您定义了另一个带有小时/分钟/秒的结构,您可以将这个时钟结构设置如下,

typedef struct
{
    int hours;
    int minutes;
    int seconds;
} Clock;

您可以将此结构打印到字符缓冲区或标准输出,

char*
clockPrint(Clock* timep,char *stringbuf)
{
    if(stringbuf)
        sprintf(stringbuf,"%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
    else
        printf("%02d:%02d:%02d",(timep)->seconds,(timep)->minutes,(timep)->seconds);
    return stringbuf;
}

可以从纪元时间或自午夜以来的秒数中提取小时、分钟和秒,

int //return days...
TimeSet(Clock* timep, int epoch)
{
    (timep)->seconds = (epoch) % 60;
    (timep)->minutes = (epoch/60) % 60;
    (timep)->hours   = (epoch/60/60) % 24;
    int days;
    return days   = (epoch/60/60/24);
}

如果您想从此时钟值中获取小时、分钟或秒,

void
TimeGet(Clock* timep, int* hoursp, int* minutesp, int* secondsp)
{
    if(hoursp)   *hoursp   = (timep)->hours;
    if(minutesp) *minutesp = (timep)->minutes;
    if(secondsp) *secondsp = (timep)->seconds;
    return;
}

由于您已经在 Date 结构中存储了一个 Time ,其中包含几秒钟(大概是从午夜开始),并且您定义了一些这些 Date 的数组,

Training data[10];
Training input;

您可以使用 scanf 读取您的秒数和距离值。如前所述,您可以将输入放入 data[0] 元素中,

//scanf wants pointers to int and float data
float distance;
printf("enter: seconds distance "); fflush(stdout);
scanf("%d %lf", &(input.time.seconds), &distance);
//you can then store the distance into your input struct double
input.distance = distance;

data[0].time.seconds = input.time.seconds;
data[0].distance = input.distance;

您还可以使用 gettimeofday(3) 或 clock_gettime(2) 来获取当前时间(自纪元以来的秒数),

struct timeval tv;
gettimeofday(&tv,NULL);    //posix.1-2001 function, seconds
input.time.seconds = tv.tv_sec;
//or
struct timespec ts;
clock_gettime(CLOCK_REALTIME,&ts); //posix.1-2008 function, seconds
input.time.seconds = ts.tv_sec;

然后你可以把你的秒分成小时、分钟和秒,

Clock clk;
int hours, minutes, seconds;
TimeSet(&clk, data[0].time.seconds);
TimeGet(&clk, &hours, &minutes, &seconds);

或者你可以格式化一个字符串进行打印,或者打印到标准输出,

char clockbuffer[30];
clockPrint(&clk,NULL);
printf("time (HH:MM:SS): %s\n", clockPrint(&clk,clockbuffer));
于 2013-11-13T00:57:18.757 回答