数据结构-类 21/7/22

这篇博客介绍了如何使用C++实现日期类`Date`,包括构造函数、赋值函数和日期运算,如加一天。同时,讲解了点类`Point`的构造、析构、拷贝构造以及距离计算方法,讨论了数组中点的最大距离计算。涉及到类的生命周期管理和成员函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目1

Date(类与对象)

Description
下面是一个日期类的定义,请在类外实现其所有的方法,并在主函数中生成对象测试之。

class Date {
int year, month, day;
public:
Date();
Date(int y, int m, int d); // 缺省构造函数,给year、month、day分别赋值为1900、1、1
int getYear(); // 带参构造函数,给year、month、day分别赋参数的值
int getMonth(); // 返回当前日期的年份
int getDay(); // 返回当前日期的月份
void setDate(int y, int m, int d); // 返回当前日期的日
void print(); // 按参数重设日期的值
void addOneDay(); // 在当前日期上加一天
}
注意,在判断明天日期时,要加入跨月、跨年、闰年的判断

例如9.月30日的明天是10月1日,12月31日的明天是第二年的1月1日

2月28日的明天要区分是否闰年,闰年则是2月29日,非闰年则是3月1日

Input

测试数据的组数 t

第一组测试数据的年 月 日

要求第一个日期的年月日初始化采用构造函数,第二个日期的年月日初始化采用setDate方法,第三个日期又采用构造函数,第四个日期又采用setDate方法,以此类推。

Output
输出今天的日期

输出明天的日期

Sample Input

4
2012 1 3
2012 2 28
2012 3 31
2012 4 30

Sample Output

Today is 2012/01/03
Tomorrow is 2012/01/04
Today is 2012/02/28
Tomorrow is 2012/02/29
Today is 2012/03/31
Tomorrow is 2012/04/01
Today is 2012/04/30
Tomorrow is 2012/05/01

题解1


了解原理-类的构造与赋值

  • 在一个类中会有一个构造函数,构造函数的作用是初始化类的数据。
  • 构造函数分为无参构造函数,与一般即有参构造函数。
  • 构造函数名与类名一致,并且没有返回类型。
#include<iostream>

Class Date
{
	private:
		int x,y;
	public:
		Date()  //无参构造函数
		{
			x=1;
			y=1;
		}  //设置x,y为 1
		Date(int y_value, int x_value) //有参构造函数
		{
			x=x_value;
			y=y_value;
		} //将x_value 和 y_value 的值赋予 x,y
}
  • 类的赋值一般是void返回类型,赋值的前提是类生成了。
Class Date
{
	public:
		void setXY(int x_value,int y_value) 
		{
			x=x_value;
			y=y_value;
		}
}

思路


题目要求两种不同的数据初始化方式,一种是Date用有参构造函数即可

if(times%i==1)
{
	Date a(y,m,d);
	print();
}

第二种是setDate的形式,我们则先构造一个函数,再赋值

if(times%i==0)
{
	Date a;
	setDate a(y,m,d);
	print();
}

代码


类函数太长,就放主函数了

int main()
{
    int t,times=1;
    int y,m,d;
    cin>>t;
    while(t--)
    {
        cin>>y>>m>>d;
        if(times%2 ==  1)
        {
            Date a(y,m,d);
            a.print();
        }
        else
        {
            Date a;
            a.setDate(y,m,d);
            a.print();
        }
    }
    return 0;
}

题目2

Point_Array

Description

class Point
{
double x, y;
public:
Point();
Point(double x_value, double y_value); // 缺省构造函数,给 x, y 分别赋值为0
~Point(); // 析构函数
double getX(); // 返回x的值
double getY(); // 返回y的值
void setXY(double x1, double y1){x = x1; y = y1;}
void setX(double x_value); // 设置x的值
void setY(double y_value); // 设置y的值
double getDisTo(Point &p); // 计算当前点到参数点p的距离
}
上面是我们曾经练习过的一个习题,请在原来代码的基础上作以下修改:

增加自写的拷贝构造函数;
增加自写的析构函数;
将getDisTo方法的参数修改为getDisTo(const Point &p);
根据下面输出的内容修改相应的构造函数。
然后在主函数中根据用户输入的数目建立Point数组,求出数组内距离最大的两个点之间的距离值。

Input
测试数据的组数 t

