You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++实现24/12小时制时间互转时12转24功能异常求助

问题原因
  • 12小时转24小时的converTo24hours函数核心逻辑错误:没有判断输入的AM/PM标识type参数,错误以小时是否大于12作为加12的判断条件,完全无法识别PM时段
  • 函数代码不完整:输出调用、函数闭合括号都被注释,逻辑执行断裂
  • 边界场景缺失适配:12PM对应24小时制12点无需加12,12AM对应24小时制0点需要特殊处理,原有逻辑均未覆盖
修复后完整代码
#include <iostream>
#include <iomanip>

using namespace std;

void inputChoice(int& choice);
void input12hours(int& hours, int& minutes, char& type);
void input24hours(int& hours, int& minutes);
void converTo12hours(int hours, int minutes);
void converTo24hours(int hours, int minutes, char type);
void output12hours(int hours, int minutes, char type);
void output24hours(int hours, int minutes);

int main() {
    int hours, minutes;
    char type;
    int choice;
    char repeatChoice;
    do {
        inputChoice(choice);
        switch (choice) {
        case 1:
            input24hours(hours, minutes);
            converTo12hours(hours, minutes);
            break;
        case 2:
            input12hours(hours, minutes, type);
            converTo24hours(hours, minutes, type);
            break;
        }
        //repeat confirmation
        cout << "\nDo you want to continue? [y/n]: ";
        cin >> repeatChoice;
    } 
    while (repeatChoice == 'y' || repeatChoice == 'Y');
    return 0;
}

void inputChoice(int& choice) {
    cout << "\n\n";
    cout << "1) Convert 24 hour to 12 hour" << endl;
    cout << "2) Convert 12 hour to 24 hour" << endl;
    cout << "Enter your choice:";
    cin >> choice;
}
//get input in 12 hours format
void input12hours(int& hours, int& minutes, char& type) {
    cout << "\nEnter hours:";
    cin >> hours;
    cout << "\nEnter minutes:";
    cin >> minutes;
    cout << "\nEnter AM/PM [A/P]:";
    cin >> type;
}
//get input in 24 hours format
void input24hours(int& hours, int& minutes) 
{
    cout << "\nEnter hours:";
    cin >> hours;
    cout << "\nEnter minutes:";
    cin >> minutes;
}
//convert 24 hour format to 12 hours format
void converTo12hours(int hours, int minutes) 
{
    char type;
    if (hours < 12) {
        type = 'A';
    }
    else {
        hours = hours - 12;
        if (hours == 0) 
        {
            hours = 12;
        }
        type = 'P';
    }
    output12hours(hours, minutes, type);
}
//convert 12 hour format to 24 hours format
void converTo24hours(int hours, int minutes, char type) 
{
    // PM时段处理
    if (type == 'P' || type == 'p') {
        // 12PM不用加12,其余PM时段小时加12
        if (hours != 12) {
            hours += 12;
        }
    } else {
        // AM时段处理,12AM对应0点
        if (hours == 12) {
            hours = 0;
        }
    }
    output24hours(hours, minutes);
}
//output in 12 hours
void output12hours(int hours, int minutes, char type) 
{
    cout << "\nThe time converted to 12 hour format is: " << hours << ":";
    //special handling for leading 0s on minutes
    cout.width(2);
    cout.fill('0');
    cout << minutes << " ";
    if (type == 'A') 
    {
        cout << "A.M." << endl;
    }
    else {
        cout << "P.M." << endl;
    }
}
//output in 24 hours
void output24hours(int hours, int minutes) {
    cout << "\nThe time converted to 24 hour format is: " << hours << ":";
    //special handling for leading 0s on minutes
    cout.width(2);
    cout.fill('0');
    cout << minutes;
}

内容的提问来源于stack exchange,提问作者geo flem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 21:30:01