首页 > 编程 > C > 正文

c语言实现24小时制转换为12小时制示例

2020-01-26 15:32:51
字体:
来源:转载
供稿:网友

分别用三个函数:输入(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";}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选