2. (20 scores)#
点击查看考点
类的基础语法
A vector \(\overrightarrow{v}\) on a two-dimensional plane can be expressed as \((a, b)\). The basic operation of a vector is defined as follows:
Vector modulus: \(||\overrightarrow{v}|| = \sqrt{a^2 + b^2}\).
Vector inner product: \(\overrightarrow{v_1} * \overrightarrow{v_2} = a_1 * a_2 + b_1 * b_2\), where \(\overrightarrow{v_1} = (a_1, b_1)\), \(\overrightarrow{v_2} = (a_2, b_2)\).
Cosine distance of vector: \(cos\theta = \frac{\overrightarrow{v_1} * \overrightarrow{v_2}}{||\overrightarrow{v_1}|| * ||\overrightarrow{v_2}||}\), where \(\overrightarrow{v_1} = (a_1, b_1)\), \(\overrightarrow{v_2} = (a_2, b_2)\). If cosine distance is 0, the two vectors are perpendicular; if cosine distance is 1, the two vectors are similar.
Write the program to encapsulate the vector class CVector
, and verify the correctness of the CVector
.
翻译
一个二维向量 \(\overrightarrow{v}\) 可以被表示为 \((a, b)\). 其基本运算定义如下:
求模: \(||\overrightarrow{v}|| = \sqrt{a^2 + b^2}\).
求内积: \(\overrightarrow{v_1} * \overrightarrow{v_2} = a_1 * a_2 + b_1 * b_2\), 其中 \(\overrightarrow{v_1} = (a_1, b_1)\), \(\overrightarrow{v_2} = (a_2, b_2)\).
余弦距离: \(cos\theta = \frac{\overrightarrow{v_1} * \overrightarrow{v_2}}{||\overrightarrow{v_1}|| * ||\overrightarrow{v_2}||}\), 其中 \(\overrightarrow{v_1} = (a_1, b_1)\), \(\overrightarrow{v_2} = (a_2, b_2)\). 如果余弦距离为 0, 则两向量相垂直; 如果余弦距离为 1, 则两向量相似.
编写程序封装该向量类为 CVector
, 并验证 CVector
的正确性.