From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp | 77 +++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp (limited to 'Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp') diff --git a/Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp b/Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp new file mode 100644 index 0000000..94e76ac --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil3/main3.cpp @@ -0,0 +1,77 @@ +// Übung PG 2, Teil 3 +// inhomogeneous list for Shape-Objects +// Author: Prinz / Kirch-Prinz / Weber +// Date: 26.05.05 + +#include +using std::cout; +using std::endl; +using std::cin; +using std::left; + +#include +#include // class-Template list< T > + // T is type of list elements +#include "shape.h" + +typedef std::list< Shape* > ShapePtrList; + +void printShapeList( const ShapePtrList& spl ); + +int main() +{ + cout << "\n\t *** Eine Liste geometrischer Figuren ***\n" + << endl; + ShapePtrList myShapes; + printShapeList( myShapes ); + + cout << "\nElemente in die Liste einfuegen: " << endl; + // append at end of list: + myShapes.push_back( new Line( Point( 0, 0 ), Point( 2, 2 ) ) ); + myShapes.push_back( new Rectangle( Point( -1, -1 ), 2, 2 ) ); + + // insert ellipse at front of list: + myShapes.push_front( new Ellipse( Point( 0, 0 ), 3, 1) ); + + Point vertices[] = { Point( 0, -3 ), Point( -3, 0 ), Point( 0, 3 ), Point( 3, 0 ) }; + Shape* ptr = new Polygon( vertices, 4 ); // a rombus + // insert polygon as second element: + myShapes.insert( ++myShapes.begin(), ptr ); + + // a circle as last-but-one element: + ShapePtrList::iterator pos = // position of circle + myShapes.insert( --myShapes.end(), new Circle( Point( 0, 0 ), 5 ) ); + ( *pos )->scale( 0.7 ); // make this element smaller + + cout << "Anzahl Elemente in der Liste: " + << myShapes.size() << endl; + printShapeList( myShapes ); // print list + + cout << "Figur vor dem Kreis loeschen ... " << endl; + myShapes.erase( --pos ); + + cout << "und die zweite Figur (das Polygon) verschieben: " << endl; + pos = myShapes.begin(); + ptr = *( ++pos ); // second element = pointer to polygon + ptr->move( 0, 3 ); // move upwards + + cout << "Die veraenderte Liste: " << endl; + printShapeList( myShapes ); // print list + + return 0; +} + +void printShapeList( const ShapePtrList& spl ) +{ + if( spl.empty() ) { + cout << "Die Liste ist leer!" << endl; + return; + } + ShapePtrList::const_iterator pos = spl.begin(); + for( ; pos != spl.end(); ++pos ) { + cout.width(20); + cout << left << typeid( **pos ).name(); + cout << ( *pos )->toString() << endl; + } + cout << endl; +} -- cgit v1.2.3