第一组点的个数

第一个点的 x 坐标 y坐标

第二个点的 x坐标 y坐标

Output
输出第一组距离最大的两个点以及其距离

在C++中,输出指定精度的参考代码如下:

#include
#include //必须包含这个头文件
using namespace std;
void main( )
{
double a =3.141596;
cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位

C中如下:

printf("%.3f\n", a);

Sample Input

2
4
0 0
5 0
5 5
2 10
3
-1 -8
0 9
5 0

Sample Output

Constructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 10.44,between p[1] and p[3].
Distructor.
Distructor.
Distructor.
Distructor.
Constructor.
Constructor.
Constructor.
The longeset distance is 17.03,between p[0] and p[1].
Distructor.
Distructor.
Distructor.

题解2

了解原理-类的构造与析构


  • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
Class Point
{
	public:
		Point()
		{
			x=1,y=1;
		}
		Point(int x_value,int y_value)
		{
			x=x_value,y=y_value;
		}
		~Point()
		{
		cout<<Distructor.<<endl;
}
int main()
{
	Point a; //定义一个Point类 对象a,然后调用Point()给 a.x,a.y赋值为 1
	Point a(x,y); //生成Point类 对象a, 然后调用Point(int x_value,int y_value)赋值a.x=x,a.y=y
}
  • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。

第一种在程序结束的时候会自动析构

int main() 
{
	Point a;
	system("pause"); //结束后输出 Distructor.
	return 0;
}

第二种在类生成的循环结束时

int main()
{
	int t; 
	cin>>t;
	while(t--)
	{
		Point a;
		print();
	} //每一个循环结束时候输出 Distructor.
	return 0;
}

思路


由于题目要在构造和析构时输出Constructor 和 Distructor

Point::Point()
{
	cout<<"Constructor"<<endl; //调用构造函数时会输出
}
Point::~Point()
{
	cout<<"Distructor"<<endl; //析构时输出
}

在输入数据时,我们先生成一个对象数组p[n],再用for循环读入x,y用setXY函数赋值

Point p[n];
for(int i=0;i<n;i++)
{
	cin>>x>>y;
	p[i].setXY(x,y);
}

由于要找到最大的值,就用2个for循环让i与i+1到n依次比较

for(int i=0;i<n;i++)
	for(int j=i+1;j<n;j++)
	{
		double dis=p[i].getDisTo(p[j]);  //getDisTo函数用于获取p[i]与p[j]之间距离
		if(max_dis > dis)
		{
			dis=max_dis;
			index1=i; 
			index2=j; //分别记录两个点所在数组位置
		}
	}

代码


#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
int times=0;

class Point
{
    double x, y;
public:
    Point();
    Point(double x_value, double y_value);      // 缺省构造函数,给 x, y 分别赋值为0
    ~Point();                                   // 析构函数
    double getX();                              // 返回x的值
    double getY();                              // 返回y的值
    void setXY(double x1, double y1){x = x1; y = y1;}
    void setX(double x_value);                  // 设置x的值
    void setY(double y_value);                  // 设置y的值
    double getDisTo(const Point &p);                  // 计算当前点到参数点p的距离
};

Point::Point()
{
    cout<<"Constructor."<<endl;
    x=0;
    y=0;
}
Point::Point(double x_value,double y_value)
{
    x=x_value;
    y=y_value;
}
Point::~Point()
{
        cout<<"Distructor."<<endl;
}
double Point::getX()
{
    return x;
}
double Point::getY()
{
    return y;
}
void Point::setX(double x_value)
{
    x=x_value;
}
void Point::setY(double y_value)
{
    y=y_value;
}
double Point::getDisTo(const Point &p)
{
    return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}

int main()
{
    int n,index1,index2;
    double t,max_dis=0;
    double x,y;
    cin>>t;
    while(t--)
    {
        cin>>n;
        times=n;
        Point p[n];
        for(int i=0;i<n;i++)
        {
            cin>>x>>y;
            p[i].setXY(x,y);
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                double dis=p[i].getDisTo(p[j]);
                if(dis > max_dis)
                {
                    max_dis=dis;
                    index1=i;
                    index2=j;
                }
            }
        }
        cout<<"The longeset distance is "<<fixed<<setprecision(2)<<max_dis<<",between p["<<index1<<"] and p["<<index2<<"]."<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值