-
Notifications
You must be signed in to change notification settings - Fork 0
/
points2.h
249 lines (213 loc) · 6.11 KB
/
points2.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******
TITLE: Points2.h
NAME : INGMAR FJOLLA
We created a class that contains a template object used to store a sequence of 2D points as described
by Professor Raja
********/
#ifndef CSCI335_HOMEWORK1_POINTS2_H_
#define CSCI335_HOMEWORK1_POINTS2_H_
#include <array>
#include <iostream>
#include <cstddef>
#include <string>
#include <sstream>
using namespace std;
namespace teaching_project {
/*The class Points2, as described by professor Raja
was that the purpose of this assignment is to have you create a Points2 class
from scratch with limited help from the STL. Points 2 desribes a sequence of
of 2D points such as : Example 1: (1, 3), (4, 5) and
Example 2: (1.2, 3.4), (5.6, 10.1), (11.1, 12.0). First example being filled with int types
and the second having double coordinates.
*/
template<typename Object>
class Points2 {
public:
// Default "big five" -- you have to alter them for your assignment.
// That means that you will remove the "= default" statement.
// and you will provide an implementation.
// Zero-parameter constructor.
// Set size to 0.
//also set sequence to a nullptr
Points2(): size_(0),sequence_(nullptr)
{}
// Copy-constructor.
Points2(const Points2 &rhs) //: size_{rhs.size_}
{
//if rhs.size is zero (size is empty) then set privae memebrs to zero
if(rhs.size()==0)
{
size_ = 0;
sequence_ = nullptr;
}
else{
size_ = rhs.size_;
//dynamically allocate memory to create space for the sequence
sequence_ = new array<Object, 2>[size_];
copy(rhs.sequence_, rhs.sequence_+rhs.size_, sequence_);
}
}
// Copy-assignment. If you have already written
// the copy-constructor and the move-constructor
// you can just use:
// {
// Points2 copy = rhs;
// std::swap(*this, copy);
// return *this;
// }
//given code
Points2& operator=(const Points2 &rhs)
{
Points2 copy = rhs;
swap(*this, copy);
return *this;
}
// Move-constructor.
//transfers everything from rhs to new object
//then deletes whatever is in rhs
Points2(Points2 &&rhs)
{
sequence_=move(rhs.sequence_);
size_= move(rhs.size_);
rhs.sequence_ = nullptr;
rhs.size_ = 0;
}
// Move-assignment.
// Just use std::swap() for all variables.
Points2& operator=(Points2 &&rhs)
{
if(this == &rhs)
{
return *this;
}
swap(size_, rhs.size_);
swap(sequence_, rhs.sequence_);
return *this;
}
//destructor responsible for reallocating space
~Points2()
{
delete[] sequence_;
sequence_=nullptr;
//size_ = 0;
}
// End of big-five.
// One parameter constructor.
Points2(const std::array<Object, 2>& item): sequence_(new array<Object, 2>{item}), size_(item.size())
{}
// Read a chain from standard input.
void ReadPoints2() {
// Part of code included (without error checking).
std::string input_line;
std::getline(std::cin, input_line);
std::stringstream input_stream(input_line);
if (input_line.empty()) return;
// Read size of sequence (an integer).
int size_of_sequence;
input_stream >> size_of_sequence;
// Allocate space for sequence.
size_ = size_of_sequence;
sequence_ = new array<Object, 2>[size_];
Object token;
for (int i = 0 ;input_stream >> token; ++i) {
// Read coordinates.
// Fill sequence_ here.
sequence_[i][0] = token;
input_stream >> token;
sequence_[i][1] = token;
}
}
size_t size() const {
return size_;
}
// @location: an index to a location in the sequence.
// @returns the point at @location.
// const version.
// abort() if out-of-range.
const std::array<Object, 2>& operator[](size_t location) const {
//bounds checking
if(location <= -1 || location > size_-1)
{
cout << "Location out-of-range" << endl;
abort();
}
else
{
return sequence_[location];
}
}
// @c1: A sequence.
// @c2: A second sequence.
// @return their sum. If the sequences are not of the same size, append the
// result with the remaining part of the larger sequence.
friend Points2 operator+(const Points2 &c1, const Points2 &c2)
{
//object to return
Points2<Object> resulting_sequence;
//compare sizes prior to beginning the loop, less if else statements
if(c1.size_>c2.size_)
{
resulting_sequence.size_ = c1.size_;
}
else
{
resulting_sequence.size_ = c2.size_;
}
resulting_sequence.sequence_ = new array<Object, 2>[resulting_sequence.size_];
for ( auto i =0 ; i < resulting_sequence.size_ ; i++)
{
if(c1.size_>c2.size_ && i >= c1.size_)
{
resulting_sequence.sequence_[i][0] = c1.sequence_[i][0];
resulting_sequence.sequence_[i][1] = c1.sequence_[i][1];
}
else if(c2.size_>c1.size_ && i >= c2.size_)
{
resulting_sequence.sequence_[i][0] = c2.sequence_[i][0];
resulting_sequence.sequence_[i][1] = c2.sequence_[i][1];
}
else{
resulting_sequence.sequence_[i][0]=c1.sequence_[i][0] + c2.sequence_[i][0];
resulting_sequence.sequence_[i][1] = c1.sequence_[i][1] + c2.sequence_[i][1];
}
}
return resulting_sequence;
}
// Overloading the << operator.
friend std::ostream &operator<<(std::ostream &out, const Points2 &some_points2)
{
//makes sure its not empty
if(some_points2.size_ >=1)
{
for (size_t i = 0; i < some_points2.size_; i++)
{
out << "(" << some_points2.sequence_[i][0] << ", " << some_points2.sequence_[i][1] << ")" << " ";
}
}
else
{
out << "()";
}
// if(some_points2.sequence_==nullptr || some_points2.size_<1)
// {
// out<<"()"; //the case of the empty
// }
// else{
// for(size_t i = 0;i< some_points2.size_;i++)
// {
// out<<"("<< some_points2.sequence_[i][0]<<", "<<some_points2.sequence_[i][1]<<")";
// }
// out << endl;
// return out;
// }
out << endl;
return out;
}
private:
// Sequence of points.
std::array<Object, 2> *sequence_;
// Size of sequence.
size_t size_;
};
} // namespace teaching_project
#endif // CSCI_335_HOMEWORK1_POINTS2_H_