-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvel.h
151 lines (143 loc) · 3.63 KB
/
vel.h
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// vel.hpp
// RainProblem
//
// Created by BlackK on 2016/11/2.
// Copyright © 2016年 BlackK. All rights reserved.
//
#ifndef vel_hpp
#define vel_hpp
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void ff(T a);
template < typename T,int n>
class vel{
private:
vector<T> _data;
int _num;
public:
vel():_data(),_num(0){};
vel & operator<<(const T &data){
if(_num<n){
_data.push_back(data);
_num++;
}
return *this;
}
vel & operator,(const T &data){
return (*this)<<data;
}
vel& operator()(const T & op1, const T& op2)
{
return (*this)<<op1<<op2;
}
//乘法运算1
vel operator*(const vel& other) const;
vel operator*(const T&) const;
friend vel operator*(const T& x,const vel & v){
return v*x;
}
//加法运算符
vel operator+(const vel& other) const;
vel operator+(const T&) const;
friend vel operator+(const T& x,const vel & v){
return v+x;
}
//减法运算符
vel operator-(const vel& other) const;
vel operator-(const T&) const;
//除法运算符
vel operator/(const vel& other) const;
vel operator/(const T&) const;
template <typename P,int q>
friend vel<P,q> operator/ (const P& x,const vel<P,q> & v);
//括号运算符
T& operator[](const int i){
return _data[i];
}
T operator[](const int i) const {
return _data[i];
}
friend ostream & operator<<(ostream & os,const vel & v){
if(v._num){
os<<"{";
for (int i = 0; i<v._num-1; i++) {
os<<v._data[i]<<",";
}
return os<<v._data[v._num-1]<<"}";
}else{
return os<<"{}";
}
}
virtual ~vel(){};
};
template < typename T,int n>
vel<T,n> vel<T,n>::operator*(const vel& other) const {
vel<T,n> newVel;
if(_num==other._num)
for(int i = 0;i<_num;i++)
newVel << (*this)[i] * (other[i]);
return newVel;
}
template <typename T,int n>
vel<T,n> vel<T,n>::operator*(const T& x) const {
vel<T,n> newVel;
for(int i = 0;i<_num;i++)
newVel << (*this)[i] * x;
return newVel;
}
template < typename T,int n>
vel<T,n> vel<T,n>::operator+(const vel& other) const {
vel<T,n> newVel;
if(_num==other._num)
for(int i = 0;i<_num;i++)
newVel << (*this)[i] + (other[i]);
return newVel;
}
template <typename T,int n>
vel<T,n> vel<T,n>::operator+(const T& x) const {
vel<T,n> newVel;
for(int i = 0;i<_num;i++)
newVel << (*this)[i] + x;
return newVel;
}
template < typename T,int n>
vel<T,n> vel<T,n>::operator-(const vel& other) const {
vel<T,n> newVel;
if(_num==other._num)
for(int i = 0;i<_num;i++)
newVel << (*this)[i] - (other[i]);
return newVel;
}
template <typename T,int n>
vel<T,n> vel<T,n>::operator-(const T& x) const {
vel<T,n> newVel;
for(int i = 0;i<_num;i++)
newVel << (*this)[i] - x;
return newVel;
}
template < typename T,int n>
vel<T,n> vel<T,n>::operator/(const vel& other) const {
vel<T,n> newVel;
if(_num==other._num)
for(int i = 0;i<_num;i++)
newVel << (*this)[i] / (other[i]);
return newVel;
}
template <typename T,int n>
vel<T,n> vel<T,n>::operator/(const T& x) const {
vel<T,n> newVel;
for(int i = 0;i<_num;i++)
newVel << (*this)[i] / x;
return newVel;
}
template <typename T,int n>
vel<T,n> operator/ (const T& x,const vel<T,n> &v) {
vel<T,n> newVel;
for(int i = 0;i<v._num;i++)
newVel << x / v[i];
return newVel;
}
#endif /* vel_hpp */