设计一个结构来存储时间和日期.写一个函数来计算两个时间之间的差(以分钟为单位)
Design a structure to store time and date. Write a function to find the difference between two times in minutes
我正在自己完成一本O'Reilly的教科书,我现在正在学习结构。其中一个编程练习是:
设计一个结构来存储时间和日期。写一个函数求两个时间的差(以分钟为单位)我相信我已经把结构部分写下来了,但我对差异函数感到困惑。我太懒了,没有考虑到分开的天数,但是这个问题要求分开的时间,所以我要假装他们说的只是24小时。我可以在函数的参数中调用结构吗?我当然尽力了。任何建议都会有所帮助。由于
我的代码到目前为止(没有完成):
#include <iostream>
int difference(struct date_time);
int main()
{
return 0;
}
struct date_time{
int day;
char month[20];
int year;
int second;
int minute;
int hour;
} super_date_time = {
29,
"may",
2013,
30,
30,
23
};
int difference(date_time)
{
int second1 = 45;
int minute1 = 50;
int hour1 = 24;
std::cout << "Time difference is " << hour1 - int hour
return 0;
}
坚持你的数据结构…
// Passing your structures by reference (&)
double MA_TimeDiffMinutes(const struct date_time& t1, const struct date_time& t2) {
// As per your instruction, ignore year, month, day
int diff = ((t1.hour - t2.hour)*60 + t1.minute - t2.minute)*60 + t1.second - t2.second;
return diff/60.0;
}
int main() {
struct date_time t_first;
struct date_time t_next;
// TBD fill the fields of t_first and t_next.
cout << MA_TimeDiffMinutes(t_next, t_first) << endl;
}
考虑使用整数形式的月份而不是字符串。
是,你可以把结构作为参数传递给函数。
process(struct date_time T1) or
process(struct date_time *T1) (struct pointer)
可以通过使用像
这样的函数来计算差值difference(struct date_time *T1, struct date_time *T2) { //T2 is recent time
//process...
std::cout<<"differ: "<<T2->hour-T1->hour<<"h "<<T2->minute-T1->minute<<"m "<<T2->seconds-T1->seconds<<"s "<<endl;
}
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- ::grpc::ServerReaderWriter 对象在另一个线程中一段时间后无法调用
- 如果你有一个固定大小的数组,你需要遍历它!n次,使用二进制搜索如何改变时间复杂性
- 超过CPU时间限制:当MPI_Sent一个非常大的int*时
- 我们可以在队列前面以 O(1) 时间复杂度排队一个元素吗?
- 给定的两个代码有什么区别.一个在 ideone 上运行时超出时间限制,另一个工作正常
- 将 lambda 函数转换为另一个编译单元中的普通函数会缩短编译时间吗?
- 在模板中,有没有办法为每个时间实例化只编写一个专用化?(纳秒、毫秒、秒等)
- C 中是否有一个无法更改的时间戳?(Internet独立)
- C++将时间从一个环境转换为另一个环境
- 一个足够大的问题大小 C++ 需要 0 个运行时间
- 一个循环,可以打印所有项目(无论链条多长时间) *指针
- 为什么glm :: mat3和glm :: value_ptr会在时间和宇宙中创建一个黑洞,并且正在破坏我的脑海中
- 为什么我的多线程示例需要与任何一个相同的时间
- 当该应用程序中加速文件的最后一个修改时间发生更改时,如何重新启动应用程序
- 我如何仅使用一个Arduino uno才能在同一时间和同一方向上旋转两个步进电动机
- C++:将当前时间与下一个工作时间进行比较
- 有没有一个C++等同于逻辑的时间