设计一个MyVector(矢量)类,在类中定义整型成员变量x,y ,z代表矢量在三维笛卡尔坐标系上的坐标,与一个浮点型成员变量myLength代表矢量本身的模长(x,y, z为其三维坐标);成员函数至少包含构造函数、计算模长的函数calculateLength(),以及输出模长的函数outputLength()。现给以三维坐标(x1, y1, z1) 和 (x2, y2, z2)的形式给出两个三维矢量
,试使用矢量类,计算出的
模长和
的模长。
#include<iostream>
#include<math.h>
using namespace std;
class MyVector
{
private:
int x;
int y;
int z;
double myLength;
public:
MyVector(int x=0, int y=0, int z=0) : x{x}, y{y}, z{z} {}
void calculateLength(){
myLength = pow(pow(x,2.0)+pow(y,2.0)+pow(z,2.0),0.5);
}
double outputLength(){
printf("%f\n",myLength);
}
friend istream &operator>>(istream &in, MyVector &c);
};
//重载>>
istream &operator>>(istream &in, MyVector &c) {
in >> c.x >> c.y >> c.z;
return in;
}
int main() {
MyVector v1, v2;
cin >> v1;
cin >> v2;
v1.calculateLength();
v2.calculateLength();
v1.outputLength();
v2.outputLength();
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
class MyVector
{
private:
int x;
int y;
int z;
double myLength;
public:
MyVector(int x=0, int y=0, int z=0) : x{x}, y{y}, z{z} {}
void calculateLength(){
myLength = pow(pow(x,2.0)+pow(y,2.0)+pow(z,2.0),0.5);
}
double outputLength(){
printf("%f\n",myLength);
}
friend istream &operator>>(istream &in, MyVector &c);
MyVector operator-(const MyVector &c) const{
return {this->x-c.x, this->y-c.y, this->z-c.z};
}
MyVector operator+(const MyVector &c) const{
return {this->x+c.x, this->y+c.y, this->z+c.z};
}
};
//重载>>
istream &operator>>(istream &in, MyVector &c) {
in >> c.x >> c.y >> c.z;
return in;
}
int main() {
MyVector v1, v2;
cin >> v1;
cin >> v2;
MyVector v3 = v1-v2;
v3.calculateLength();
v3.outputLength();
v3 = v1+v2;
v3.calculateLength();
v3.outputLength();
return 0;
}
分别是矢量相减,相加的模长
请问这个多组输出怎么改啊
C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html