我知道这可能是一个愚蠢的问题,但我对此有一些麻烦,我很惭愧,但我真的不知道如何做到。我想加减两个以整数形式给出的“小时”。
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
// should be: 21:59:2
int rH, rM, rS;
int h1 = 12, m1 = 34, s1 = 56;
int h2 = 9, m2 = 24, s2 = 6;
if(h1 + h2 >= 24)
{
rH = abs(h2 + h1);
m1 += 60;
if(m1 + m2 >= 60)
{
rH = (m2 - m1);
}
}
else
{
rH = h1 + h2;
}
cout << rH << " " << rM << " " << rS << " " << "\n";
return 0;
}
尝试了上述方法,但我不知道如何进一步做。我不能使用任何date-time
对象。
编辑
减法,我不知道错误在哪里,但它计算错误:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
int rH, rM, rS;
int h1 = 12, m1 = 25, s1 = 29, h2 = 11, m2 = 12, s2 = 1;
int sum1, sum2, sum;
sum1 = (h1)*3600 + (m1)*60 + s1;
sum2 = (h2)*3600 + (m2)*60 + s2;
if(sum1 > sum2)
sum = sum1-sum2;
else
sum = sum2-sum1;
rS = sum %60;
sum = sum/60;
rM = sum % 60;
rH = sum/60;
std::cout << rH << " " << rM << " " << rS << " " << "\n";
return 0;
}