-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path问题1.cpp
44 lines (33 loc) · 859 Bytes
/
问题1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cmath>
using namespace std;
class Vector {
private:
double x, y;
public:
Vector(double x_val, double y_val) : x(x_val), y(y_val) {}
Vector add(const Vector& other) const {
return Vector(x + other.x, y + other.y);
}
void print() const {
cout << "Vector的x:" << x << " "<< "Vector的y: " << y << endl;
}
void dir() const {
double magnitude = sqrt(x * x + y * y);
cout << magnitude << endl;
}
};
int main() {
Vector vector1(3.0, 4.0);
Vector vector2(1.0, 2.0);
cout << "Vector 1: ";
vector1.print();
cout << "Vector 2: ";
vector2.print();
Vector vector3 = vector1.add(vector2);
cout << "Vector 1 和 Vector 2的和: ";
vector3.print();
cout << "向量和的模长 ";
vector3.dir();
return 0;
}