-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab13.cpp
More file actions
203 lines (174 loc) · 5.01 KB
/
lab13.cpp
File metadata and controls
203 lines (174 loc) · 5.01 KB
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
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <stdexcept>
using namespace std;
class doublevector {
static constexpr double DOUBLE_DEFAULT_VALUE = -5.55;
double *mValues;
int mNumValues;
public:
doublevector();
~doublevector();
void resize(int new_size, double initial_value=DOUBLE_DEFAULT_VALUE);
void push_back(double value);
double &at(int index);
const double &at(int index) const;
unsigned long size() const;
};
void print_all(const doublevector &v)
{
if (v.size() == 0) {
cout << "Vector is empty.\n";
}
else {
unsigned long i;
for (i = 0;i < v.size();i++) {
cout << "[" << setfill('0') << right << setw(3) << i
<< "] = " << fixed << setprecision(4) << v.at(i)
<< '\n';
}
}
}
int main()
{
doublevector v;
do {
string command;
cout << "Enter a command ('quit' to quit): ";
if (!(cin >> command) || command == "quit") {
break;
}
else if (command == "push_back") {
double value;
cin >> value;
v.push_back(value);
cout << "Pushed back "
<< fixed << setprecision(4)
<< value << '\n';
}
else if (command == "resize") {
string line;
int new_size;
double initial;
cin >> new_size;
getline(cin, line);
istringstream sin(line);
if (sin >> initial)
v.resize(new_size, initial);
else
v.resize(new_size);
}
else if (command == "size") {
cout << v.size() << '\n';
}
else if (command == "print") {
print_all(v);
}
else if (command == "get") {
int index;
cin >> index;
try {
cout << "Value at " << index << " = "
<< setprecision(4) << fixed
<< v.at(index) << '\n';;
}
catch(out_of_range &err) {
cout << err.what() << '\n';
}
}
else if (command == "set") {
double d;
int index;
cin >> index >> d;
try {
v.at(index) = d;
cout << "SET: " << index << " = "
<< setprecision(4) << fixed
<< v.at(index) << '\n';;
}
catch(out_of_range &err) {
cout << err.what() << '\n';
}
}
else if (command == "clear") {
v.resize(0);
}
else {
cout << "Invalid command '" << command << "'\n";
}
} while(true);
cout << endl;
return 0;
}
//Write your class members below here.
//Name: Autumn Henderson
//Date: 11/21/2018
//Title: Lab 13
//Description: This program creates a vector class.
doublevector::doublevector() {
mNumValues = 0;
}
doublevector::~doublevector() {
mValues = nullptr;
}
void doublevector::resize(int new_size, double initial_value) {
//Allocates mValues memory if it has not been allocated
if (mValues == nullptr) {
mValues = new double [new_size];
mNumValues = new_size;
for (int i = 0; i < mNumValues; ++i)
mValues[i] = initial_value;
if (mValues != nullptr)
delete [] mValues;
}
//Grows the vector
if (new_size > mNumValues && mValues != nullptr) {
double *newPtr = new double [new_size];
for (int i = 0; i < mNumValues; ++i) {
newPtr[i] = mValues[i];
}
for (int i = 0; i < (new_size - mNumValues); ++i) {
newPtr[i + mNumValues] = initial_value;
}
//All values for the new pointer have been assigned
mValues = newPtr;
mNumValues = new_size;
}
//Shrinks the vector
else if (new_size < mNumValues && mValues != nullptr) {
double *newPtr;
newPtr = new double [new_size];
for (int i = 0; i < new_size; ++i)
newPtr[i] = mValues[i];
mValues = newPtr;
mNumValues = new_size;
}
//Deletes mValues
if (mValues != nullptr && new_size == 0) {
delete [] mValues;
}
}
void doublevector::push_back(double value) {
resize(mNumValues + 1, value);
}
double& doublevector::at(int index) {
if (index >= mNumValues) {
ostringstream sout;
sout << "Invalid index #" << index;
throw out_of_range(sout.str());
}
return mValues[index];
}
const double& doublevector::at(int index) const {
if (index >= mNumValues) {
ostringstream sout;
sout << "Invalid index #" << index;
throw out_of_range(sout.str());
}
return mValues[index];
}
unsigned long doublevector::size() const {
return mNumValues;
}