功能要求
假定需要遵从不同用户的习惯来显示日期,如“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