出于某种原因,我的程序似乎认为第二次输入是第二天而不是同一天。我猜我放错了一些操作员,我真的可以在这个问题上使用二手视角。
这是我的代码:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int hours, minutes;
bool isAm;
int computeDifference(int Hour1, int Hour2, int Min1, int Min2, bool isPm1, bool isPm2);
// calcualtes the time difference between the start time and ending time in minutes
int main()
{
int Hour1, Hour2, Min1, Min2;
bool isPm1 = 0, isPm2 = 0;
char Time1, Time2;
char ch = ':';
cout << "Enter start time, in the format 'HH:MM xm', where 'xm' is \n";
cout << "either 'am' or 'pm' for AM or PM: \n";
cin >> Hour1 >> ch >> Min1 >> Time1;
cin.ignore(1,'\n');
cout << "Enter future time in the format 'HH:MM xm' where 'xm' is \n";
cout << "either 'am' or 'pm': \n";
cin >> Hour2 >> ch >> Min2 >> Time2;
cout << "There are " << computeDifference(Hour1, Min1, Hour2, Min2, isPm1, isPm2) << " minutes " << "between " << Hour1 << ch << Min1 << Time1 << " and " << Hour2 << ch << Min2 << Time2 << "." << endl;
return 0;
}
int computeDifference(int Hour1, int Hour2, int Min1, int Min2, bool isPM1, bool isPM2)
{
int Difference, Minutes1, Minutes2;
if (isPM1)
{
if ((Hour1 >= 1) && (Hour1 < 12))
{
Hour1 += 12;
}
}
if (isPM2)
{
if ((Hour2 >= 1) && (Hour2 < 12))
{
Hour2 += 12;
}
}
Minutes1 = (Hour1 * 60) + Min1;
Minutes2 = (Hour2 * 60) + Min2;
if ((Hour1 >= Hour2) || ((Hour1 == Hour2) && (Min1 > Min2)))
{
Minutes2 += 1440;
}
Difference = Minutes2 - Minutes1;
if (Difference > 1440)
{
Difference -= 1440;
}
return Difference;
}
图片上显示的文字:
Given the following was entered
from the keyboard:
09:30◦am⏎
09:31◦am⏎
you displayed:
Enter◦start◦time,◦in◦the◦format◦'HH:MM◦xm',◦where◦'xm'◦is◦⏎
either◦'am'◦or◦'pm'◦for◦AM◦or◦PM:◦⏎
Enter◦future◦time◦in◦the◦format◦'HH:MM◦xm'◦where◦'xm'◦is◦⏎
either◦'am'◦or◦'pm':◦⏎
There◦are◦1282◦minutes◦between◦9:30a◦and◦9:31a.⏎
instead of:
Enter◦start◦time,◦in◦the◦format◦'HH:MM◦xm',◦where◦'xm'◦is◦⏎
either◦'am'◦or◦'pm'◦for◦AM◦or◦PM:◦⏎
Enter◦future◦time◦in◦the◦format◦'HH:MM◦xm'◦where◦'xm'◦is◦⏎
either◦'am'◦or◦'pm':◦⏎
There◦are◦1◦minutes◦(0◦hours◦and◦1◦minute)◦between◦9:30◦AM◦and◦9:31AM.⏎)
我一开始以为是
cin
输入的问题(有些问题可能是因为它),我调整了很多次。我也尝试修改数学以查看它是否会给我更好的结果,但通常结果并不好。
快速编辑:我使用自动评分器的原因是因为我的课程需要它。
有点简化和清洁:
#include <iostream>
#include <string_view>
#include <string>
// calcualtes the time difference between the start time and ending time in minutes
int computeDifference( const int start_hour, const int start_minute, const bool is_start_in_pm,
const int end_hour, const int end_minute, const bool is_end_in_pm );
int main( )
{
constexpr std::string_view am_uppercase { "AM" };
constexpr std::string_view pm_uppercase { "PM" };
constexpr std::string_view am_lowercase { "am" };
constexpr std::string_view pm_lowercase { "pm" };
char colon { };
int start_hour;
int start_minute;
std::string start_period_format;
std::cout << "Enter start time, in the format 'HH:MM xm', where 'xm' is \n"
"either 'am' or 'pm' for AM or PM: \n";
std::cin >> start_hour >> colon >> start_minute >> start_period_format;
int end_hour;
int end_minute;
std::string end_period_format;
std::cout << "Enter future time in the format 'HH:MM xm' where 'xm' is \n"
"either 'am' or 'pm': \n";
std::cin >> end_hour >> colon >> end_minute >> end_period_format;
const bool is_start_in_pm { ( start_period_format == pm_lowercase ) ? true : false };
const bool is_end_in_pm { ( end_period_format == pm_lowercase ) ? true : false };
const int difference_in_minutes { computeDifference( start_hour, start_minute, is_start_in_pm,
end_hour, end_minute, is_end_in_pm ) };
std::cout << "There are "
<< difference_in_minutes
<< " minutes "
<< "(" << difference_in_minutes / 60 << " hours and " << difference_in_minutes % 60 << " minute) "
<< "between "
<< start_hour << ':' << start_minute << ( is_start_in_pm ? pm_uppercase : am_uppercase )
<< " and "
<< end_hour << ':' << end_minute << ( is_end_in_pm ? pm_uppercase : am_uppercase )
<< ".\n";
}
int computeDifference( int start_hour, const int start_minute, const bool is_start_in_pm,
int end_hour, const int end_minute, const bool is_end_in_pm )
{
int difference;
if ( is_start_in_pm && (start_hour >= 1) && (start_hour <= 11) )
start_hour += 12;
if ( is_end_in_pm && (end_hour >= 1) && (end_hour <= 11) )
end_hour += 12;
int minutes1 { start_hour * 60 + start_minute };
int minutes2 { end_hour * 60 + end_minute };
if ( (start_hour >= end_hour) || ( (start_hour == end_hour) && (start_minute > end_minute) ) )
minutes2 += 1440;
difference = minutes2 - minutes1;
if ( difference > 1440 )
difference -= 1440;
return difference;
}
现在请记住,我真的没有改变你
computeDifference
功能的逻辑。只有一点重构不会影响它的行为。所以它可能仍然存在错误或边缘情况。我没有以有意义的方式对其进行测试,这实际上是您的责任,使其尽可能正确。