1.设计一个日期类Date,用于表示日期值(年、月、日)。具有成员数据:年、月、日;成员函数有:
有参和无参的构造函数进行日期数据初始化;
设置日期函数;
获取日期值函数(可分为3个,分别获取年、月、日的值);
判断是否闰年的函数;
校验日期合法性的函数;
(提示:需要确定月份(1-12),日子(根据月份及闰年/平年)(1-28,29,30,31))。
编写主函数对该类进行测试。
#include<iostream>
using namespace std;
class Date{
private:
int y,m,d;
public:
Date()
:y(0),m(0),d(0){}
Date(int a,int b,int c)
:y(a),m(b),d(c){}
void set_date(int a,int b,int c){
y=a;
m=b;
d=c;
}
int get_y()const{return y;}
int get_m()const{return m;}
int get_d()const{return d;}
int Pdyear()const{
if(y%400==0)
return 1;//返回1是闰年
else{
if(y%4==0&&y%100!=0)
return 1;
else
return 0;返回0不是闰年
}
}
bool hfx(int a=1)const{
switch(a){
case 1:cout<<"闰年"<<endl;break;
case 0:cout<<"不是闰年"<<endl;break;
}//判断闰年
if(m>12||m<1){
cout<<"不合法"<<endl;
return false;
}
else{
if(m==2){
switch(a){
case 1:
if(d>29||d<1){cout<<"不合法"<<endl;return false;}
else {
cout<<"合法"<<endl;return true;}
case 0:
if(d>28||d<1){cout<<"不合法"<<endl;return false;}
else {
cout<<"合法"<<endl;return true;}
}
}
else if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
if(d>31||d<1){
cout<<"不合法"<<endl;return false;}
else{
cout<<"合法"<<endl;return true;}
}
else{
if(d>30||d<1){
cout<<"不合法"<<endl;return false;}
else{
cout<<"合法"<<endl;return true;}
}
}
}
};
int main(){
int a=1;
int yy=0,mm=0,dd=0;
Date d1(1949,10,1),d2;
cout<<d1.get_y()<<"年"<<d1.get_m()<<"月"<<d1.get_d()<<"日"<<endl;
d1.hfx(d1.Pdyear());
cout<<d2.get_y()<<"年"<<d2.get_m()<<"月"<<d2.get_d()<<"日"<<endl;
while(a){
cout<<"请输入年月日,用空格隔开"<<endl;
cin>>yy>>mm>>dd;
d2.set_date(yy,mm,dd);
d2.hfx(d2.Pdyear());
cout<<"---------------"<<endl;
cout<<"是否继续测试--输入1继续--输入0退出"<<endl;
fflush(stdin);
cin>>a;
cout<<"---------------"<<endl;
fflush(stdin);
}
system("pause");
return 0;
}
2.定义一个矩形类CRect,包含私有数据成员为对角线两点的坐标:x1、y1、x2、y2,(均为整型)。除提供必要的构造函数,该类还提供多个成员函数,可分别实现:求周长、求面积、设置坐标值、读取值、显示输出矩形基础信息(两点坐标值)。编写主函数,输入矩形对角线两点的坐标值,计算输出矩形的周长和面积。
#include<iostream>
using namespace std;
class CRect{
private:
double x1,y1,x2,y2;
public:
CRect()
:x1(0),y1(0),x2(0),y2(0){}
CRect(double a,double b,double c,double d)
:x1(a),y1(b),x2(c),y2(d){}
double get_x1()const{return x1;}
double get_y1()const{return y1;}
double get_x2()const{return x2;}
double get_y2()const{return y2;}
void set(double a,double b,double c,double d){
x1=a;
y1=b;
x2=c;
y2=d;
}
void CCR(){
double a=0,b=0;
a=x1-x2;
b=y1-y2;
if(a<0)
a=-1*a;
if(b<0)
b=-1*b;
cout<<(a+b)*2<<endl;;
}
void SCR(){
double a=0,b=0;
a=x1-x2;
b=y1-y2;
if(a<0)
a=-1*a;
if(b<0)
b=-1*b;
cout<<(a*b)<<endl;;
}
void output(double a,double b,double c,double d)const{
cout<<"对角线两点坐标:";
cout<<'('<<a<<','<<b<<')'<<'('<<c<<','<<d<<')'<<endl;
}
};
int main(){
double a=0,b=0,c=0,d=0;
CRect c1;
c1.output(c1.get_x1(),c1.get_y1(),c1.get_x2(),c1.get_y2());
cout<<"请输入两个二维坐标"<<endl;
cin>>a>>b>>c>>d;
c1.set(a,b,c,d);
cout<<"基本信息:"<<endl;
c1.output(c1.get_x1(),c1.get_y1(),c1.get_x2(),c1.get_y2());
cout<<"周长:";c1.CCR();
cout<<"面积:";c1.SCR();
system("pause");
return 0;
}
3.利用string类实现字符串的输入、赋值、比较、字符查找等功能。
#include<iostream>
#include<string>
using namespace std;
int main(){
string a,b;
int position;
cout<<"请输入a的字符串"<<endl;
cin>>a;
cout<<"a的值"<<a<<endl;
b=a;
cout<<"b的值"<<b<<endl;
cout<<"请输入b的字符串"<<endl;
cin>>b;
cout<<"比较a与b的大小"<<endl;
int m=b.compare (a);
if(m<0)
{cout<<"b大于a"<<endl;}
else if(m>0)
{cout<<"a大于b"<<endl;}
else
{cout<<"a等于b"<<endl;}
cout<<"输入要在a中找查的字符串"<<endl;
string n;
cin>>n;
position=a.find(n);
if(position != a.npos)
cout << "找到了位置是 : " << position << endl;
else
{cout << "没有找到 "<< endl;}
system("pause");
return 0;
}