编写一个程序将时间从24小时转换为24小时但是捕获的一切都必须是从输入到结果的函数

时间:2017-12-22 16:07:18

标签: c++

所以基本上我迷路了,我不知道去哪儿原来的问题说: 编写程序将时间从24小时表示法转换为12小时表示法,反之亦然。您的程序必须由菜单驱动,让用户可以选择转换两种表示法之间的时间。此外,您的程序必须至少包含以下功能:将时间从24小时表示法转换为12小时表示法的功能,将时间从12小时表示法转换为24小时表示法的功能,显示功能选择,获取输入的功能和显示结果的功能。 (对于12小时时间表示法,您的程序必须显示AM或PM。)

我想出了如何使它工作但使用的功能少于指定的..如何创建一个具有输出的单独功能?

#include "stdafx.h"
#include <iostream>
using namespace std;

void choice();
void inputincaseof24to12(int hours, int minutes);
void inputincaseof12to24(int hours, int minutes, char ampm);


int main()
{
    int hours = 1;
    int minutes = 0;
    char ampm = 0;
    char yourchoice;
    choice();
    cin >> yourchoice;

    if (yourchoice == 'A')
        inputincaseof24to12(hours, minutes);
    else if (yourchoice == 'B')
        inputincaseof12to24(hours, minutes, ampm);

}

void choice() {
    cout << "in case of conversion to 24 hours from 12 hours please enter 'A' " << endl;
    cout << "in case of comversion to 12 hours from 24 hours please enter 'B' " << endl;
}


void inputincaseof24to12 (int hours, int minutes) {

    cout << "Enter any number of Hours & Minutes to be converted from 24 hours Notation to 12 hours Notation " << endl;
    cin >> hours;
    cin >> minutes;

    if (hours < 12)
        cout << hours << " " << minutes << "A.M" << endl;
    else if (hours == 12)
        cout << hours << " " << minutes << "P.M" << endl;
    else if (hours > 12)
        cout << hours - 12 << " " << minutes << "P.M" << endl;

}



void inputincaseof12to24(int hours, int minutes, char ampm) {

    cout << "Enter any number of Hours & Minutes while considring the AM/PM [where 'A' stands for AM and 'P' stands for PM] state to be converted from 12 hours Notation to 24 hours Notation " << endl;
    cin >> hours;
    cin >> minutes;
    cin >> ampm;

    if (ampm == 'A' && hours <= 11)
        cout << hours << ":" << minutes;
    else if (ampm == 'P' && hours == 12)
        cout << 12 << ":" << minutes;
    else if (ampm == 'P' && hours > 12);
    cout << hours + 12 << ":" << minutes;
}

请注意我在编程时仍然是一个菜鸟所以对我很轻松:)谢谢

1 个答案:

答案 0 :(得分:1)

你需要问一个具体问题,这里没有人会为你做这个任务。我有一些一般性的建议,希望能帮助指导你找到答案。

  1. 清理您的输入。当用户输入他们的时间值时,请确保他们实际上是您正在寻找的范围内的数字。很高兴您有std::cout消息请求,但这不会确保您的用户真正遵守规则。
  2. 整合您的I / O操作。你已经在你的函数中调用了std::cin已经传递这些参数,实际上你正在这样做的方式它们不是通过引用传递的,即int &hours所以即使用户放置了可用的当函数返回时,它们中的值将超出范围。
  3. 考虑用于转换12和24小时时间格式的逻辑。请参阅此图表以供参考。您将了解为什么conversionincaseB的当前实现逻辑不太正确。
  4. 12 and 24 hour time

    1. 您应该为您的功能提供更多描述性名称。没有人知道conversionincaseB应该做什么,但是他们可能能够推断出像convert24HourTime这样的更具描述性的函数名称应该做什么。