convert time-24時間制を12時間制に変換
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:
";
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?