Exemple #1
0
int main()
{
	XList<int> list;
	XList<int>::iterator it;
	XList<int>::reverse_iterator rit;
	
	list.push_back(11);
	list.push_front(8);
	list.push_back(12);
	list.push_back(13);
	list.push_back(14);

	list.print();
	std::cout<<list.size()<<std::endl;

	it = list.begin();
	std::cout<<*(++it)<<std::endl;

	for (it = list.begin(); it != list.end(); ++it)
		std::cout<<*it<<" ";
	std::cout<<std::endl;

	for (rit = list.rbegin(); rit != list.rend(); ++rit)
		std::cout<<*rit<<" ";
	std::cout<<std::endl;

	it = list.end();

	return 0;
}
Exemple #2
0
int main() {	
	srand((unsigned) time(NULL));

	try {
		XList<Shape*> list;
		// filling list of figures with random figures of all types.
		int numberOfElements = rand() % 100;
		for(int i = 0; i < numberOfElements; i++) {
			switch (i % 5) {
			case 0:
				list.pushElementToBack(new Point("TestPoint", rand() % 100, rand() % 100));
				break;
			case 1:
				list.pushElementToBack(new Circle("TestCircle", rand() % 100, rand() % 100, rand() % 100));
				break;
			case 2:
				list.pushElementToBack(new Rect("TestRect", rand() % 100, rand() % 100, rand() % 100, rand() % 100));
				break;
			case 3:
				list.pushElementToBack(new Square("TestSquare", rand() % 100, rand() % 100, rand() % 100));
				break;
			case 4:
				Polyline* pol = new Polyline("TestPol");				
				Point p1("pol_point_1", rand() % 100, rand() % 100);
				Point p2("pol_point_2", rand() % 100, rand() % 100);
				Point p3("pol_point_3", rand() % 100, rand() % 100);
				pol->addPoint(p1);
				pol->addPoint(p2);
				pol->addPoint(p3);
				list.pushElementToBack(pol);
				break;
			}
		}

		std::cout << "Number of shapes " << Shape::getNumberOfShapes() << "\n";
		XList<Shape*>::Iterator it = list.begin();
		for(it = list.begin(); it != list.end(); ++it) {
			std::cout << "\n" << **it << "\n";
		}

		std::cout << "Number of shapes " << Shape::getNumberOfShapes() << "\n";
		for(it = list.begin(); it != list.end(); ++it) {
			delete *it;
		}

		// should be zero
		std::cout << "Number of shapes " << Shape::getNumberOfShapes() << "\n";

		return 0;
	} catch (std::exception ex) {
		std::cerr << ex.what();
		return 1;
	}
}
Exemple #3
0
int main(){
    XList<Shape*> shapes;
    Point * point1 = new Point(0., 2., "point_1");
    shapes.push_back(point1);
    Point * point2 = new Point(2., 2., "point_2");
    shapes.push_back(point2);
    Point * point3 = new Point(2., 0., "point_3");
    shapes.push_back(point3);
    Point * point4 = new Point(0., 0., "point_4");
    shapes.push_back(point4);

    Circle * circle = new Circle(point4, 10., "circle_1");
    shapes.push_back(circle);

    Rect * rect = new Rect(point1, point2, point3, point4, "rectangle");
    shapes.push_back(rect);

    try {
        Square * square = new Square(point1, point2, point3, point4, "square");
        shapes.push_back(square);
    } catch (const char* error){
        // std::cout << "EXCEPTION RAISED: " << error << std::endl;
    }


    Polyline * polyline = new Polyline("polyline");
    polyline->AddPoint(point1);
    polyline->AddPoint(point2);
    polyline->AddPoint(point3);
    polyline->AddPoint(point4);
    shapes.push_back(polyline);

    for (XList<Shape*>::iterator it = shapes.begin(); it != NULL; ++it) {
        std::cout << *it;
    }
    printf("Number of shapes = %d\n", Shape::GetCount());
    for (XList<Shape*>::iterator it = shapes.begin(); it != NULL; ++it) {
        delete *it;
    }
    printf("Number of shapes = %d\n", Shape::GetCount());
    return 0;
}
Exemple #4
0
// this function is what is required by task #3 here: https://sites.google.com/site/kostovoop/tasks
//    Составить и отладить программу:
//    - создать некий XList фигур, наполнить его случайным образом конкретными фигурами всех типов;
//    - вывести в std::cout общее кол-во фигур с помощью Shape::GetCount();
//    - напечатать в std::cout сведения обо всех фигурах, находящихся в списке;
//    - очистить память;
//    - вывести в std::cout общее кол-во фигур с помощью Shape::GetCount() – удостовериться, что там 0;
//    - завершить работу программы.
void task3()
{
    XList<Shape> shapesList;
    // pt, circle, rect, square, polyline
    Point * pt = new Point("a point", 10, 100); // 1
    Circle * circle = new Circle("a circle", 30, Point("circle center", 10, 20)); // 2
    Rect * rect = new Rect("a rect", 10, -30, 10, -100); // 2
    Square * square = new Square("a square", 40, 20, 10); // 2
    Polyline * line = new Polyline("line"); // 1
    
    Point * pt1 = new Point("", 10, 20);
    Point * pt2 = new Point("", 20, 20);
    Point * pt3 = new Point("", 20, 40);
    Point * pt4 = new Point("", 10, 40);

    line->AddPoint(*pt1);
    line->AddPoint(*pt2);
    line->AddPoint(*pt3);
    line->AddPoint(*pt4);

    shapesList.pushBack(pt);
    shapesList.pushBack(circle);
    shapesList.pushBack(rect);
    shapesList.pushBack(square);
    shapesList.pushBack(line);
    
    std::cout << "Total number of shapes is: " << Shape::getCount() << std::endl;
    assert(Shape::getCount() == 16); // 5 figures in list and their copies, 4 points in the polyline, rect's origin, square's origin, circle's center
    
    XList<Shape>::Iterator it = shapesList.begin();
    int l = shapesList.numberOfElements() + 1;
    while(--l > 0){
        Shape * shape = it.currentItem();
        std::cout << *shape << std::endl;
        it.next();
    }
    
    shapesList.clearList();
    // freeing memory
    delete pt;
    delete circle;
    delete square;
    delete rect;
    delete line;
    delete pt1;
    delete pt2;
    delete pt3;
    delete pt4;
    
    std::cout << "Total number of shapes is now: " << Shape::getCount() << std::endl;
    assert(Shape::getCount() == 0);
}
Exemple #5
0
void *list_read(void *arguments) {
	bool flag;
	XList<T>* list = (XList<T>*) arguments;
	XListIterator<T> iterator = list->begin();
	flag = true;
	while (true) {
		if (flag) {
			++iterator;
			this_thread::sleep_for(chrono::seconds(1));
			if(iterator == list->end()){
				flag = false;
				cout << "Read Forward: Finished" << endl;
			}
		} else {
			--iterator;
			this_thread::sleep_for(chrono::seconds(2));
			if(iterator == list->begin()){
				flag = true;
				cout << "Read Reverse: Finished" << endl;
			}
		}
	}
	pthread_exit(NULL);
}
Exemple #6
0
void *list_update(void *arguments){
	XList<T>* list = (XList<T>*) arguments;
        XListIterator<T> iterator;
        while (true) {
                iterator = list->begin();
	        for (int i = 0; i < rand()%(static_cast<int>(list->size()) + 1)
				; i++) {
	        	iterator.operator++();
		}
		*iterator = rand();
		cout << "Update Sleep" << endl;
                this_thread::sleep_for (chrono::seconds(4));
		cout << "Update Wakeup" << endl;
        }
	pthread_exit(NULL);
}
Exemple #7
0
int main()
{
	std::cout<<Shape::GetCount()<<std::endl; //0

	XList<Shape*>* shlist = new XList<Shape*>;
	Point* point0 = new Point("simple_point");
	Point* point1 = new Point(1,1);
	Point* point2 = new Point(-2.2, 2);
	Circle* circle = new Circle("bublic", *point0,5);
	Rect* rect1 = new Rect(*point1, *point2);
	Square* square = new Square(*point0, *point1);
	Rect* rect2 = new Rect(*square);
	Polyline* pl = new Polyline;
	pl->AddPoint(*point0);
	pl->AddPoint(*point1);
	pl->AddPoint(*point2);
	Square* sq = NULL;
	try
	{
		sq = new Square("ABBA", *point1, *point2);
		shlist->push_front(sq);
	}
	catch (std::exception& e)
	{
		std::cout << e.what() << std::endl;
	}
	try
	{
		sq = new Square("ABBA2", *point1, *point0);
		shlist->push_front(sq);
	}
	catch (std::exception& e)
	{
		std::cout << e.what() << std::endl;
	}

	circle->Print();

	std::cout<<Shape::GetCount()<<std::endl; //21

	shlist->push_back(point0);
	shlist->push_back(rect2);
	shlist->push_back(circle);
	shlist->push_front(rect1);
	shlist->push_back(square);
	shlist->push_back(pl);
	shlist->push_back(point2);
	shlist->push_front(point1);
	

	std::cout<<Shape::GetCount()<<std::endl; //21

	for (XList<Shape*>::iterator it = shlist->begin(); it != shlist->end(); ++it)
	{
		std::cout<<**it<<std::endl;
	}
	std::cout<<Shape::GetCount()<<std::endl; //21
	
	for (XList<Shape*>::iterator it = shlist->begin(); it != shlist->end(); ++it)
	{
		delete *it;
	}
	std::cout<<Shape::GetCount()<<std::endl; //0

	delete shlist;

	std::cout<<Shape::GetCount()<<std::endl; //0
	Named *myshape = new Point(0, 0);
	delete myshape;
	std::cout<<Shape::GetCount()<<std::endl; //0
	return 0;
}