Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// wenn du das hier definierst... float fnZ_1_P(int nZ_1_W, float fZ_1_R) { float fWasserpegel; fWasserpegel = nZ_1_W/PI/(fZ_1_R*fZ_1_R); return fWasserpegel; } int main() { [.,..] // Kannst du das doch nicht so aufrufen O_X, // du musst schon die Werte übergeben... // Hast du ein C++-Buch? Wenn ja ->lies es :D // Besonders die Abschnitte Funktionen und Klassen. fZ_1_P = fnZ_1_P(); cout << endl <<fZ_1_P<<endl; return 1; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <cmath> #include <iostream> #ifndef PI # define PI 3.14 #endif /* PI */ class Zylinder { private: Zylinder() {} public: Zylinder(const float radius, const float inhalt) { m_inhalt = inhalt; m_radius = radius; } Zylinder(const Zylinder& z) { m_inhalt = z.m_inhalt; m_radius = z.m_radius; } virtual ~Zylinder() {} Zylinder& operator= (const Zylinder& z) { m_inhalt = z.getInhalt(); m_radius = z.getRadius(); } float getInhalt() const { return m_inhalt; } float getRadius() const { return m_radius; } private: float m_radius; float m_inhalt; }; class ZylinderRechner { public: ZylinderRechner() : m_ergebnis(0) {} virtual ~ZylinderRechner() {} ZylinderRechner(const ZylinderRechner& c) { m_ergebnis = c.m_ergebnis; } float rechne( const Zylinder& z ) { m_ergebnis = (z.getInhalt() / PI) / (z.getRadius() * z.getRadius() ); return m_ergebnis; } float getErgebnis() const{ return m_ergebnis; } // Obey the rule of three. ZylinderRechner& operator=(const ZylinderRechner& c) { m_ergebnis = c.m_ergebnis; return *this; } private: float m_ergebnis; }; int main() { Zylinder zylinder = Zylinder(1.0f, 2.0f); ZylinderRechner rechner; float ergebnis = rechner.rechne(zylinder); std::cout << "Ergebnis: " << ergebnis << std::endl; // oder std::cout << "Ergebnis: " << rechner.getErgebnis() << std::endl; return 0; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <cmath> #include <iostream> //#ifndef PI //# define PI 3,14 //#endif /* PI */ static const float PI = 3.14f; // PUNKT nicht KOMMA!!! class Zylinder { private: //inline Zylinder() {} // irrelevant public: /*inline braucht kein Mensch hier*/Zylinder(const float radius, const float inhalt) : m_inhalt( inhalt ), m_radius( radius ) { /*m_inhalt = inhalt; m_radius = radius; => es gibt eine Initialisationsliste */ } /* => WOZU braucht man hier so etwas?? Oo inline Zylinder(const Zylinder& z) { m_inhalt = z.m_inhalt; m_radius = z.m_radius; } */ // inline virtual ~Zylinder() {} -> weg mit dem unnützen Zeug! /* Auch unnötig inline Zylinder& operator= (const Zylinder& z) { m_inhalt = z.getInhalt(); m_radius = z.getRadius(); } */ /*inline weg*/ float getInhalt() const { return m_inhalt; } /*inline weg*/ float getRadius() const { return m_radius; } private: float m_radius; float m_inhalt; }; class ZylinderRechner { public: /*inline weg*/ ZylinderRechner() : m_ergebnis(0) {} //virtual inline ~ZylinderRechner() {} /* Unsinnig inline ZylinderRechner(const ZylinderRechner& c) { m_ergebnis = c.m_ergebnis; } */ /*inline */float rechne( const Zylinder& z ) { m_ergebnis = (z.getInhalt() / PI) / (z.getRadius() * z.getRadius() ); return m_ergebnis; } /*inline*/ float getErgebnis() const{ return m_ergebnis; } // Obey the rule of three. => kannste hier vergessen /*inline ZylinderRechner& operator=(const ZylinderRechner& c) { m_ergebnis = c.m_ergebnis; return *this; } */ private: float m_ergebnis; }; int main() { Zylinder zylinder = Zylinder(1.0f, 2.0f); ZylinderRechner rechner; float ergebnis = rechner.rechne(zylinder); std::cout << "Ergebnis: " << ergebnis << std::endl; // oder std::cout << "Ergebnis: " << rechner.getErgebnis() << std::endl; return 0; } |
Zitat
David, kannst Du mir bitte erklären, warum Singletons grenzwertig sind? Ich verwende die eigentlich recht gerne, wenn ich zu 100% weiss, dass von einer Klasse nur ein Objekt benötigt wird oder nur eins existieren darf.
Zitat
WOZU braucht man hier so etwas?? Oo
Zitat von »"Nico"«
David, das hat schon sinn gemacht mit dem privaten Konstruktor.... ich würde es zumindest so machen oder noch eine set-Methode einfügen.. oO
Zitat von »"Nico"«
Und die Regel der 3 kann man hier bestimmt _nicht_ vergessen.. gerade bei so mathematischen Klassen, wo man öfters mal rumkopiert....
Zitat von »"Nico"«
Ich hab mir bei dem Code schon was gedacht... das einzigste was richtig ist, ist dass das Komma falsch war... und ob ich jetzt PI per #define anlege oder eine statische globale Variable nehme, is vollkommen egal.
Zitat von »"Nico"«
Noch nie etwas von einem Kopierkonstruktor gehört?
lg
Zitat von »"Nico"«
Ob man den CopyCtor oder operator= jetzt anlegt oder nicht ist eigentlich egal.
Ich code halt nach der maxime "Was gemacht ist, ist gemacht". Und wenns im nachhinein doch nötig wird, dann muss man den Header nicht mehr ändern. Gerade bei DLL's is das wichtig, da man sonst die ClientAnwendung neu kompilieren muss. Wenn sich nur der Inhalt der Func ändert, eben nicht .
Aber is ja auch egal.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
template< typename T, typename U > T LexicalCast( const U& value ) { std::stringstream stream; T result; if ( !( stream << value && stream >> result && ( stream >> std::ws ).eof() ) ) throw std::logic_error( "LexicalCast failed" ); return result; } class MathConstants { public: static const float PI; static const float INV_PI; }; const float MathConstants::PI = std::atanf( 1.0f ) * 4.0f; const float MathConstants::INV_PI = 1.0f / PI; class GeomObject { public: struct ParseError : std::exception { ParseError( const char* const& desc ) : std::exception( desc ) {} }; virtual ~GeomObject() {} virtual std::ostream& WriteToStream( std::ostream& stream ) const = 0; virtual std::istream& ReadFromStream( std::istream& stream ) = 0; friend std::ostream& operator<<( std::ostream& stream, const GeomObject& c ) { return c.WriteToStream( stream ); } friend std::istream& operator>>( std::istream& stream, GeomObject& c ) { return c.ReadFromStream( stream ); } }; class Cylinder : public GeomObject { private: float mRadius; float mHeight; float mVolume; public: Cylinder( float r = .0f, float h = .0f, float v = .0f ) : mRadius( r ) , mHeight( h ) , mVolume( v ) {} static Cylinder FromRadiusHeight( float r, float h ) { return Cylinder( r, h, MathConstants::PI * r * r * h ); } static Cylinder FromHeightVolume( float h, float v ) { return Cylinder( std::sqrtf( v * MathConstants::INV_PI / h ), h, v ); } static Cylinder FromRadiusVolume( float r, float v ) { return Cylinder( r, v * MathConstants::INV_PI / ( r * r ), v ); } std::ostream& WriteToStream( std::ostream& stream ) const { return ( stream << "r: " << mRadius << " h: " << mHeight << " v: " << mVolume ); } std::istream& ReadFromStream( std::istream& stream ) { static const char* parserLUT[] = { "r: ", "h: ", "v: " }; int i = 0; std::string line( "" ); char c = 0; float v[ 3 ]; while ( i < 2 ) { std::getline( stream, line ); for ( int j = 0; j < 3; ++j ) { if ( line.compare( 0, 3, parserLUT[ j ] ) == 0 ) { c |= 1 << j; try { v[ j ] = LexicalCast< float >( line.substr( 3 ) ); } catch ( ... ) { v[ j ] = 0.0f; } break; } } ++i; } if ( c & 0x1 && c & 0x2 ) { *this = Cylinder::FromRadiusHeight( v[ 0 ], v[ 1 ] ); } else if ( c & 0x1 && c & 0x4 ) { *this = Cylinder::FromRadiusVolume( v[ 0 ], v[ 2 ] ); } else if ( c & 0x2 && c & 0x4 ) { *this = Cylinder::FromHeightVolume( v[ 1 ], v[ 2 ] ); } else { throw new ParseError( "parsing failed" ); } return stream; } }; void ReadCylinderValue( const std::string& prompt, Cylinder& c ) { while ( true ) { std::cout << prompt << std::endl; try { std::cin >> c; } catch ( ... ) { std::cout << "Falsche Eingabe: Gueltig ist( kuerzel: <wert> wobei kuerzel sein kann (r:, h: oder v:))" << std::endl; } } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
#include <iostream> using namespace std; //Konstanten const float PI = 3.141592654; //Funktionsprototypen int main (); float fnZylinder_Wasser(int cIndex_01); float fnZylinder_Radius(int cIndex_02); float fnZylinder_1_Wasserpegel(float nZylinder_1_Wasser, float fZylinder_1_Radius); float fnZylinder_2_Wasserpegel(float nZylinder_2_Wasser, float fZylinder_2_Radius); float fnZylinder_3_Wasser(float fZylinder_1_Wasserpegel, float fZylinder_3_Radius); float fnZylinder_4_Wasser(float fZylinder_2_Wasserpegel, float fZylinder_4_Radius); float fnZylinder_1_3_Wasser(float fZylinder_1_Wasser, float fZylinder_3_Wasser); float fnZylinder_2_4_Wasser(float fZylinder_2_Wasser, float fZylinder_4_Wasser); int fnAnzahl_der_Durchfuehrungen(); void Ausgeben(int cCounter, float fZylinder_1_Wasser, float fZylinder_2_Wasser); bool Quit(); float fnZylinder_Wasser(int cIndex_01) { float fWasser; cout << "Geben Sie an, wie viel Wasser in [ml] Zylinder " << cIndex_01 << " beinhaltet: "; cin >> fWasser; while (fWasser < 0) { cout << "Die Menge an Wasser kann nicht negativ sein, bitte neue Eingabe: "; cin >> fWasser; } return fWasser; } float fnZylinder_Radius(int cIndex_02) { float fRadius = 1; cout << "Geben Sie an, welchen Radius in [cm] Zylinder " << cIndex_02 << " hat: "; cin >> fRadius; while (fRadius <= 0) { cout << "Der Radius kann nicht 0 oder negativ sein. Bitte neue Eingabe: "; cin >> fRadius; } return fRadius; } float fnZylinder_1_Wasserpegel(float nZylinder_1_Wasser, float fZylinder_1_Radius) { float fWasserpegel; fWasserpegel = nZylinder_1_Wasser/PI/(fZylinder_1_Radius*fZylinder_1_Radius); return fWasserpegel; } float fnZylinder_2_Wasserpegel(float nZylinder_2_Wasser, float fZylinder_2_Radius) { float fWasserpegel; fWasserpegel = nZylinder_2_Wasser/PI/(fZylinder_2_Radius*fZylinder_2_Radius); return fWasserpegel; } float fnZylinder_3_Wasser(float fZylinder_1_Wasserpegel, float fZylinder_3_Radius) { float fWasser; fWasser = fZylinder_1_Wasserpegel*PI*(fZylinder_3_Radius*fZylinder_3_Radius); return fWasser; } float fnZylinder_4_Wasser(float fZylinder_2_Wasserpegel, float fZylinder_4_Radius) { float fWasser; fWasser = fZylinder_2_Wasserpegel*PI*(fZylinder_4_Radius*fZylinder_4_Radius); return fWasser; } float fnZylinder_1_3_Wasser(float fZylinder_1_Wasser, float fZylinder_3_Wasser) { float dDifferenz; dDifferenz = fZylinder_1_Wasser - fZylinder_3_Wasser; return dDifferenz; } float fnZylinder_2_4_Wasser(float fZylinder_2_Wasser, float fZylinder_4_Wasser) { float dDifferenz; dDifferenz = fZylinder_2_Wasser - fZylinder_4_Wasser; return dDifferenz; } float fnAdd_Zylinder_1_4(float dZylinder_1_3_Wasser, float fZylinder_4_Wasser) { float fWasser; fWasser = dZylinder_1_3_Wasser + fZylinder_4_Wasser; return fWasser; } float fnAdd_Zylinder_2_3(float dZylinder_2_4_Wasser, float fZylinder_3_Wasser) { float fWasser; fWasser = dZylinder_2_4_Wasser + fZylinder_3_Wasser; return fWasser; } int fnAnzahl_der_Durchfuehrungen() { int nAnzahl_der_Durchfuehrungen; cout << "\n\n Wie oft soll Umgefuellt werden? : "; cin >> nAnzahl_der_Durchfuehrungen; while (nAnzahl_der_Durchfuehrungen< 0) { cout << "Leider nicht möglich; neue Eingabe: "; cin >> nAnzahl_der_Durchfuehrungen; } return nAnzahl_der_Durchfuehrungen; } void Ausgeben(int cCounter, float fZylinder_1_Wasser, float fZylinder_2_Wasser) { cout << "\nDurchlauf: " << cCounter << " Wasser in Zylinder 1: " << fZylinder_1_Wasser << " Wasser in Zylinder 2: " << fZylinder_2_Wasser ; } bool Quit() { char chInput; cout << "\nMoechten Sie das Programm beenden? Ja: y; Nein: beliebige Taste : "; cin >> chInput; if (chInput == 121) return true; else return false; } int main() { bool bQuit; while (bQuit != true) { int nAnzahl_der_Durchfuehrungen; int cCounter = 0; int cIndex_01 = 1; int cIndex_02 = 1; float fZylinder_1_Wasser; float fZylinder_2_Wasser; float fZylinder_3_Wasser; float fZylinder_4_Wasser; float fZylinder_1_Radius; float fZylinder_2_Radius; float fZylinder_3_Radius; float fZylinder_4_Radius; float fZylinder_1_Wasserpegel; float fZylinder_2_Wasserpegel; float dZylinder_1_3_Wasser; float dZylinder_2_4_Wasser; fZylinder_1_Wasser = fnZylinder_Wasser(cIndex_01); cIndex_01++; fZylinder_2_Wasser = fnZylinder_Wasser(cIndex_01); fZylinder_1_Radius = fnZylinder_Radius(cIndex_02); cIndex_02++; fZylinder_2_Radius = fnZylinder_Radius(cIndex_02); cIndex_02++; fZylinder_3_Radius = fnZylinder_Radius(cIndex_02); cIndex_02++; fZylinder_4_Radius = fnZylinder_Radius(cIndex_02); nAnzahl_der_Durchfuehrungen = fnAnzahl_der_Durchfuehrungen(); while (cCounter < nAnzahl_der_Durchfuehrungen) { fZylinder_1_Wasserpegel = fnZylinder_1_Wasserpegel(fZylinder_1_Wasser, fZylinder_1_Radius); fZylinder_2_Wasserpegel = fnZylinder_2_Wasserpegel(fZylinder_2_Wasser, fZylinder_2_Radius); fZylinder_3_Wasser = fnZylinder_3_Wasser(fZylinder_1_Wasserpegel, fZylinder_3_Radius); fZylinder_4_Wasser = fnZylinder_4_Wasser(fZylinder_2_Wasserpegel, fZylinder_4_Radius); dZylinder_1_3_Wasser = fnZylinder_1_3_Wasser(fZylinder_1_Wasser, fZylinder_3_Wasser); dZylinder_2_4_Wasser = fnZylinder_2_4_Wasser (fZylinder_2_Wasser, fZylinder_4_Wasser); fZylinder_1_Wasser = fnAdd_Zylinder_1_4(dZylinder_1_3_Wasser, fZylinder_4_Wasser); fZylinder_2_Wasser = fnAdd_Zylinder_2_3(dZylinder_2_4_Wasser, fZylinder_3_Wasser); Ausgeben(cCounter, fZylinder_1_Wasser, fZylinder_2_Wasser); cCounter++; } bQuit = Quit(); cout <<"\n\n\n\n"; } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
float fnZylinder_1_Wasser() { float fWasser; cout << "Geben Sie an, wie viel Wasser in [ml] Zylinder 1 beinhaltet: "; cin >> fWasser; while (fWasser < 0) { cout << "Die Menge an Wasser kann nicht negativ sein, bitte neue Eingabe: "; cin >> fWasser; } return fWasser; } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
float fnZylinder_Wasser(const int index) { float fWasser; cout << "Geben Sie an, wie viel Wasser in [ml] Zylinder " << index << " beinhaltet: "; cin >> fWasser; while (fWasser < 0) { cout << "Die Menge an Wasser kann nicht negativ sein, bitte neue Eingabe: "; cin >> fWasser; } return fWasser; } |
Werbeanzeige