Repository navigation
Expand file tree
/
Copy pathOO_Comparison.cpp
More file actions
96 lines (73 loc) · 2.54 KB
/
Copy pathOO_Comparison.cpp
File metadata and controls
96 lines (73 loc) · 2.54 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
//
// Created by Andrea Halenkamp on 10/1/16.
//
#include "OO_Comparison.h"
bool IntWrapper::isLessThan(IComparable* rhs) const{
IntWrapper* comparable = dynamic_cast<IntWrapper *>(rhs);
return (storedValue < comparable->getValue());
}
int IntWrapper::getValue() {
return storedValue;
}
IntWrapper::IntWrapper(int initialValue) {
storedValue = initialValue;
}
bool StringWrapper::isLessThan(IComparable* rhs) const{
StringWrapper* comparable = dynamic_cast<StringWrapper *>(rhs);
return (storedValue < comparable->getValue());
}
string StringWrapper::getValue() {
return storedValue;
}
StringWrapper::StringWrapper(string initialValue) {
storedValue = initialValue;
}
vector<IComparable*> sortingFunctions::sorting(vector<IComparable*> input) {
for (int x = 0; x < vectorSize; x++) {
if(dynamic_cast<IntWrapper *>(input[x])) {
IntWrapper* beginning = dynamic_cast<IntWrapper *>(input[x]);
IntWrapper* minValue = beginning;
int old = x;
for (int y = x; y < vectorSize; y++) {
if(minValue->isLessThan(dynamic_cast<IntWrapper *>(input[y]))) {
minValue = dynamic_cast<IntWrapper *>(input[y]);
old = y;
}
}
input[x] = minValue;
input[old] = beginning;
}
if(dynamic_cast<StringWrapper *>(input[x])) {
StringWrapper* beginning = dynamic_cast<StringWrapper *>(input[x]);
StringWrapper* minValue = beginning;
int old = x;
for (int y = x; y < vectorSize; y++) {
if(minValue->isLessThan(dynamic_cast<StringWrapper *>(input[y]))) {
minValue = dynamic_cast<StringWrapper *>(input[y]);
old = y;
}
}
input[x] = minValue;
input[old] = beginning;
}
}
return input;
}
bool sortingFunctions::sortingVerifications(vector<IComparable *> input) {
bool isRight = 1; // Innocent until proven guilty!!!
for (int i = 0; i < vectorSize-1; i++) {
if(input[i]->isLessThan(input[i+1]))
isRight = false;
}
return isRight;
}
void sortingFunctions::print(vector<IComparable *> input) {
for(IComparable* i : input) {
IntWrapper* intw = dynamic_cast<IntWrapper *>(i);
if(intw)
cout << intw->getValue() << endl;
StringWrapper* stringw = dynamic_cast<StringWrapper *>(i);
if(stringw)
cout << stringw->getValue() << endl;
}
}