面向对象程序设计实践(C++)——二维向量

概述

设计一个类,实现对二维向量的存储及实现。其类中存储了向量的坐标,以及一些常见的操作。

实现

  • Vec2D.h
#pragma once
#include <bits/stdc++.h>

using namespace std;

class Vec2D
{
	double x_;
	double y_;
public:
	//无参构造函数
	Vec2D();
	//用向量坐标初始化对象
	Vec2D(double, double);
	//析构函数
	~Vec2D();
	//将向量转换为坐标形式的字符串
	std::string toString();
	//求两个向量的加法
	Vec2D add(const Vec2D& vec);
	//求向量与一个数的加法
	Vec2D add(double num);
	//求两个向量的减法
	Vec2D sub(const Vec2D& vec);
	//求向量与一个数的减法
	Vec2D sub(double num);
	//求两个向量的数量积
	double dot(const Vec2D& vec);
	//求向量数乘
	Vec2D mul(double num);
	//求向量的相反向量
	Vec2D neg();
	//向量自增1
	Vec2D& inc();
	//向量自减1
	Vec2D& dec();
	//求向量的模
	double len();
	//返回向量的横坐标
	double getX();
	//返回向量的纵坐标
	double getY();
	//比较两个向量的大小
	int comp(Vec2D& vec);
	//对向量的模重载<运算符
	bool operator<(Vec2D& vec);
	//求相反向量,重载-运算符
	Vec2D operator-();
	//自增/自减重载
	Vec2D& operator++();
	Vec2D& operator--();
	//输入输出流重载
	friend ostream& operator<<(ostream& os, Vec2D vec);
	friend istream& operator>>(istream& is, Vec2D& vec);
	operator double();
};
  • Vec2D.cpp
#include "Vec2D.h"

Vec2D::Vec2D()
{
	x_ = 0.0;
	y_ = 0.0;
}

Vec2D::Vec2D(double x, double y)
{
	x_ = x;
	y_ = y;
}

Vec2D::~Vec2D()
{}

std::string Vec2D::toString()
{
	return std::string('(' + std::to_string(x_) + ',' + std::to_string(y_) + ')');
}

Vec2D Vec2D::add(const Vec2D& vec)
{
	return Vec2D(x_ + vec.x_, y_ + vec.y_);
}

Vec2D Vec2D::add(double num)
{
	return Vec2D(x_ + num, y_ + num);
}

Vec2D Vec2D::sub(const Vec2D& vec)
{
	return Vec2D(x_ - vec.x_, y_ - vec.y_);
}

Vec2D Vec2D::sub(double num)
{
	return Vec2D(x_ - num, y_ - num);
}

double Vec2D::dot(const Vec2D& vec)
{
	return x_ * vec.x_ + y_ * vec.y_;
}

Vec2D Vec2D::mul(double num)
{
	return Vec2D(x_ * num, y_ * num);
}

Vec2D Vec2D::neg()
{
	return Vec2D(-x_, -y_);
}

Vec2D& Vec2D::inc()
{
	x_++;
	y_++;
	return *this;
}

Vec2D& Vec2D::dec()
{
	x_--;
	y_--;
	return *this;
}

double Vec2D::len()
{
	return sqrt(x_ * x_ + y_ * y_);
}

double Vec2D::getX()
{
	return x_;
}

double Vec2D::getY()
{
	return y_;
}

int Vec2D::comp(Vec2D& vec)
{
	double l1 = this->len();
	double l2 = vec.len();
	if (abs(l1 - l2) < 1e-10)
		return 0;
	else
		return l1 > l2 ? 1 : -1;
	return 0;
}

bool Vec2D::operator<(Vec2D& vec)
{
	return this->comp(vec) < 0;
}

Vec2D Vec2D::operator-()
{
	return Vec2D(-x_, -y_);
}

Vec2D& Vec2D::operator++()
{
	x_ += 1;
	y_ += 1;
	return *this;
}

Vec2D& Vec2D::operator--()
{
	x_ -= 1;
	y_ -= 1;
	return *this;
}

Vec2D::operator double()
{
	return len();
}

ostream& operator<<(ostream& os, Vec2D vec)
{
	os << vec.toString();
	return os;
}

istream& operator>>(istream& is, Vec2D& vec)
{
	is >> vec.x_ >> vec.y_;
	return is;
}

测试

  • Main.cpp
#include <bits/stdc++.h>
#include "Vec2D.h"

using namespace std;

int main()
{
	Vec2D v1{ 3,4 }, v2{ 6,8 }, v3{};
	cout << "v1=" << v1.toString() << endl;
	cout << "v2=" << v2 << endl;
	cout << "v1.len=" << v1.len() << endl;
	cout << "v2.len=" << v2.len() << endl;
	cout << "v1+v2=" << v1.add(v2).toString() << endl;
	cout << "v1-v2=" << v1.sub(v2).toString() << endl;
	cout << "v1·v2=" << v1.dot(v2) << endl;
	cout << "v1+1=" << v1.add(1).toString() << endl;
	cout << "v2-2=" << v2.sub(2).toString() << endl;
	cout << "v1 vs v2=" << v1.comp(v2) << endl;
	cout << "v1.neg=" << v1.neg().toString() << endl;
	cout << "-v1=" << (-v1).toString() << endl;
	cout << "v1*3=" << v1.mul(3).toString() << endl;
	cout << "v1.inc=" << v1.inc().toString() << endl;
	cout << "++v1=" << (++v1).toString() << endl;
	cout << "v2.dec=" << v2.dec().toString() << endl;
	cout << "--v2=" << (--v2).toString() << endl;
	if (v1 < v2)
		cout << "v1<v2" << endl;
	else
		cout << "v1>=v2" << endl;
	cout << "Input (x,y): ";
	cin >> v3;
	cout << v3 << endl;
	double lv1=v1;
	cout << "double v1=" << lv1;
	return 0;
}
  • 运行情况
v1=(3.000000,4.000000)
v2=(6.000000,8.000000)
v1.len=5
v2.len=10
v1+v2=(9.000000,12.000000)
v1-v2=(-3.000000,-4.000000)
v1·v2=50
v1+1=(4.000000,5.000000)
v2-2=(4.000000,6.000000)
v1 vs v2=-1
v1.neg=(-3.000000,-4.000000)
-v1=(-3.000000,-4.000000)
v1*3=(9.000000,12.000000)
v1.inc=(4.000000,5.000000)
++v1=(5.000000,6.000000)
v2.dec=(5.000000,7.000000)
--v2=(4.000000,6.000000)
v1>v2
Input (x,y): 1 2
(1.000000,2.000000)
double v1=7.81025
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值