convert time-24小时制转换为12小时制,允许重复计算:
//convert time
#include<iostream>
using namespace std;
void get_time(int& hours,int& minutes);
void convert_time(int& hours,char& ampm);
void show_time(int hours,int minutes,char ampm);
int main()
{
int hours,minutes;
char ans,ampm;
do
{
get_time(hours,minutes);
convert_time(hours,ampm);
show_time(hours,minutes,ampm);
cout<<"Do you want again?";
cin>>ans;
}while(‘y‘ == ans || ‘Y‘ == ans);
}
void get_time(int& hours,int& minutes)
{
char b;
cout<<"Enter the time like 11:11 format:\n";
cin>>hours>>b>>minutes;
}
void convert_time(int& hours,char& a)
{
if(hours < 12)
a = ‘A‘;
else if(hours >12)
{
hours -=12;
a = ‘P‘;
}
else if(hours == 12)
{
a = ‘P‘;
}
}
void show_time(int hours,int minutes,char ampm)
{
cout<<"The convert time is "<<hours<<":"<<minutes<<" "<<ampm<<"M"<<endl;
}结果:
Enter the time like 11:11 format: 11:30 The convert time is 11:30 AM Do you want again?Y Enter the time like 11:11 format: 15:30 The convert time is 3:30 PM Do you want again?Y Enter the time like 11:11 format: 12:30 The convert time is 12:30 PM Do you want again?Y Enter the time like 11:11 format: 0:40 The convert time is 0:40 AM Do you want again?
原文地址:http://9320314.blog.51cto.com/9310314/1547068