在C ++中将时间从24小时制转换为12小时制
在本教程中,我们将讨论将时间从24小时制转换为12小时制的程序。
为此,我们将提供24小时格式的特定时间。我们的任务是将其转换为带有“AM”或“PM”扩展名的12小时格式。
示例
#include <bits/stdc++.h>
using namespace std;
//转换为12小时格式
void convert_12hour(string str){
int h1 = (int)str[0] - '0';
int h2 = (int)str[1] - '0';
int hh = h1 * 10 + h2;
//找到扩展名
string Meridien;
if (hh < 12) {
Meridien = "AM";
}
else
Meridien = "PM";
hh %= 12;
if (hh == 0) {
cout << "12";
for (int i = 2; i < 8; ++i) {
cout << str[i];
}
} else {
cout << hh;
for (int i = 2; i < 8; ++i) {
cout << str[i];
}
}
cout << " " << Meridien << '\n';
}
int main(){
string str = "17:35:20";
convert_12hour(str);
return 0;
}输出结果
5:35:20 PM
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