c++ 尽管对数学进行了多次调整,但程序似乎计算错误时间

smdnsysy  于 2023-03-14  发布在  其他
关注(0)|答案(1)|浏览(72)

由于某种原因,我的程序似乎认为第二次输入是在第二天而不是同一天,我猜我放错了一些运算符,我真的可以用第二手的观点来看待这个问题。
下面是我的代码:

#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输入(有些问题可能就是因为它),我多次调整它们。我还尝试修改数学,看看它是否会给予我一个更好的结果,但它通常不会很好。
快速编辑:我使用自动评分器的原因是因为我的课需要它。

zkure5ic

zkure5ic1#

简单明了:

#include <iostream>
#include <string_view>
#include <string>

// calcualtes the time difference between the start time and ending time in minutes
int compute_difference( 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 ) noexcept;

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 { compute_difference( 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 compute_difference( 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 ) noexcept
{
    int difference_in_minutes;

    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;

    if ( !is_start_in_pm && start_hour == 12 )
        start_hour = 0;

    if ( !is_end_in_pm && end_hour == 12 )
        end_hour = 0;

    const int total_minutes_of_start { start_hour * 60 + start_minute };
    const int total_minutes_of_end   { end_hour * 60 + end_minute };

    if ( total_minutes_of_end >= total_minutes_of_start )
        difference_in_minutes = total_minutes_of_end - total_minutes_of_start;
    else
        difference_in_minutes = total_minutes_of_start - total_minutes_of_end;

    return difference_in_minutes;
}

请随意测试它,并向我报告它可能存在的任何问题。

相关问题