所以我有一个项目需要我基本上制作一个时钟/ watch 。代码的默认格式是 24 小时格式,但菜单中的选项 5“切换 24 小时格式”要求将小时格式更改为 12 小时格式。不需要保留之前输入的 24 小时,例如:用户输入 23:45,然后按选项 5,然后按选项 4,不应显示 11:45pm。用户应返回选项 1 以输入新时间并输入“a”以选择上午或“p”以选择下午。我的问题是,如何编写一个 boolean 语句,以便当用户在开关中输入“5”时,void 函数知道格式现在是 12 小时格式,并对等式进行变体处理 12 小时。
我发现发生的问题是 12 小时格式的减法或加法函数。我将如何编写 12 小时加减语句,以便如果时间是 12:30 并添加 45 分钟,则新时间将输出为 1:15 而不是 13:15。
我知道如何去做(我对 boolean 逻辑和函数很糟糕):
bool mode
if (mode==true)
{
\\12 hour format
}
else (mode==false)
{
\\24 hour format
}
但我不知道如何格式化和制作它,所以选项 5 改变了模式。我有点明白如何在 void 函数中做到这一点:
如果模式==true 然后将小时和分钟添加为 12 小时格式
但又一次,对如何做到这一点感到困惑。我查看了大约 6 天前发布的类似问题,但答案并不令我满意,并且只处理了一个输入并进行了设置,因此用户必须输入 24 或 12 来表示时间,而不是将其设置为选项5 切换到模式。它确实使用了:
mode= !mode
这看起来很有希望,如果选项 5 被切换,用户可以再次输入选项 5 并再次切换格式,但是再次:我如何才能让选项 5 切换模式?
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew);
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew);
void getTime(int &h, int &m, bool mode);
void rollTimeForward(int& h, int& m);
void rollTimeBack(int& h, int& m);
void printTime(int h, int m, bool mode);
void calcDeltaFutr(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew)
{
cout << "Enter a new time:" << endl << "Hours: ";
cin >> deltaH;
cout << "Minutes: ";
cin >> deltaM;
hNew = h + deltaH;
mNew = m + deltaM;
rollTimeForward(hNew, mNew);
cout << "The new time is: " << hNew << ":" << mNew << endl;
return;
}
void calcDeltaPast(int h, int m, int deltaH, int deltaM, int& hNew, int& mNew)
{
cout << "Enter a new time:" << endl << "Hours: ";
cin >> deltaH;
cout << "Minutes: ";
cin >> deltaM;
hNew = h - deltaH;
mNew = m - deltaM;
rollTimeBack(hNew, mNew);
cout << "The new time is: " << hNew << ":" << mNew << endl;
return;
}
void getTime(int &h, int &m, bool mode)
{
cout << "Enter a time (hours, minutes): ";
cin >> h;
cin >> m;
if (h>23 || m>59 || h<0 || m<0)
{
cout << "Invalid input! Please input a value for hours below 24 and a value of minutes below 60." << endl;
}
return;
}
void rollTimeForward(int& h, int& m)
{
h = h + m / 60;
m = m % 60;
h = h % 24;
return;
}
void rollTimeBack(int& h, int& m)
{
h = h - m / 60;
m = m % 60;
return;
}
void printTime(int h, int m, bool mode)
{
cout << "The current time is: ";
cout << h << ":" << m << endl;
}
int h, m, deltaH, deltaM, hNew, mNew;
bool mode;
char time;
int main()
{
int choice;
do
{
cout << "-----MENU-----" << endl << endl << "1-Enter a time" << endl << "2-Add delta to time" << endl << "3-Subtract delta from time" << endl << "4-Display current time" << endl << "5-Toggle 24 hour mode" << endl << "6-exit" << endl;
cin >> choice;
switch (choice)
{
case 1:
getTime(h, m, mode);
continue;
case 2:
calcDeltaFutr(h, m, deltaH, deltaM, hNew, mNew);
continue;
case 3:
calcDeltaPast(h, m, deltaH, deltaM, hNew, mNew);
continue;
case 4:
printTime(h, m, mode);
continue;
case 5:
cout << "12 hour mode turned on" << endl;
continue;
case 6:
cout << "Exiting...";
break;
}
if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5 && choice != 6)
{
cout << "Invalid input! Please try again!" << endl << endl;
}
else
{
break;
}
} while (choice != 6);
return 0;
}
最佳答案
您的打印时间函数对模式没有任何作用。
void printTime (int h, int m, bool mode)
{
if (mode) // assuming that when mode is true it means 24 hour time
{
//print time in 24 hour format
}
else
{
//print time in 12 hour format
}
}
您的获取时间也需要做类似的事情,因为截至目前,您只能使用 24 小时格式设置它。
在案例 5 的 switch 语句中,您想要切换模式值。 mode = !mode 只要 mode 已经分配了一个 boolean 值就可以工作。
边注: 如果函数在定义之前被调用,则只需要函数原型(prototype)。
关于c++ - 在开关菜单中将时间方程的 24 小时格式更改为 12 小时格式的 boolean 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40534188/