📄 string.cpp
字号:
// String.cpp: implementation of the String class.
//
//////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
#include "String.h"
class Date //Date类
{
public:
unsigned int year;
unsigned int month;
unsigned int date;
public:
Date operator=(const Date&);
Date(unsigned int, unsigned int, unsigned int);
Date();
void ShowDate();
};
Date::Date()
{
}
bool IsYearValid(unsigned int y, unsigned int m, unsigned int d)
{
if (m>12 || m<1)
return false; //对月份进行处理
bool flag=false;//判断闰年
short dayBig;
if (y%400==0 || (y%4==0 && y%100!=0))
flag=true;
if (m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
dayBig=1;
else if (m==2)
dayBig=0;
else
dayBig=-1;
if (dayBig==1)
{
if (d>31 || d<1)
return false;
}
else if (dayBig==-1)
{
if (d>30 || d<1)
return false;
}
else //对2月的判断
{
if (flag) //如果是闰年
{
if (d>29 || d<1)
return false;
}
else
{
if (d>28 || d<1)
return false;
}
}
return true;
}
bool IsIdValid(Date b, String id) //判断身份证号是否合法
{
if (id.Length()!=18) //18位身份证号
return false;
String sYear=id.SubString(6,4);
String sMonth=id.SubString(10,2);
String sDate=id.SubString(12,2); //从身份证中得到生日日期
if (b.year!=atoi(sYear.MakeChar()) ||
b.month!=atoi(sMonth.MakeChar()) ||
b.date!=atoi(sDate.MakeChar())) //如果身份证号中的日期与出生日期不匹配
return false;
return true;
}
Date::Date(unsigned int y, unsigned int m, unsigned int d)
{
if (!IsYearValid(y,m,d))
{
return;
}
year=y;
month=m;
date=d;
}
Date Date::operator=(const Date& da)
{
year=da.year;
date=da.date;
month=da.month;
return *this;
}
void Date::ShowDate()
{
if (!IsYearValid(year, month, date)) //如果日期不合法
{
cout<<"日期错误!"<<endl;
return;
}
//cout<<"日期:";
cout<<year<<"年"<<month<<"月"<<date<<"日"<<endl;
}
class People //People类
{
private:
int number; //编号
String gender; //性别
Date birthday; //生日
String id; //身份证号
public:
People();
People(int,String,Date,String); //number,gender,Date, id
People(People&);
~People();
public:
void SetNumber(int);
void SetId(String);
void SetBirthday(Date);
void SetGender(String);
void GetNumber();
void GetId();
void GetBirthday();
void GetGender();
void ShowMessage();
};
inline People::People()
{
}
inline People::People(int n, String g, Date b, String i) : number(n), gender(g), birthday(b), id(i)
{
}
inline People::People(People& p)
{
number=p.number;
gender=p.gender;
birthday=p.birthday;
id=p.id;
}
inline People::~People()
{
}
inline void People::SetNumber(int n)
{
number=n;
}
inline void People::SetBirthday(Date b)
{
birthday=b;
}
inline void People::SetGender(String g)
{
gender=g;
}
inline void People::SetId(String i)
{
id=i;
}
inline void People::ShowMessage()
{
cout<<"ID为"<<number<<"成员信息如下:"<<endl;
cout<<" 性别:"<<gender<<endl;
cout<<" 出生日期:";
birthday.ShowDate();
cout<<" 身份证号:";
if (IsYearValid(birthday.year, birthday.month, birthday.date) && !IsIdValid(birthday, id))
cout<<"身份证号与出生日期不匹配,或身份证号长度不为18!"<<endl;
else
cout<<id<<endl;
}
void Menu() //显示菜单
{
cout<<"---------菜单---------"<<endl;
cout<<"<A> 录入信息"<<endl
<<"<B> 显示信息"<<endl
<<"<Q> 退出系统"<<endl;
cout<<"请输入您的选择:";
}
void Input(People &p)
{
int n; //编号
Date b; //出生日期
String g; //性别
String i; //身份证号
cout<<"编号 性别 出生日期(年月日) 身份证号"<<endl;
cin>>n>>g>>b.year>>b.month>>b.date>>i;
p.SetNumber(n);
p.SetBirthday(b);
p.SetGender(g);
p.SetId(i);
}
int main(int argc, char* argv[])
{
People p;
while (true)
{
Menu();
char sel; //用于选择
cin>>sel;
sel&=0x0df; //小写转大写,大写不变
switch (sel)
{
case 'A':
Input(p);
break;
case 'B':
p.ShowMessage();
break;
case 'Q':
system("pause");
exit(0);
break;
default:
cout<<"输入错误,请重新输入!"<<endl;
}
system("pause");
system("cls");
}
return EXIT_SUCCESS;
}
/*
int main(int argc, char * argv[])
{
String str;
str="你好啊!";
cout<<str.Length()<<endl;
//cout<<str.SubString(2,2);
str+="hi,你好";
cout<<str;
return 0;
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -