Professional Documents
Culture Documents
Week8 Exercise
Week8 Exercise
2. 判斷時間之時、分、秒是否合理,若有任何錯誤告
知「時、分、秒」哪幾個項目有誤。
Exercise 1 程式執行成果
一併顯示使用者輸入的無效參數
錯誤訊息
12 小時制小時欄位需介於 1~12
之間,並顯示 AM 或 PM
輸入 exit 關閉程式
Exercise 1 程式功能規劃
1. Time.h & Time.cpp(功能介面)
1) set 方法參數為 24 小時制時間(時、分、秒),並儲存至
private 屬性中,若參數超出範圍則丟回 exception 訊息,
一併告知所有有誤的參數項目(Hour、Minute 或 Second)
2. main.cpp (功能測試)
(1) 建立 Time 物件 (2) 使用者輸入、輸出訊息 (3) 字串分割
Exercise 1 程式碼:Time.h
#ifndef TIME_H // 判斷 m_hour 並修改以符合 12 小時
#define TIME_H // 制,儲存至 returnString,再
// 加入 AM 或 PM 字串後回傳
#include <iostream> std::string Get12HourMode();
#include <string>
#include <iomanip> private:
#include <stdexcept> // 成員變數
unsigned short m_hour;
class Time unsigned short m_minute;
{ unsigned short m_second;
public: // Exception 與 Get12HourMode()
// 判斷 hour, minute, second 是否正確 // 所回傳之字串
// ,若任一參數錯誤,將參數錯誤訊息累 std::string returnString;
// 加至 returnString 中,再一起回傳 };
void SetTime(unsigned short hour,
unsigned short minute, unsigned short #endif // TIME_H
second);
Exercise 1 程式碼:Time.cpp
#include "Time.h" // 不合理:添加錯誤訊息至 returnString
using namespace std;
// 若有錯誤,throw 錯誤訊息
void Time::SetTime(unsigned short hour, if (returnString != "null")
unsigned short minute, unsigned short {
second)
returnString += " 設定錯誤!";
{
throw invalid_argument(returnString);
// 預設沒有錯誤發生
}
returnString = "null";
}
// 不合理的 hour,添加至錯誤訊息中
if (hour >= 24)
string Time::Get12HourMode()
returnString = "Hour";
{
// 合理的 hour 儲存至 m_hour
// TO DO: 回傳字串符合格式:
else
// hh:mm:ss [AM/PM] (hh: 1~12)
m_hour = hour;
// returnString = ...
// TO DO: 完成 minute 與 second
// return returnString;
// 的合理性判斷
}
// 合理:儲存至 m_minute 與 m_second
Exercise 1 程式碼:main.cpp
#include "Time.h" else if (SplitUserInput(user_input,
using namespace std; hour, minute, second) == false)
// 依照使用者輸入的字串 user_input, 切分為 hour, // 若使用者輸入錯誤,要求重新輸入
minute, second 並儲存到個別參數 continue;
bool SplitUserInput(const string &user_input, try
unsigned short &hour, unsigned short &minute,
unsigned short &second) {
{ /*請從 main.cpp 超連結複製完整程式*/ } time.SetTime(hour, minute, second);
cout << "轉換為 12 小時制為:"
int main() << time.Get12HourMode()
{ << endl << endl;
Time time; }
string user_input; catch (invalid_argument e)
unsigned short hour, minute, second; {
do cout << "無效參數: " << e.what()
{ << endl << endl;
cout << "請輸入時間 (hh:mm:ss):"; }
cin >> user_input; } while (true);
if (user_input == "exit") return 0;
break; // 跳出迴圈並關閉程式 }
Exercise 2 程式功能
1. 由使用者輸入西元日期,轉換為民國日期
(中華民國 xx 年 xx 月 xx 日)
2. 依序判斷輸入之西元日期之年、月、日是否合理
(需考量閏年),若不合理顯示錯誤的項目
依序檢查「年、月、日」是否輸入正確
判斷是否為閏年
留意中華民國出生年分
驗證今天日期
輸入 exit 離開程式
Exercise 2 程式功能規劃
1. Date.h & Date.cpp(功能介面)
1) set 方法參數為西元日期(年、月、日),並儲存至
private 屬性中,若參數超出範圍則丟回 exception 訊息,
告知有誤的參數項目(Year、Month 或 Day)。尚需判斷
該年民國是否成立、閏年 2 月份天數。
2) 回傳中華民國日期格式字串:中華民國 xx 年 xx 月 xx 日
2. main.cpp (功能測試)
(1) 建立 Date 物件 (2) 使用者輸入、輸出訊息 (3) 字串分割
Exercise 2 程式碼:Date.h
#ifndef DATE_H // 若 year 為閏年、month 為 2 月、day > 29:
#define DATE_H // throw exception: Day 輸入錯誤
#include <iostream> // 若 month 不為 2 月、day > daysInMonth:
#include <string> // throw exception: Day 輸入錯誤
#include <stdexcept> void SetDate(int year, unsigned short
// 紀錄每個月的總天數,二月需另外判斷是否為閏年 month, unsigned short day);
// 若為閏年最多 29 天 // 依據目前西元年分換算並回傳標準格式: