|
| 1 | + |
| 2 | +/* |
| 3 | + Vorlesung "C++ für Java Programmierer" |
| 4 | + Medieninformatik Bachelor |
| 5 | + Aufgabe 3, Sommersemester 2012 |
| 6 | + (C)opyright Hartmut Schirmacher |
| 7 | + http://hschirmacher.beuth-hochschule.de |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "assert.h" |
| 11 | +#include <iostream> |
| 12 | +#include <string> |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +// include your own header files here... |
| 16 | +#include "pair.h" |
| 17 | +//#include "order.h" |
| 18 | +//#include "tree.h" |
| 19 | +//#include "map.h" |
| 20 | + |
| 21 | +// you should define your own namespace for |
| 22 | +// the templates/classes in this project |
| 23 | +using namespace mystl; |
| 24 | + |
| 25 | +// explicit template instantiation for compiling / debugging |
| 26 | +//template class Pair<int,string>; |
| 27 | +//template class Tree< int,Less<int> >; |
| 28 | +//template class Map<int, string>; |
| 29 | + |
| 30 | +// list and count all nodes in a set using an iterator |
| 31 | +template<class Container> |
| 32 | +int printAndCount(Container & c) { |
| 33 | + |
| 34 | + int n=0; |
| 35 | + for(typename Container::iterator i=c.begin(); i!=c.end(); ++i, ++n) |
| 36 | + cout << *i << " "; |
| 37 | + |
| 38 | + cout << "(" << n << " elements)." << endl; |
| 39 | + |
| 40 | + return n; |
| 41 | +} |
| 42 | + |
| 43 | +// list backwards and count all nodes in a set |
| 44 | +template<class Container> |
| 45 | +int printAndCountBackwards(Container & c) { |
| 46 | + int n = 0; |
| 47 | + typename Container::iterator i = c.end(); |
| 48 | + while(i != c.begin()) { |
| 49 | + --i; |
| 50 | + cout << *i << " "; |
| 51 | + n++; |
| 52 | + } |
| 53 | + cout << endl; |
| 54 | + return n; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +int testTemplates(void) |
| 59 | +{ |
| 60 | + |
| 61 | + cout << "Starting..." << endl; |
| 62 | + |
| 63 | + ///////////////////////////////////////// |
| 64 | + // TEST PAIR |
| 65 | + |
| 66 | + Pair<int,float> i_f(2,3.14); |
| 67 | + Pair<int,float> i_f2(4,3.14); |
| 68 | + Pair<string,string> s_s("Hello","World!"); |
| 69 | + cout << i_f << " " << s_s << endl; |
| 70 | + |
| 71 | +#if 0 // move this line down while your implementation proceeds... |
| 72 | + |
| 73 | + ///////////////////////////////////////// |
| 74 | + // TEST ORDER |
| 75 | + Less<int> lessInt; |
| 76 | + cout << "2<3 == " << lessInt(2,3) << endl; |
| 77 | + cout << "4<3 == " << lessInt(4,3) << endl; |
| 78 | + |
| 79 | + ///////////////////////////////////////// |
| 80 | + // TEST PAIR ORDER |
| 81 | + MapToFirst< int, float, Less > lessPair; |
| 82 | + cout << i_f << " < " << i_f2 << " == " << lessPair(i_f, i_f2) << endl; |
| 83 | + |
| 84 | + ///////////////////////////////////////// |
| 85 | + // TEST TREE |
| 86 | + |
| 87 | + // construct empty tree |
| 88 | + Tree<int> t; |
| 89 | + cout << "empty tree: "; |
| 90 | + assert(printAndCount(t) == 0); |
| 91 | + |
| 92 | + // insert elements in a certain order |
| 93 | + t.insert(4); |
| 94 | + t.insert(3); |
| 95 | + t.insert(2); |
| 96 | + t.insert(1); |
| 97 | + t.insert(5); |
| 98 | + cout << "tree 4-3-2-1-5: "; |
| 99 | + assert(printAndCount(t) == 5); |
| 100 | + |
| 101 | + // test clear() |
| 102 | + t.clear(); |
| 103 | + cout << "after clear(): "; |
| 104 | + assert(printAndCount(t) == 0); |
| 105 | + |
| 106 | + // try another insertion order |
| 107 | + t.insert(1); |
| 108 | + t.insert(2); |
| 109 | + t.insert(3); |
| 110 | + t.insert(5); |
| 111 | + t.insert(4); |
| 112 | + cout << "tree 1-2-3-5-4: "; |
| 113 | + assert(printAndCount(t) == 5); |
| 114 | + |
| 115 | + // and yet another insertion sequence |
| 116 | + t.clear(); |
| 117 | + t.insert(45); |
| 118 | + t.insert(4); |
| 119 | + t.insert(89); |
| 120 | + t.insert(9); |
| 121 | + t.insert(7); |
| 122 | + t.insert(3); |
| 123 | + cout << "6-element tree: "; |
| 124 | + assert(printAndCount(t) == 6); |
| 125 | + |
| 126 | + // now we contruct a tree with a "reverse" order |
| 127 | + typedef Tree< float, Greater<float> > RevFloatTree; |
| 128 | + RevFloatTree ft; |
| 129 | + ft.insert(3.1); |
| 130 | + ft.insert(6.2); |
| 131 | + ft.insert(4.3332); |
| 132 | + ft.insert(17.20); |
| 133 | + cout << "reverse-sorted 4-float tree: "; |
| 134 | + assert(printAndCount(ft) == 4); |
| 135 | + |
| 136 | + // if we list elements backwards, they should be |
| 137 | + // in the same order as with the function Less<> |
| 138 | + cout << "listing backwards: "; |
| 139 | + assert(printAndCountBackwards(ft) == 4); |
| 140 | + |
| 141 | + |
| 142 | + ///////////////////////////////////////// |
| 143 | + // TEST MAP |
| 144 | + |
| 145 | + Pair<int,string> p42(42,"Douglas Adams"); |
| 146 | + Pair<int,string> p3(3,"Nummer 3"); |
| 147 | + Pair<int,string> p1(1,"Nummer 1"); |
| 148 | + Pair<int,string> p7(7,"James Bond"); |
| 149 | + string value; |
| 150 | + |
| 151 | + Map<int,string, MapToFirst<int,string,Less> > m; |
| 152 | + |
| 153 | + // insert pairs of (key,value) |
| 154 | + m.insert(p42); |
| 155 | + m.insert(p7); |
| 156 | + cout << "map 42-7: "; |
| 157 | + assert(printAndCount(m) == 2); |
| 158 | + |
| 159 | + // test finding elements via operator() |
| 160 | + cout << "find 42 in map: " << (value=m[42]) << endl; |
| 161 | + assert(value == p42.second()); |
| 162 | + cout << "find 3 in map: " << (value=m[3]) << endl; |
| 163 | + assert(value == string()); |
| 164 | + |
| 165 | + // direct write access via operator[] |
| 166 | + cout << "setting m[3] and m[1]." << endl; |
| 167 | + m[1] = p1.second(); |
| 168 | + m[3] = p3.second(); |
| 169 | + cout << "find 3 in map: " << (value=m[3]) << endl; |
| 170 | + assert(value == p3.second()); |
| 171 | + |
| 172 | + cout << "resulting map: "; |
| 173 | + assert(printAndCount(m) == 4); |
| 174 | + |
| 175 | + // test first() and last(), min() and max() |
| 176 | + Map<int,string>::iterator first = m.first(); |
| 177 | + Map<int,string>::iterator last = m.last(); |
| 178 | + cout << "first in map: " << *first << endl; |
| 179 | + cout << "last in map: " << *last << endl; |
| 180 | + assert(first->first() == 1); |
| 181 | + assert(last->first() == 42); |
| 182 | + assert(m.min() == first->first()); |
| 183 | + assert(m.max() == last->first()); |
| 184 | + |
| 185 | +#endif |
| 186 | + |
| 187 | + cout << "Success!" << endl; |
| 188 | + |
| 189 | + return 0; |
| 190 | +} |
| 191 | + |
0 commit comments