功能要求
假定需要遵從不同用戶的習慣來顯示日期,如“2018-04-22”或“22/4/2018”等,按約定和要求完成程序。
功能要求及說明
(1) 設計一個日期類(TDate)並派生出TNormalDate、TFormatDate兩個格式化日期類,分別以格式“2018-04-22”、“22/4/2018”顯示用戶給定的一個日期(年、月、日),除按約定格式顯示日期的功能外,有關日期的其它處理功能都不作要求;
(2) 設計主程序(main函數),提示用戶輸入一個日期,並在提示用戶輸入日期顯示格式(1表示Normal格式,2表示Format格式)後,將該日期按要求顯示在屏幕上;
(3) 可以反覆對同一個日期設置不同顯示格式;
(4) 可以輸入多個不同的日期;
(5) 支持將所有操作結果(用戶輸入的日期,顯示格式,顯示結果)輸出到一個文本文件(date.log),多次運行程序時,該文本文件的已有內容不能被覆蓋。
源碼
#include <iostream>
#include <Windows.h>
#include <string>
#include<fstream>
#include <conio.h>
using namespace std;
/*父類:日期*/
class TDate {
protected:
int year; //年
int month; //月
int day; //日
public:
TDate(int year, int month, int day) {
this->year = year;
this->month = month;
this->day = day;
}
int getYear() {
return year;
}
int getMonth() {
return month;
}
int getDay() {
return day;
}
};
/*子類:輸出 "yyyy-MM-dd" 格式的類*/
class TNormalDate :public TDate {
public:
//構造函數,顯式調用父類構造函數
TNormalDate(int year, int month, int day) :TDate(year, month, day) {
this->year = year;
this->month = month;
this->day = day;
}
void showDate() {
printf_s("%d-%02d-%02d\n", getYear(), getMonth(), getDay());
}
};
/*子類:輸出 "dd/MM/yyyy" 格式的類*/
class TFormatDate :public TDate {
public:
//構造函數,顯式調用父類構造函數
TFormatDate(int year, int month, int day) :TDate(year, month, day) {
this->year = year;
this->month = month;
this->day = day;
}
void showDate() {
printf_s("%02d/%02d/%d\n", getDay(), getMonth(), getYear());
}
};
/*日誌類,用於記錄用戶操作記錄*/
class Log {
private:
fstream fout;//文件輸出流
string date;//用戶輸入的日期
string format;//日期格式
string result;//打印結果
public:
//構造函數,初始化數據
Log(string d, string f, string r) {
fout.open("C:/Users/HP/Desktop/date.log", ios::app | ios::binary);//打開桌面上的date.log文件,沒用則會自動創建
date = d; format = f; result = r;
fout.seekg(0, ios_base::end);
output();
}
//析構函數,關閉文件流
~Log() {
fout.close();
}
private:
//將日誌信息輸出到文件
void output() {
if ((int)fout.tellg() == 0) {//如果文件內容爲空,則先輸出一個標題,再輸出操作記錄
fout << "輸入日期" << "\t顯示格式" << "\t輸出結果\r\n";
}
fout << date << "\t" << format << "\t" << result << "\r\n";
}
};
class Menu {
public:
Menu() {
init();//初始化菜單界面
}
void init() {
cout << "分別輸入一個日期的年,月,日,然後通過快捷鍵顯示日期的不同格式" << endl;
cout << "1.輸出Normal格式:yyyy-MM-dd" << endl;
cout << "2.輸出Format格式:dd/MM/yyyy" << endl;
cout << "3.重新輸入日期" << endl;
cout << "4.退出程序\n" << endl;
cout << "請輸入一個日期:" << endl;
input();
}
void input() {
int year, month, day, ch;
string stry = "年", strm = "月", strd = "日";
cout << stry << ":"; cin >> year;
cout << strm << ":"; cin >> month;
cout << strd << ":"; cin >> day;
cout << "輸入你的操作:(不需要回車)" << endl;
while (1) {
if (_kbhit()) {//如果有按鍵按下,則_kbhit()函數返回真
ch = _getch();//使用_getch()函數獲取按下的鍵值
if (ch == 49) { //49爲鍵盤上“1”的鍵值
TNormalDate tnd(year, month, day); tnd.showDate();
string d = to_string(year) + stry + to_string(month) + strm + to_string(day) + strd;
string f = "yyyy-MM-dd";
string r = to_string(year) + "-" + formatSTR(month) + "-" + formatSTR(day);
Log(d, f, r);
_getch();
}
else if (ch == 50) {//50爲鍵盤上“2”的鍵值
TFormatDate tfd(year, month, day); tfd.showDate();
string d = to_string(year) + stry + to_string(month) + strm + to_string(day) + strd;
string f = "dd/MM/yyyy";
string r = formatSTR(day) + "/" + formatSTR(month) + "/" + to_string(year);
Log(d, f, r);
_getch();
}
else if (ch == 51) {//51爲鍵盤上“3”的鍵值
system("cls");
_getch();
init();
break;
}
else if (ch == 52) {
exit(0);
}
else {
cout << "輸入有誤!" << endl;
_getch();
}
}
}
}
private:
//格式化字符串,在個位數前面加“0”
string formatSTR(int num) {
if (num < 10) return "0" + to_string(num);
else return to_string(num);
}
};
int main() {
Menu menu;
system("pause");
}
效果截圖
控制檯:
文件:date.log