指导书五
实验任务二]:*三维坐标向量类*
【问题描述】
设计一个描述三维坐标的向量类vector3D,成员如下:
数据成员:
² 三个坐标x,y,z,float类型,私有访问属性
公有函数成员:
Ø 三个参数均有默认值的构造函数,默认值为0,0,0;
Ø 重载输入输出运算符,输出格式为(x, y, z);
Ø 重载加法+、减法-、数乘*(乘数在前,乘数为float类型)这三个运算符;
在主函数中定义两个vector3D类对象v1,v2,均不带参数,之后输入数字1或2选择为v1输入赋值,还是为v1和v2输入赋值,对v1和v2进行加、减运算,对v1进行数乘运算,乘数由用户输入,最后输出三种运算结果。
【输入形式】
1
v1的坐标值x,y,z
数乘值
或者
2
v1、v2的坐标值x,y,z
数乘值
【输出形式】
v1+v2相加结果
v1-v2相减结果
v1数乘结果
【样例1输入】
1
1 2 3
5
【样例1输出】
(1, 2, 3)
(1, 2, 3)
(5, 10, 15)
【样例2输入】
2
1 2 3
4 5 6
2
【样例2输出】
(5, 7, 9)
(-3, -3, -3)
(2, 4, 6)
【样例说明】
输出形式中每个坐标后为英文逗号”,”,并跟着一个空格;括号为英文括号“()”
#include<iostream>
using namespace std;
class vector3d
{
friend ostream& operator<<(ostream& out, vector3d& v);
private:
float x, y, z;
public:
vector3d(float m_x = 0, float m_y = 0, float m_z = 0)
{
x = m_x;
y = m_y;
z = m_z;
}
vector3d& operator+(vector3d& v)
{
vector3d temp;
temp.x = this->x + v.x;
temp.y = this->y + v.y;
temp.z = this->z + v.z;
return temp;
}
vector3d& operator-(vector3d& v)
{
vector3d temp;
temp.x = this->x - v.x;
temp.y = this->y - v.y;
temp.z = this->z - v.z;
return temp;
}
vector3d& operator*(float);
};
vector3d& vector3d::operator*(float m)
{
vector3d temp;
temp.x = m * this->x;
temp.y = m * this->y;
temp.z = m * this->z;
return temp;
}
ostream& operator<<(ostream& out, vector3d& v)
{
out << "(" << v.x << "," << v.y << "," << v.z << ")" << endl;
return out;
}
int main()
{
int n;
cin >> n;
if (n == 1)
{
float x1, y1, z1, m;
cin >> x1 >> y1 >> z1 >> m;
vector3d v1(x1, y1, z1);
cout << v1 << endl;
cout << v1 << endl;
vector3d temp = v1 * m;
cout << temp << endl;
}
else
{
float x1, y1, z1, x2, y2, z2, m;
cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2 >> m;
vector3d v1(x1, y1, z1), v2(x2, y2, z2);
/*cout << (v1 + v2) << endl;
cout << (v1 - v2) << endl;
cout << (v1 * m) << endl;*/
vector3d a, b, c;
a = v1 + v2;
b = v1 - v2;
c = v1 * m;
cout << a;
cout << b;
cout << c;
}
}
[实验任务三]: N维整型向量类
【问题描述】
线性代数里面我们学习过n维向量,请用类vector_N来封装n维整型向量,成员如下;
私有数据成员:
² 向量维数n, int型
² 指针 p,int型
公有函数成员:
Ø 无参默认构造函数,在该函数中,将n置0,将p置null;
Ø 重载输入输出运算符,输入运算符,先指定向量维数,若输入为非正整数,则提示错误信息,“Error Length!”然后退出程序,若维数输入正确则分配长度为n的动态内存,并用指针p指向该内存,输出运算符依次输出向量各元素即可;
Ø 重载向量的加法+、减法-、数乘*(乘数在前,乘数为int型)这三运算符;
Ø 重载[]运算,例如,向量a=(1,2,3,4),a[0]的值为1,若下标越界,则输出“Error Index”,然后退出程序;
Ø 返回向量维数的函数;
Ø 将两个向量的内积运算定义为vector_N的友元函数;
在主函数中定义两个vector_N类对象v1,v2,均不带参数,之后对两个对象进行输入赋值,输入数乘运算的乘数,输入待取元素的下标,对两个向量进行加、减、数乘和内积运算,并将结果输出,输出v1中对应下标对应的元素。加法、减法和内积运算先判断两向量维数是否一致,若一致则输出运算结果,否则输出错误提示信息“Mismatch Length!”
提示:1.此类需要用到动态内存的分配,所以在析构函数中应释放空间,并将指针置null,将维数n置0
2.需要显式定义复制构造函数vector_N(vector_N &)
3.需要重载复制运算符 vector_N operator= (vector_N &)
4.退出程序用函数 _exit(0)
5.返回值类型需要为引用的形式,另一方面,在使用时就要考虑不能返回临时变量的引用
【输入形式】
v1的维数
v1各元素的值
v2的维数
v2各元素的值
数乘运算乘数
待取元素下标值
【输出形式】
若维数输入均正确
v1+v2的结果
v1-v2的结果
v1、v2的内积结果
v1数乘的结果
v1向量中对应下标的元素
若维数输入错误
Error Length!
【输入样例1】
3
1 2 3
3
4 5 6
2
2
【输出样例1】
(5, 7, 9)
(-3, -3, -3)
32
(2, 4, 6)
3
【输入样例2】
-6
【输出样例2】
Error Length!
【输入样例3】
3
1 2 3
4
7 8 9 6
3
4
【输出样例3】
Mismatch Length!
(3, 6, 9)
Error Index
【样例说明】
输出形式中每个元素后为英文逗号”,”,并跟着一个空格;括号为英文括号“()”
#include<iostream>
using namespace std;
class vectorN
{
friend istream& operator>>(istream& in, vectorN& v);
friend ostream& operator<<(ostream& out, vectorN& v);
friend vectorN& ji(vectorN& v1, vectorN& v2);
private:
int n;
int* p;
public:
vectorN() { n = 0; p = NULL; }
vectorN(vectorN& v)
{
this->n = v.n;
p = new int[v.n];
for(int i=0;i<v.n;i++)
{
p = new int(v.p[i]);
}
}
vectorN& operator=(vectorN &v)
{
this->n = v.n;
p = new int[v.n];
for (int i = 0; i < v.n; i++)
{
p = new int(v.p[i]);
}
return (*this);
}
vectorN& operator+(int m)
{
for (int i = 0; i < n; i++)
{
this->p[i] += m;
}
}
vectorN& operator-(int m)
{
for (int i = 0; i < n; i++)
{
this->p[i] -= m;
}
}
vectorN& operator*(int m)
{
for (int i = 0; i < n; i++)
{
this->p[i] *= m;
}
}
int operator[](int i)
{
if (i < 0 || i >= n) { cout << "Error Index" << endl; _exit(0); }
else
{
return p[i];
}
}
int getN()
{
return n;
}
~vectorN()
{
if (p != NULL)
{
delete[]p;
p = NULL;
n = 0;
}
}
};
istream& operator>>(istream& in, vectorN& v)
{
in>> v.n;
if (v.n < 0) { cout << "Error Length!" << endl; _exit(0); }
else
{
v.p = new int[v.n];
for (int i = 0; i < v.n; i++)
{
in >> v.p[i];
}
return in;
}
}
ostream& operator<<(ostream& out, vectorN& v)
{
cout << "(";
for (int i = 0; i < v.n; i++)
{
if(i<v.n-1)
{
out << v.p[i] << " ,";
}
else
{
out << v.p[i]<<" ";
}
}
cout <<")" << endl;
return out;
}
vectorN& ji(vectorN& v, vectorN& v1)
{
for (int i = 0; i < v.n; i++)
{
v.p[i] *= v1.p[i];
}
return v;
}
int main()
{
vectorN v1;
cin >> v1;
vectorN v2(v1);
cout << v1;
cout << v2;
return 0;
}