Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Exercise 1 程式功能

1. 由使用者輸入 24 小時制時間,轉換成 12 小時制時


間並顯示(hour 需介於 1~12 之間,在時間後方加
上 AM 或 PM)

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) 回傳標準 12 小時制時間格式字串(hh:mm:ss [AM/PM],


hh 介於 1~12,數值可不用補零)

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. 依序判斷輸入之西元日期之年、月、日是否合理
(需考量閏年),若不合理顯示錯誤的項目

3. 民國元年為 1912 年,若輸入更早的年份顯示


「此年中華民國還沒出生」
Exercise 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 天 // 依據目前西元年分換算並回傳標準格式:

const short daysInMonth[] = { 31, 28, 31, 30, // 「中華民國 xx 年 xx 月 xx 日」


31, 30, 31, 31, 30, 31, 30, 31 }; std::string GetDateInROC();
class Date
{ private:
public: // 若 year 為閏年回傳 true,否則回傳 false
// 依序判斷 year, month, day 是否符合規則 bool isLeapYear(int year);
// 若 year < 0: throw exception: Year 輸入錯誤
// 若 year < 1912: throw exception: int m_year;
// 此年中華民國還沒出生 unsigned short m_month;
// 若 month > 12: throw exception: unsigned short m_day;
// Month 輸入錯誤 };
// 若 year 非閏年、month 為 2 月、day > 28:
// throw exception: Day 輸入錯誤 #endif
Exercise 2 程式碼:Date.cpp
#include "Date.h" std::string Date::GetDateInROC()
using namespace std; {
// 若 year 為閏年回傳 true,否則回傳 false // To Do: 回傳符合格式字串:
bool Date::isLeapYear(int year) // 「中華民國 xx 年 xx 月 xx 日」
{ }
return ((((year) % 4) == 0 && ((year) %
100) != 0) || ((year) % 400) == 0);
}

void Date::SetDate(int year, unsigned short


month, unsigned short day)
{
// TO DO: 依序判斷 year, month, day
// 是否符合條件
// 若不符合 throw 回指定錯誤訊息
// 若符合將 year, month, day 紀錄至
// m_year, m_month, m_day
}
Exercise 2 程式碼:main.cpp
#include "Date.h" else if (SplitUserInput(user_input,
using namespace std; year, month, day) == false)

// 依照使用者輸入的字串 user_input, 切分為 // 若使用者輸入錯誤,要求重新輸入


year, month, day 並儲存到個別參數 continue;
bool SplitUserInput(const string &user_input, try
int &year, unsigned short &month, unsigned {
short &day)
date.SetDate(year, month, day);
{ /*請從 main.cpp 超連結複製完整程式*/ }
cout << date.GetDateInROC()
int main()
<< endl << endl;
{
}
Date date;
catch (invalid_argument e)
string user_input;
{
int year;
cout << "無效參數: " << e.what()
unsigned short month, day;
<< endl << endl;
do
}
{
} while (true);
cout << "請輸入西元日期 (yyyy/mm/ss):";
cin >> user_input;
return 0;
if (user_input == "exit")
}
break; // 跳出迴圈並關閉程式

You might also like