首页 > 代码库 > convert time-24小时制转换为12小时制
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:\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?
convert time-24小时制转换为12小时制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。