日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++24小时制转换成12小时制

發布時間:2023/12/20 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++24小时制转换成12小时制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

英文題目(老師給的原版題目):

Write a program that converts from 24-hour notation to 12-hour notation.For example,it should convert 14:25 to 2:25 PM.The input is given as two integers.There should be at least three functions,one for input,one to do the conversion,and one for output.Record the AM/PM information as a value of type char,’A’ for AM and ‘P’ for PM.Thus,the function for doing the conversions will have a call-by-reference formal parameter of type char to record whether it is AM or PM.(The function will have other parameters as well.)Include a loop that lets the user repeat this computation for new input values again and again until the user says he wants to end the program.

中文題目(簡單翻譯一下):

將24小時制轉換為12小時制的程序。例如,它應該將14:25轉換為2:25 PM。輸入是兩個整數。應該至少有三個函數,一個用于輸入,一個用于轉換,一個用于輸出。將AM/PM信息記錄為char類型的值,’ A '表示AM, ’ P '表示PM。因此,用于進行轉換的函數將具有一個char類型的按引用調用形參,以記錄它是AM還是PM。(該函數還有其他參數。)包含一個循環,讓用戶對新的輸入值一次又一次地重復這個計算,直到用戶說他想結束程序。

代碼:

#include <iostream>using namespace std; int time_24_hour,time_24_minute,time_12_hour,time_12_minute;void Input(){cout<<"請輸入時間(24小時制,小時和分鐘之間用空格區分):"<<endl;cin>>time_24_hour;cin>>time_24_minute;if(time_24_minute<0||time_24_hour<0||time_24_hour>24||time_24_minute>60){cout<<"輸入錯誤程序退出!"<<endl;exit(0);} }void Output(){char time_quanyum;if(time_24_hour>=12){time_quanyum='P';}else{time_quanyum='A';}if(time_12_minute<10){cout<<"轉換成12小時制之后的時間為 "<<time_12_hour<<":0"<<time_12_minute<<" "<<time_quanyum<<endl;}else{cout<<"轉換成12小時制之后的時間為 "<<time_12_hour<<":"<<time_12_minute<<" "<<time_quanyum<<endl;} }void Conversion(){if(time_24_hour<=12){time_12_hour=time_24_hour;}else{time_12_hour=time_24_hour-12;}time_12_minute=time_24_minute; }int main() {char choice;do{Input();Conversion();Output();cout<<"是否繼續轉換?(y或n)"<<endl;cin>>choice;if(choice!='y'&&choice!='n'){cout<<"輸入錯誤程序退出!"<<endl;exit(0);}}while(choice=='y');cout<<"退出成功!"<<endl;return 0; }

總結

以上是生活随笔為你收集整理的C++24小时制转换成12小时制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。