Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабораторная работа 22 / lab2_2 / lab2_2
.cpp// lab1_1.cpp : Defines the entry point for the console application.
//
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <ctime>
#include "stdafx.h"
#include "Hash.h"
#include "HashElem.h"
#include "TextInTriangle.h"
#include "utils.h"
#include <string>
#include <iostream>
using namespace std;
void clrscr(){
system("cls");
}
Shape* createRandomShape(){
int random = (rand()%4)+1;
switch(random){
case 1:
return new Point();
case 2:
return new Line();
case 3:
return new Triangle();
case 4:
return new TextInTriangle();
default:
return new Point();
}
}
void printHashObjectStats(){
cout << "-----" << endl;
cout << "Hash: " << Hash<intKey, Point>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<intKey, Point>::getNumberOfObjects() << endl;
cout << "intKey: " << intKey::count << endl;
cout << "-----" << endl;
}
void printShapeObjectStats(){
cout << "-----" << endl;
cout << "Shape: " << Shape::getNumberOfObjects() << endl;
cout << "Point: " << Point::getNumberOfObjects() << endl;
cout << "Line: " << Line::getNumberOfObjects() << endl;
cout << "Triangle: " << Triangle::getNumberOfObjects() << endl;
cout << "Text: " << Text::getNumberOfObjects() << endl;
cout << "TextInTriangle: " << TextInTriangle::getNumberOfObjects() << endl;
cout << "-----" << endl;
}
template <class K, class V>
void printMap(Hash<K, V>& h){
Hash<K, V>::iterator it = h.begin();
while(it != h.end()){
pair<K, V> he = it++;
cout << "key: " << he.first << endl;
cout << "value: " << he.second << endl << endl;
}
}
template <class K, class V>
void removeElement(pair<pair<K, V>, bool> el){
if (el.second == true){
cout << "removed" << endl;
cout << "now delete object it holds:" << endl;
delete el.first.second; // el.first is HashPair, HashPair.second is Value
}
else{
cout << "not found" << endl;
}
};
int main(int argc, char* argv[])
{
clrscr();
cout << "======================" << endl;
cout << "Test HashMap container" << endl;
cout << "======================" << endl;
getch();
cout << "Initial objects' statistics:" << endl;
printShapeObjectStats();
getch();
{
// test container creation
cout << "Create HashMap that maps integer keys on Shape pointers" << endl;
getch();
Hash<intKey, Shape*> h;
cout << "HashMap was created" << endl;
getch();
cout << "Check objects' statistics:" << endl;
printShapeObjectStats();
getch();
cout << "Hash: " << Hash<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "intKey: " << intKey::count << endl;
getch();
cout << "Create random Shape objects and put them in container" << endl;
getch();
srand((unsigned)time(0));
for (int i = 0; i < 8; i++){
cout << endl << "-------------" << endl;
Shape* shape = createRandomShape();
intKey key(i);
cout << endl;
cout << "intKey: " << key << endl;
cout << "Shape: " << shape << endl << endl;
getch();
cout << "Now put it in container" << endl;
getch();
h.put(key, shape);
}
cout << "Check objects' statistics:" << endl;
getch();
printShapeObjectStats();
cout << "Hash: " << Hash<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "intKey: " << intKey::count << endl;
getch();
cout << "Print HashMap" << endl;
getch();
printMap<intKey, Shape*>(h);
getch();
cout << endl;
cout << "==============" << endl;
cout << "Test rehashing" << endl;
cout << "==============" << endl;
getch();
cout << "Create random shape" << endl;
getch();
Shape* shape = createRandomShape();
intKey key(20);
cout << endl;
cout << "intKey: " << key << endl;
cout << "Shape: " << shape << endl << endl;
getch();
h.put(key, shape);
cout << "Element was inserted. Now check hash" << endl;
getch();
cout << "Print HashMap" << endl;
getch();
printMap<intKey, Shape*>(h);
getch();
cout << "Test remove method" << endl;
cout << "remove key(14): ";
removeElement<intKey, Shape*>(h.remove(intKey(14)));
getch();
cout << "remove key(5): ";
removeElement<intKey, Shape*>(h.remove(intKey(5)));
getch();
cout << endl;
cout << "Print HashMap" << endl;
getch();
printMap<intKey, Shape*>(h);
getch();
cout << "Now destroy objects this hash holds" << endl;
getch();
Hash<intKey, Shape*>::iterator it = h.begin();
while(it != h.end()){
pair<intKey, Shape*> he = it++;
delete he.second;
}
}
cout << "Check objects' statistics:" << endl;
getch();
printShapeObjectStats();
cout << "Hash: " << Hash<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<intKey, Shape*>::getNumberOfObjects() << endl;
cout << "intKey: " << intKey::count << endl;
getch();
cout << endl;
cout << "=====================" << endl;
cout << "Test HashMap iterator" << endl;
cout << "=====================" << endl;
getch();
cout << "Check objects' statistics:" << endl;
printShapeObjectStats();
getch();
{
cout << "Create HashMap that maps string keys on Shape pointers" << endl;
getch();
Hash<stringKey, Shape*> h(8);
cout << "ins point: " << h.put(stringKey("point"), new Point(1,2)).second << endl << endl;
cout << "ins triangle: " << h.put(stringKey("triangle"), new Triangle()).second << endl << endl;
cout << "ins text: " << h.put(stringKey("text"), new Text("some text")).second << endl << endl;
cout << "ins line: " << h.put(stringKey("line"), new Line()).second << endl << endl;
cout << "ins textInTriangle: " << h.put(stringKey("textInTriangle"), new TextInTriangle()).second << endl << endl;
getch();
cout << "Check objects' statistics:" << endl;
getch();
printShapeObjectStats();
cout << "Hash: " << Hash<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "stringKey: " << stringKey::count << endl;
getch();
cout << "Print HashMap: " << endl;
getch();
printMap<stringKey, Shape*>(h);
getch();
cout << "Test find method" << endl;
getch();
Hash<stringKey, Shape*>::iterator it = h.find(stringKey("triangle"));
cout << "hash[\"triangle\"] = ";
if (it != h.end())
cout << (*it).second << endl;
else
cout << "not found" << endl;
getch();
it = h.find(stringKey("square"));
cout << "hash[\"square\"] = ";
if (it != h.end())
cout << (*it).second << endl;
else
cout << "not found" << endl;
getch();
cout << "Check objects' statistics:" << endl;
getch();
printShapeObjectStats();
cout << "Hash: " << Hash<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "stringKey: " << stringKey::count << endl;
getch();
cout << endl;
cout << "Print hash using inner iterator:" << endl;
Hash<stringKey, Shape*>::iterator it2 = h.getIterator();
while(it2 != h.end()){
pair<stringKey, Shape*> he = it2++;
cout << "key: " << he.first << endl;
cout << "value: " << he.second << endl << endl;
}
getch();
cout << "Now destroy objects this hash holds using iterator" << endl;
getch();
it = h.begin();
while(it != h.end()){
pair<stringKey, Shape*> he = it++;
delete he.second;
}
getch();
}
cout << "Check objects' statistics:" << endl;
getch();
printShapeObjectStats();
cout << "Hash: " << Hash<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "HashElem: " << HashElem<stringKey, Shape*>::getNumberOfObjects() << endl;
cout << "stringKey: " << stringKey::count << endl;
getch();
return 0;
}
Соседние файлы в папке lab2_2