我有一个struct array(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;
这似乎在我眼中是正确的,但它失败了。小时是正确计算的,但不是分钟和秒数:(
答案 0 :(得分:1)
要传递结构,请在通话中命名:
some_function(data[0].time); // By value
other_function(&data[0].time); // By address
这两个函数都传递了Time
结构数组的data[0]
元素中包含的Training
值。
答案 1 :(得分:0)
假设您有一个值,它是自午夜以来的秒数。假设你用小时/分钟/秒定义另一个结构,你可以按如下方式设置这个时钟结构,
typedef struct
{
int hours;
int minutes;
int seconds;
} Clock;
您可以将此结构打印到char缓冲区或stdout
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);
或者您可以格式化字符串以进行打印,或者打印到stdout,
char clockbuffer[30];
clockPrint(&clk,NULL);
printf("time (HH:MM:SS): %s\n", clockPrint(&clk,clockbuffer));