c說話完成24小時制轉換為12小時制示例。本站提示廣大學習愛好者:(c說話完成24小時制轉換為12小時制示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c說話完成24小時制轉換為12小時制示例正文
分離用三個函數:輸出(time_input)、輸入(time_output)、轉換(time_change)函數
#include<iostream>
#include<cstdlib>
using namespace std;
void time_input(int& hour,int& minute);
void time_output(int& hour,int& minte,char& noon);
void time_change(int& hour,int& minte,char& noon);
int main()
{
int hour,minute;
char p;
char noon;
do{
time_input(hour,minute);
time_change(hour,minute,noon);
time_output(hour,minute,noon);
cout<<"Enter 'r' to again. ";
cin>>p;
}while(p=='R'||p=='r');
system("pause");//某些編譯器此處可以不要
return 0;
}
void time_input(int& hour,int& minute)
{
cout<<"Enter hour:";
cin>>hour;
cout<<"Enter minte:";
cin>>minute;
}
void time_output(int& hour,int& minute,char& noon)
{
cout<<"12-hours: "
<<hour
<<":"
<<minute
<<" "
<<noon
<<endl;
}
void time_change(int& hour,int& minute,char& noon)
{
if((hour>12&&hour<=24)&&(minute>=0&&minute<=60))
{
noon='P';
hour-=12;
}
else if((hour>=0&&hour<12)&&(minute>=0&&minute<=60)) {noon='A';}
else {cout<<"Time is error!\n";}
}