IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 数据结构-类 21/7/22 -> 正文阅读

[数据结构与算法]数据结构-类 21/7/22

题目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;
}

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 11:03:36  更:2021-07-23 11:06:34 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 8:51:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码