#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include<cstdlib>
using namespace std;
class Date
{
public:
Date();//构造函数
Date(const Date&);//拷贝构造函数
Date strToDate(string);//将string转化为Date
void setDate(string);//设置日期
void printDate();//打印日期
private:
int year;
int month;
int day;
bool pass;
bool checkFormat(string);//验证日期输入格式
void validate();//验证日期数值有效性
bool leapYear(int);//闰年判断
};
class A
{
private:
int age;
public:
int get_age();
void set_age();
};
int A::get_age()
{
age = 28;
return age;
}
void A::set_age()
{
age = 100;
}
Date::Date()
{
year = 2006;
month = day = 1;
validate();
}
Date::Date(const Date& date)
{
year = date.year;
month = date.month;
day = date.day;
pass = date.pass;
}
Date Date::strToDate(string ds)
{
// yyyy:mm:dd
Date d;
d.setDate(ds);
return d;
}
void Date::setDate(string ds)
{
if( !checkFormat(ds) )
{
cout << "日期格式错误" << endl;
pass = false;
return;
}
validate();
if(pass)
{
year = atoi(ds.substr(0, 4).c_str());
month = atoi(ds.substr(5, 2).c_str());
day = atoi(ds.substr(8, 2).c_str());
}
}
void Date::printDate()
{
if (pass)
{
cout << year << "年"
<< month << "月"
<< day << "日" << endl;
}
else
{
cout << "日期错误" << endl;
}
}
void Date::validate()
{
pass = false;
if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31)
{
return;
}
if (month == 2)
{
if (leapYear(year))
{
if (day <= 29)
{
pass = true;
}
return;
}
else
{
if (day <= 28)
{
pass = true;
}
return;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (month <= 30)
{
pass = true;
}
return;
}
else
pass = true;
}
bool Date::leapYear(int year)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
return true;
}
}
else
{
if ((year % 4) == 0)
{
return true;
}
}
return false;
}
bool Date::checkFormat(string ds)
{
//yyyy:mm:dd
if (ds.length() != 10
|| ds[4] != ':'
|| ds[7] != ':')
{
return false;
}
for (int i = 0; i < (int)ds.length(); i++ )
{
if( i != 4 && i != 7)
{
if (ds[i] < '0' || ds[i] > '9')
{
return false;
}
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
// 测试是否能正确设置日期
string str;
cout << "--TEST:请输入一个日期<yyyy:mm:dd>:" << endl;
cin >> str;
Date d;
d.setDate(str);
d.printDate();
//测试拷贝构造函数
cout << "--TEST:调用拷贝构造函数" << endl;
Date dc = d;
dc.printDate();
//测试字符串到日期转换的函数
cout << "--TEST:将字符串’2012:03:28‘转化为Date类型并输入" << endl;
Date dt = d.strToDate("2012:03:28");
dt.printDate();
A a;
a.set_age();
cout<<"您目前的年龄是:";
cout<<a.get_age();
system("pause");
return 0;
}
分享到:
相关推荐
这是管理C++类的定义域使用的概略,里面比较详细的介绍了类的想换内容,适合初学者
在C++中,声明一个类,并且定义一个全局变量,练习全局变量的使用。注意全局变量的使用技巧和方法。
zfind是本人写的一个简单的C/C++宏定义和类定义搜索工具,z是左的意思,即本人姓氏。取此名字是因为windows下有find命令,为避免冲突。另外本工具是本人在32位WIN7环境下使用的,其余环境本人不保证兼容。 写此工具...
VC++/C++复数类定义
c++类的声明和定义是在学习c++中开始会遇到的问题,希望有所帮助!
简单的c++常识,类的定义,构造函数和析构函数
定义一个Book(图书)类,在该类定义中包括: (1) 数据成员:bookname(书名)、price(价格) 和number(存书数量)。 (2) 成员函数:display()显示图书的情况;borrow()将存书数量减1。并显示当前存书数量;restore( )...
1.自定义分数类fraction,使用该类可以完成分数的输入、分数的加、减、乘、除二目运算和一目减运算、分数的约分操作、分数的倒数运算、对两个分数进行六种比较运算、以及对分数的... 定义类,运算符重载、类型转换。
c++ 基础 类外定义函数 调用函数 基础
如果要在 QML使用 C++中定义的类型,有些数据类型是是可以直接使用的,如常见的 int,但是还有一些相对特殊的类型就需要做些特定的操作才可以使用了。 博客地址:...
日期时间类定义,扩充实例编程中的日期类,为Date类增加一个成员函数,可以判 断一个日期是否是系统的当前日期。从键盘输入你的生日,如果今天是你的生日则显示“Happy Birthday!”,否则显示“还有XX天是你的生日...
C++复数定义和重载运算符,包括加减乘除,取实部,虚部等
C++_关于声明,定义,类的定义,头文件作用
定义一个串类,类里面包括了构造函数,析构函数,串的产生,串的添加和删除
C++定义一个类中的方法调用 C++如何调用类中定义的方法,调用实例
c++\实验2 C++函数的定义与使用. c++\实验2 C++函数的定义与使用.
c++实验二 简单的类的定义与使用.docx
使用c++进行操作文件,定义一个学生的类,此为源代码信息。
C++关于一个复数类的定义,实现复数的加减乘除,能够实现正的和负数的复数四则运算