嗨,我是C++新手 - 我想制作一个程序来使用HH:MM格式添加和减去时间。 样品输入:添加和减去时间
12:00 + 3:00 - 6:00
样本输出:
9:00
另一个样品输入:
9:00 + 8:00 + 13:00 - 2:25
样本输出:
27:35
我如何得到这个?我正在考虑将所有东西都转换为秒,然后应用数学运算,然后使用模数60函数返回时间。任何帮助建立这样一个程序?
嗨,我是C++新手 - 我想制作一个程序来使用HH:MM格式添加和减去时间。 样品输入:添加和减去时间
12:00 + 3:00 - 6:00
样本输出:
9:00
另一个样品输入:
9:00 + 8:00 + 13:00 - 2:25
样本输出:
27:35
我如何得到这个?我正在考虑将所有东西都转换为秒,然后应用数学运算,然后使用模数60函数返回时间。任何帮助建立这样一个程序?
最简单的解决方案只是解析输入到整数(使用std::istream
),它们插入到tm
(在<time.h>
定义的类型),并调用mktime
(也在<time.h>
)。 (在C++ 11中有一些处理时间的新东西,但我还没有真正熟悉它。)
您需要考虑'time'的含义。有两个概念,时间点和持续时间。加上或减去彼此的时间点没有任何意义。它是有道理的加减持续时间(导致一个持续时间),它是有道理的,加上和减去一个时间点的持续时间(导致一个时间点
许多时间的API不会很好这两个概念之间的工作区分,但标准C++ <chrono>
库做的相当好。
下面是一些代码,滥用的C tm
类型,以便从用户那里得到一对夫妇的持续时间,将它们相加,然后虐待再次tm
输出结果
#include <iostream> // cout, cin
#include <iomanip> // get_time, put_time
#include <chrono> // hours, minutes, duration_cast
int main() {
// input, get a couple durations to do arithmetic on
// technically std::tm represents a time point and get_time is supposed to
// parse a time point, but we treat the results as a duration
std::tm t;
std::cin >> std::get_time(&t, "%H:%M");
auto duration1 = std::chrono::hours(t.tm_hour) + std::chrono::minutes(t.tm_min);
std::cin >> std::get_time(&t, "%H:%M");
auto duration2 = std::chrono::hours(t.tm_hour) + std::chrono::minutes(t.tm_min);
// do the arithmetic
auto sum = duration1 + duration2;
// output
auto hours = std::chrono::duration_cast<std::chrono::hours>(sum);
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(sum - hours);
t.tm_hour = hours.count();
t.tm_min = minutes.count();
std::cout << std::put_time(&t, "%H:%M") << '\n';
}
I w以共享一些代码作为程序的问题请求作为C++的新手。这不是一个完美的代码,但对于新来的C++人来说可能是一个很好的实践。它将解决时间戳的加法和减法。即你可能需要添加额外的验证,并可以扩展到代表天,秒和毫秒...
#include <iostream>
#include <string>
using namespace std;
/// Represents the timestamp in HH:MM format
class Time {
public:
// Construct the time with hours and minutes
Time(size_t hours, size_t mins);
// Construct the time using a string
Time(const string& str);
// Destructor
~Time() {}
// Add a given time
Time operator + (const Time& rTime) const;
// Subtract a given time
Time operator - (const Time& rTime) const;
// Get the members
int Hours() const { return m_Hours; }
int Minutes() const { return m_Minutes; }
// Get the time as a string in HH:MM format
string TimeStr();
private:
// Private members
int m_Hours; // Hours
int m_Minutes; // Minutes
};
// Constructor
Time::Time(size_t hours, size_t mins) {
if (hours >= 60 || mins >= 60) {
cout << "Invalid input" << endl;
exit(0);
}
// Update the members
m_Hours = hours;
m_Minutes = mins;
}
// Add the given time to this
Time Time::operator + (const Time& rTime) const {
// Construct the time
int nTotalMinutes(m_Minutes + rTime.Minutes());
int nMinutes(nTotalMinutes % 60);
int nHours(nTotalMinutes/60 + (m_Hours + rTime.Hours()));
// Return the constructed time
return Time(nHours, nMinutes);
}
// Construct the time using a string
Time::Time(const string& str) {
if(str.length() != 5 || str[2] != ':') {
cout << "Invalid time string. Expected format [HH:MM]" << endl;
exit(0);
}
// Update the members
m_Hours = stoi(str.substr(0, 2));
m_Minutes = stoi(str.substr(3, 2));
}
// Substact the given time from this
Time Time::operator - (const Time& rTime) const {
// Get the diff in seconds
int nDiff(m_Hours*3600 + m_Minutes*60 - (rTime.Hours()*3600 + rTime.Minutes()*60));
int nHours(nDiff/3600);
int nMins((nDiff%3600)/60);
// Return the constructed time
return Time(nHours, nMins);
}
// Get the time in "HH:MM" format
string Time::TimeStr() {
// Fill with a leading 0 if HH/MM are in single digits
return ((m_Hours < 10) ? string("0") + to_string(m_Hours) : to_string(m_Hours))
+ string(":")
+ ((m_Minutes < 10) ? string("0") + to_string(m_Minutes) : to_string(m_Minutes));
}
int main() {
Time time1("09:00"); // Time 1
Time time2("08:00"); // Time 2
Time time3("13:00"); // Time 3
Time time4("02:25"); // Time 4
//time1 + time 2 + time3 - time4
cout << (time1 + time2 + time3 - time4).TimeStr() << endl;
return 0;
}
输出: 27:35
好像你已经得到它。祝你好运!。 – littleadv
唯一的问题是,我需要使用下面的头文件和时间类 – Emptypeace
你不'需要'的时间课这个..这将是一个浪费时间..(没有双关语意图)..你是什么要求是非常基本的,可以通过正常的算术运算轻松实现。 – iKlsR