Original link: https://anran758.github.io/blog/2022/05/21/note-04737/
This article is mainly about self-examination practice exercises.
Title description: There are vectors X=(x1, x2,…, xn) and Y=(y1, y2,…, yn), where x1, x2,…, xn, y1, y2,…, yn Both are integer types, and n is a natural number. Addition, subtraction and multiplication between vectors X and Y are:
- X + Y = (x1+y1, x2+y2, …, xn+yn)
- X – Y = (x1-y1, x2-y2, …, xn-yn)
- X Y = (x1 y1 + x2 y2, …. + xn yn)
Use the C++ language to implement the following functions:
- Write a program to define the vector class Vector
- Overload operators “+”, “-“, “*” and “=” to implement addition, subtraction, multiplication and assignment operations between Vector objects
- The overloaded operators “>>”, “<<” implement the input and output functions of the Vector object
- The legitimacy of trial operation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
# include <iostream> # include <algorithm>
class vector { private : int _size; // number of elements int _capacity; // capacity double *_list; //point to the resource allocated by new enum { SPACE_CAPACITY = 4 }; // initial capacity
// memory allocation (the allocator simplifies processing) //Every time the space is not enough, regain 2 times the space of the current capacity void resize ( int newSize) { if (newSize > _capacity) { ReAllocate (newSize + 1 ); } _size = newSize; }
// reallocate space void ReAllocate ( int newCapacity) { // If the new capacity is smaller than the current size, no operation will be performed if (newCapacity < _size) return ;
// get new space double *oldArray = _list; _list = new double [newCapacity];
// copy data for ( int k = 0 ; k < _size; k++) { _list[k] = oldArray[k]; } _capacity = newCapacity;
// release old data delete [] oldArray; }
public : // use explicit to avoid implicit type conversions explicit vector ( int size = 0 ) : _size(size), _capacity(size + SPACE_CAPACITY) { _list = new double [_capacity]; }
// copy constructor vector ( const vector &rhs ) : _list( NULL ) { operator =(rhs); }
~ vector () { delete []_list; }
const vector & operator =( const vector &rhs) { // If it is itself, there is no need to operate if ( this == &rhs ) return * this ;
delete []_list; _size = rhs.size (); _capacity = rhs.capacity ();
_list = new double [ capacity ()];
// copy element for ( int k = 0 ; k < _size; k++) { _list[k] = rhs._list[k]; } }
// output friend std::ostream & operator <<(std::ostream &os, const vector &v) { for ( int k = 0 ; k < v. size (); k++) { os << v[k] << " " ; }
return os; }
// input friend std::istream & operator >>(std::istream &is, vector &v) { // TODO: there is still room for improvement double num; is >> num; v.push_back (num);
// std::string line; // std::getline(std::cin, line); // std::cout << "line: " << line << "/n";
return is; }
vector operator +( const vector &v) { const int size = std:: max ( this -> size (), v. size ()); vector Z; int val = 0 ; for ( int i = 0 ; i < size; i++) { val = ( this -> _size >= i ? _list[i] : 0 ) + (v. size () >= i ? v[i] : 0 ); Z.push_back (val); }
return Z; }
vector operator -( const vector &v) { const int size = std:: max ( this -> size (), v. size ()); vector Z; int val = 0 ; for ( int i = 0 ; i < size; i++) { val = ( this -> _size >= i ? _list[i] : 0 ) - (v. size () >= i ? v[i] : 0 ); Z.push_back (val); }
return Z; }
vector operator *( const vector &v) { const int size = std:: max ( this -> size (), v. size ()); vector Z; int val = 0 ; for ( int i = 0 ; i < size; i++) { val = ( this -> _size >= i ? _list[i] : 0 ) * (v. size () >= i ? v[i] : 0 ); Z.push_back (val); }
return Z; }
vector operator /( const vector &v) { const int size = std:: max ( this -> size (), v. size ()); vector Z; double val = 0 ; for ( int i = 0 ; i < size; i++) { val = ( this -> _size >= i ? _list[i] : 0 ) / (v. size () >= i ? v[i] : 0 ); Z.push_back (val); }
return Z; }
// add a data at the end of the array void push_back ( const double &x) { if (_size == _capacity) { resize (_size + 1 ); }
_list[_size++] = x; }
void pop_back () { _size -= 1 ; }
// property interface int size () const { return _size; }
int capacity () const { return _capacity; }
bool empty () const { return size () == 0 ; }
const int & back () const { return _list[_size - 1 ]; }
// Access properties via the [] operator double & operator []( int index) { return _list[index]; }
const double & operator []( int index) const { return _list[index]; }
};
int main ( int argc, char *argv[]) { const int columns = 4 ;
vector X; std::cout << "Please enter the values of the vector X (space separated):" << std::endl; for ( int i = 0 ;i < columns; i++) { std::cin >> X; } std::cout << "X: " << X << '\n' ; std::cout << '\n' ;
std::cout << "Please enter the values of the vector Y (space separated):" << std::endl; vector Y; for ( int i = 0 ;i < columns; i++) { std::cin >> Y; } std::cout << "Y: " << Y << '\n' ; std::cout << '\n' ;
vector Z = X + Y; std::cout << "X + Y: " << Z << '\n' ;
vector A = X - Y; std::cout << "X - Y: " << A << '\n' ;
vector B = X * Y; std::cout << "X * Y: " << B << '\n' ;
vector C = X / Y; std::cout << "X / Y: " << C << '\n' ;
return 0 ; }
|
This article is reprinted from: https://anran758.github.io/blog/2022/05/21/note-04737/
This site is for inclusion only, and the copyright belongs to the original author.