实验五:
实验2:
#include<iostream>
#include<string>
using namespace std;
class vector3D
{
private:
float x,y,z;
public:
vector3D(float a=0,float b=0,float c=0):x(a),y(b),z(c){}
friend istream& operator>>(istream &,vector3D &);
friend ostream& operator<<(ostream &,vector3D &);
vector3D operator+(vector3D &a);
vector3D operator-(vector3D &a);
friend vector3D operator*(float s,vector3D &a);
};
istream& operator>>(istream &put,vector3D &v)
{
put>>v.x>>v.y>>v.z;
return put;
}
ostream& operator<<(ostream &out,vector3D &v)
{
out<<"("<<v.x<<","<<v.y<<","<<v.z<<")"<<endl;
return out;
}
vector3D vector3D::operator+(vector3D &a)
{
return vector3D(x+a.x,y+a.y,z+a.z);
}
vector3D vector3D::operator-(vector3D &a)
{
return vector3D(x-a.x,y-a.y,z-a.z);
}
vector3D operator*(float s,vector3D &a)
{
return vector3D(s*a.x,s*a.y,s*a.z);
}
int main()
{
vector3D v1,v2;
int n;
float s;
cin>>n;
if(n==1)
{
cin>>v1>>s;
}
else if(n==2)
{
cin>>v1>>v2>>s;
}
cout<<v1+v2;
cout<<v1-v2;
cout<<s*v1;
return 0;
}
标签:12,return,cout,vector3D,代码,float,istream,operator,小时 From: https://www.cnblogs.com/lixinyao20223933/p/17311292.html