由于某种原因,我的程序似乎认为第二次输入是在第二天而不是同一天,我猜我放错了一些运算符,我真的可以用第二手的观点来看待这个问题。
下面是我的代码:
#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;
}
我还提供了当前输出的图像以及输出应该是什么。Image of what was inputted, the current output, and what the output should be
图像上显示的文本:
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
输入(有些问题可能就是因为它),我多次调整它们。我还尝试修改数学,看看它是否会给予我一个更好的结果,但它通常不会很好。
快速编辑:我使用自动评分器的原因是因为我的课需要它。
1条答案
按热度按时间zkure5ic1#
简单明了:
请随意测试它,并向我报告它可能存在的任何问题。