m0_53238525 2021-06-25 18:39 采纳率: 40%
浏览 95
已采纳

设计并实现一个平面坐标系内的位置类position

设计并实现一个平面坐标系内的位置类position。包含的基本数据成员有:横坐标,纵坐标;包含的基本成员函数有:设置位置;读取位置;判断第几象限;计算到源点的距离;计算到其他点的距离;计算经过源点到这个位置的直线的斜率;计算经过这个位置到其他点的直线的斜率;按坐标轴平移位置。其他成员函数功能可以自行补充。
实验要求:
  ❶ 按照描述完成position类的基本的设计和实现。将数据成员设计为私有(private)成员;将成员函数设计为公有(public)成员。并通过以下测试程序。
#include “position.hpp”
#include <iostream>
using namespace std;

int main() {
    position a, b, c,d,e;
    a.set(5, 15);
    a.show();
    b.set(-4.5, 6.7);
    b.show();
    c.set(-10, -100);
    c.show();
    d.set(20.5, 5.5);
    e.set();//默认为原点
    e.show();
    cout<<distance(a, b)<<endl;
    cout<<distance(c)<<endl;//默认求与原点的距离
    cout<<a.slope()<<endl;//与原点构成直线的斜率
    cout<<a.slope(d)<<endl;    //与d构成直线的斜率
    a.move(3);//沿x轴平移
    a.show();
    b.move(-4, 5);
    b.show();
    c.move(0, 6);//沿y轴平移
    c.show();
    return 0;
}

实验提交:
  将完整的源代码和测试截图 粘贴在下面。 
源代码粘贴处:


程序测试截图:

 

  • 写回答

1条回答 默认 最新

  • 暖晴的天 2021-06-25 19:17
    关注

     其中 position.hpp 文件设计如下:

    #include "math.h"
    #include <iostream>
    using namespace std;
    class position
    {
    public:
    	position();
    	~position();
    
    	void set(float x = 0 , float y = 0);
    	float getX() { return this->x; }
    	float getY() { return this->y; }
    	void show();
    	//static float distance(position A, position B = position());
    	float slope(position A = position());
    	void move(float x = 0, float y = 0);
    private:
    	float x;
    	float y;
    };
    
    position::position()
    {
    	this->x = 0.0;
    	this->y = 0.0;
    }
    
    position::~position()
    {
    
    }
    
    void position::set(float x, float y)
    {
    	this->x = x;
    	this->y = y;
    }
    
    void position::show()
    {
    	cout << "横坐标:" << this->x << endl;
    	cout << "纵坐标:" << this->y << endl;
    
    	if (x == 0 && y == 0)
    	{
    		cout << "位于原点" << endl;
    	}
    	if (this->x > 0 && this->y > 0)
    	{
    		cout << "位于第一象限" << endl;
    	}
    	if (this->x < 0 && this->y > 0)
    	{
    		cout << "位于第二象限" << endl;
    	}
    	if (this->x < 0 && this->y < 0)
    	{
    		cout << "位于第三象限" << endl;
    	}
    	if (this->x > 0 && this->y < 0)
    	{
    		cout << "位于第四象限" << endl;
    	}
    }
    
    float position::slope(position A)
    {
    	return (this->y - A.y) / (this->x - A.x);
    }
    
    void position::move(float x, float y)
    {
    	this->x -= x;
    	this->y -= y;
    }
    
    float distance(position A, position B = position());
    
    float distance(position A, position B)
    {
    	return sqrt(pow(2, (A.getX() - B.getX())) + pow(2, (A.getY() - B.getY())));
    }

    运行结果如下: 

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题