#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string int_to_string (int n)
{
ostringstream outstr;
outstr<<n;
return outstr.str();
}
void read_time(int &hours,int &minutes)
{
string line;
getline(cin,line);
istringstream instr(line);
instr>>hours;
minutes=0;
char ch;
instr.get(ch);
if (ch==':')
instr>>minutes;
else
instr.unget();
string suffix;
instr>>suffix;
if (suffix=="pm")
hours=hours+12;
}
string time_to_string (int hours,int minutes,bool military)
{
string suffix;
if (!military)
{
if (hours<12)
suffix="am";
else
{
suffix="pm";
hours=hours-12;
}
if (hours==0)
hours=12;
}
string result=int_to_string(hours)+":";
if (minutes<10)
result+="0";
result+=int_to_string(minutes);
if (!military)
result=result+" " +suffix;
return result;
}
int main()
{
cout<<"please enter the time:"<<endl;
int hours;
int minutes;
read_time(hours,minutes);
cout<<"military time:"<<time_to_string(hours,minutes,true)<<endl;
cout<<"using am/pm"<<time_to_string (hours,minutes,false)<<endl;
return 0;
}
分钟读不出来