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/Teil1/Teil1.dsp | 108 ++++++++++++++++++++++++++++ Bachelor/Prog2/Z-Uebung/Teil1/main1.cpp | 61 ++++++++++++++++ Bachelor/Prog2/Z-Uebung/Teil1/shape.cpp | 114 ++++++++++++++++++++++++++++++ Bachelor/Prog2/Z-Uebung/Teil1/shape.h | 120 ++++++++++++++++++++++++++++++++ 4 files changed, 403 insertions(+) create mode 100644 Bachelor/Prog2/Z-Uebung/Teil1/Teil1.dsp create mode 100644 Bachelor/Prog2/Z-Uebung/Teil1/main1.cpp create mode 100644 Bachelor/Prog2/Z-Uebung/Teil1/shape.cpp create mode 100644 Bachelor/Prog2/Z-Uebung/Teil1/shape.h (limited to 'Bachelor/Prog2/Z-Uebung/Teil1') diff --git a/Bachelor/Prog2/Z-Uebung/Teil1/Teil1.dsp b/Bachelor/Prog2/Z-Uebung/Teil1/Teil1.dsp new file mode 100644 index 0000000..3fd8d7a --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil1/Teil1.dsp @@ -0,0 +1,108 @@ +# Microsoft Developer Studio Project File - Name="Teil1" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** NICHT BEARBEITEN ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=Teil1 - Win32 Debug +!MESSAGE Dies ist kein gültiges Makefile. Zum Erstellen dieses Projekts mit NMAKE +!MESSAGE verwenden Sie den Befehl "Makefile exportieren" und führen Sie den Befehl +!MESSAGE +!MESSAGE NMAKE /f "Teil1.mak". +!MESSAGE +!MESSAGE Sie können beim Ausführen von NMAKE eine Konfiguration angeben +!MESSAGE durch Definieren des Makros CFG in der Befehlszeile. Zum Beispiel: +!MESSAGE +!MESSAGE NMAKE /f "Teil1.mak" CFG="Teil1 - Win32 Debug" +!MESSAGE +!MESSAGE Für die Konfiguration stehen zur Auswahl: +!MESSAGE +!MESSAGE "Teil1 - Win32 Release" (basierend auf "Win32 (x86) Console Application") +!MESSAGE "Teil1 - Win32 Debug" (basierend auf "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "Teil1 - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD BASE RSC /l 0x407 /d "NDEBUG" +# ADD RSC /l 0x407 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "Teil1 - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GR- /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD BASE RSC /l 0x407 /d "_DEBUG" +# ADD RSC /l 0x407 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "Teil1 - Win32 Release" +# Name "Teil1 - Win32 Debug" +# Begin Group "Quellcodedateien" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\main1.cpp +# End Source File +# Begin Source File + +SOURCE=.\shape.cpp +# End Source File +# End Group +# Begin Group "Header-Dateien" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\shape.h +# End Source File +# End Group +# Begin Group "Ressourcendateien" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/Bachelor/Prog2/Z-Uebung/Teil1/main1.cpp b/Bachelor/Prog2/Z-Uebung/Teil1/main1.cpp new file mode 100644 index 0000000..3dbd65d --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil1/main1.cpp @@ -0,0 +1,61 @@ +// Übung PG 2, Teil 1 +// test Shape and Polyline +// Author: Prinz / Kirch-Prinz / Weber +// Date: 26.05.05 + +#include +using std::cout; +using std::endl; +using std::cin; + +#include "shape.h" + +int main() +{ + cout << "\nGeometrische Figuren: Linienzuege\n" << endl; + + Point vertices[] = { Point( -1, 0 ), Point( 0, 2 ), Point( 1, 0 ) }; + + Polyline poly1, + poly2( Point( 1, 2 ), Point( 2, 0 ) ), + poly3( vertices, 3 ), + poly4( poly3 ); // use of copy constructor + + cout << "Die Punkte der vier Linienzuege: \n" + << poly1.toString() << endl + << poly2.toString() << endl + << poly3.toString() << endl + << poly4.toString() << endl; + cin.get(); + + cout << "Zuweisungen testen:" << endl; // test assignment + poly1 = poly4; + cout << poly1.toString() << endl; + poly1 = Polyline(); + cout << poly1.toString() << endl << endl; + + cout << "Punkte anhaengen:" << endl; // append points + poly1 += Point( 0.5, 2.5 ); + cout << poly1.toString() << endl; + poly4 += Point( 2, 2 ); + cout << poly4.toString() << endl; + cout << "Anzahl Linien: " << poly4.getNumberOfLines() << endl + << "Laenge : " << poly4.getLength() << endl; + + cout << "Nach der Skalierung mit dem Faktor 2.0" << endl; + poly4.scale( 2.0 ); + cout << poly4.toString() << endl << endl; + + cout << "Linienzuege anhaengen:" << endl; // append lines + poly2 += poly1; + cout << poly2.toString() << endl; + poly4 += poly3; + cout << poly4.toString() << endl << endl; + + cout << "Linienzug verschieben:" << endl; // displacement + poly4.move( 1,0 ); + cout << poly4.toString() << endl; + cout << "Neuer Anker: " << poly4.getAnchor() << endl; + + return 0; +} diff --git a/Bachelor/Prog2/Z-Uebung/Teil1/shape.cpp b/Bachelor/Prog2/Z-Uebung/Teil1/shape.cpp new file mode 100644 index 0000000..b23dfab --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil1/shape.cpp @@ -0,0 +1,114 @@ +// shape.cpp +// Implementation of classes Shape and Polyline. + +#include "shape.h" + +// class Shape +// all methods inline + +// class Polyline +Polyline::Polyline( Point arr[], int n ) +{ + arrPoints = NULL; + nLines = 0; + if( n > 0 ) { + anchor = arr[ 0 ]; // anchor + if( n > 1 ) { + nLines = n - 1; + // save points relatively to anchor + arrPoints = new Point[ nLines ]; + for( int i = 0; i < n-1; ++i ) + arrPoints[ i ] = arr[ i + 1 ] - anchor; + } + } +} + +Polyline::Polyline( const Polyline& src ) // copy constructor +{ + nLines = src.nLines; + anchor = src.anchor; + arrPoints = NULL; + + if( src.nLines > 0 ) { + arrPoints = new Point[ nLines ]; + for( int i = 0; i < nLines; ++i ) + arrPoints[ i ] = src.arrPoints[ i ]; + } +} + +Polyline& Polyline::operator=( const Polyline& src ) // assignment +{ + if( this != &src ) { + delete [] arrPoints; + nLines = src.nLines; + anchor = src.anchor; + arrPoints = NULL; + if( src.nLines > 0 ) { + arrPoints = new Point[ nLines ]; + for( int i = 0; i < nLines; ++i ) + arrPoints[ i ] = src.arrPoints[ i ]; + } + } + return *this; +} + +double Polyline::getLength() const +{ + double len = 0.0; + if( nLines > 0 ) { + Point begin( 0, 0 ); + for( int i = 0; i < nLines; ++i ) { + len += begin.distance( arrPoints[ i ] ); + begin = arrPoints[ i ]; + } + } + return len; +} + +// append one more point: +Polyline& Polyline::operator+=( const Point& p ) +{ + Point* ptr = new Point[ nLines + 1 ]; // new array + for( int i = 0 ; i < nLines; ++i ) // copy points into new array + ptr[ i ] = arrPoints[ i ]; + ptr[ nLines ] = p - anchor; // add new point + ++nLines; + delete [] arrPoints; // free old array + arrPoints = ptr; + return *this; +} + +// append a second line: +Polyline& Polyline::operator+=( const Polyline& src ) +{ + if( src.nLines > 0 ) { + Point last = Point( 0, 0 ); // rel. coordinates of last point + if( nLines > 0 ) + last = arrPoints[ nLines - 1 ]; + Point* ptr = new Point[ nLines + src.nLines ]; // new array + int i; + for( i = 0; i < nLines; ++i ) // copy points into new array + ptr[ i ] = arrPoints[ i ]; + for( i = 0; i < src.nLines; ++i ) // append points of src + ptr[ nLines + i ] = last + src.arrPoints[ i ]; + + nLines += src.nLines; + delete [] arrPoints; // free old array + arrPoints = ptr; + } + return *this; +} + +void Polyline::scale( double scalingFactor ) +{ + for( int i = 0; i < nLines; ++i ) + arrPoints[ i ] *= scalingFactor; +} + +string Polyline::toString() const // points of line +{ + string str = anchor.toString(); + for( int i = 0; i < nLines; ++i ) + str += " ", str += ( anchor + arrPoints[ i ] ).toString(); + return str; +} diff --git a/Bachelor/Prog2/Z-Uebung/Teil1/shape.h b/Bachelor/Prog2/Z-Uebung/Teil1/shape.h new file mode 100644 index 0000000..a304329 --- /dev/null +++ b/Bachelor/Prog2/Z-Uebung/Teil1/shape.h @@ -0,0 +1,120 @@ +// shape.h +// Definition of classes Point, Shape and Polyline. + +#ifndef SHAPE_H +#define SHAPE_H + +#include +using std::string; +using std::stringstream; +using std::ostream; + +#include // sqrt + +// class Point +struct Point +{ + double x, y; + Point( double xx = 0.0, double yy = 0.0 ) + : x( xx ), y( yy ) { } + + double distance( const Point& p2 ) const + { + double dx = x - p2.x, + dy = y - p2.y; + return sqrt( dx * dx + dy * dy ); + } + string toString() const + { + stringstream sstream; + sstream << '(' << x << ", " << y << ')'; + return sstream.str(); + } + Point& operator*=( double c ) + { x *= c; y *= c; return *this; } +}; + +inline Point operator+( const Point& p1, const Point& p2 ) +{ + return Point( p1.x + p2.x, p1.y + p2.y ); +} + +inline Point operator-( const Point& p1, const Point& p2 ) +{ + return Point( p1.x - p2.x, p1.y - p2.y ); +} + +inline bool operator==( const Point& p1, const Point& p2 ) +{ + return p1.x == p2.x && p1.y == p2.y; +} + +inline bool operator!=( const Point& p1, const Point& p2 ) +{ + return !( p1 == p2 ); +} + +inline ostream& operator<<( ostream& os, const Point& p ) +{ + os << p.toString(); + return os; +} + +// class Shape +class Shape +{ + protected: + Point anchor; // anchor: point of reference for position of figure + public: + Shape( const Point& a = Point() ) : anchor( a ) { } + virtual ~Shape() {} + + Point getAnchor() const { return anchor; } + void setAnchor( Point a ) { anchor = a; } + + void move( double dx, double dy ) // displacement + { anchor.x += dx; anchor.y += dy; } + + virtual void scale( double scalingFactor ) = 0; + virtual void draw() const = 0; + + virtual string toString() const + { + string str( "Shape-Anker: " ); + return str += anchor.toString(); + } +}; + +// class Polyline +class Polyline : public Shape +{ + protected: + Point* arrPoints; // end-points of lines, relativ to anchor + int nLines; // number of line-segments == number of end-points + + public: + Polyline( const Point& a = Point() ) // only one point + : Shape( a ), nLines( 0 ), arrPoints( NULL ) { } + Polyline( const Point& p1, const Point& p2 ) // one line + : Shape( p1 ), nLines( 1 ) + { + arrPoints = new Point( p2 - p1 ); + } + Polyline( Point arr[], int n ); + Polyline( const Polyline& src ); // copy constructor + ~Polyline() { delete [] arrPoints; } // destructor + + Polyline& operator=( const Polyline& src ); // assignment + + int getNumberOfLines() const { return nLines; } + double getLength() const; + void scale( double scalingFactor ); // scaling + virtual void draw() const { } + + Polyline& operator+=( const Point& p ); // appends a new point + Polyline& operator+=( const Polyline& pl ); // appends a new line + + string toString() const; +}; + +#endif -- cgit v1.2.3