菜鸟的博客

纵有疾风起,人生不言弃。

导航

2023.4.24

 

 1 //实验五任务二
 2 #include <iostream>
 3 using namespace std;
 4 class vector3D
 5 {
 6 private:
 7     float x,y,z;
 8 public:
 9     vector3D()
10     {
11         x = 0;
12         y = 0;
13         z = 0;
14     }
15     friend ostream& operator <<(ostream &cout,vector3D &temp);
16     friend void setValue(vector3D &v);
17     vector3D operator+(vector3D &v)
18     {
19         vector3D temp;
20         temp.x = this->x + v.x;
21         temp.y = this->y + v.y;
22         temp.z = this->z + v.z;
23         return temp;
24     }
25     vector3D operator-(vector3D &v)
26     {
27         vector3D temp;
28         temp.x = this->x - v.x;
29         temp.y = this->y - v.y;
30         temp.z = this->z - v.z;
31         return temp;
32     }
33     vector3D& operator* (int a)
34     {
35         this->x =  a*this->x;
36         this->y =  a*this->y;
37         this->z =  a*this->z;
38         return *this;
39     }
40 };
41 ostream& operator <<(ostream &cout,vector3D &v)
42     {
43         cout<<"("<<v.x<<","<<v.y<<","<<v.z<<")";
44         return cout;
45     }
46 void setValue(vector3D &v)
47 {
48     cin>>v.x;cin>>v.y;cin>>v.z;
49 }
50 int main()
51 {
52     vector3D v1;
53     vector3D v2;
54     int num;
55     cin>>num;
56     if(num == 1)
57     {setValue(v1);}
58     else if(num == 2)
59     {
60         setValue(v1);
61         setValue(v2);
62     }
63     int a;
64     cin>>a;
65     vector3D v3;
66     v3 = v1 + v2;
67     cout<<v3<<endl;
68     v3 = v1 - v2;
69     cout<<v3<<endl;
70     v3 = v1*a;
71     cout<<v3<<endl;
72     return 0;
73 }

 

posted on 2023-04-24 22:41  hhmzd233  阅读(8)  评论(0编辑  收藏  举报