From da8168f2cf633a2eada6c1ebd96c56fc08af0fd2 Mon Sep 17 00:00:00 2001 From: Patrice Matz Date: Sat, 21 Oct 2017 15:28:48 +0200 Subject: [PATCH] Converted 1.1.3 to OO --- SCE/SCE/2d.cpp | 1 + SCE/SCE/2d.h | 19 +++++++++ SCE/SCE/3d.h | 107 +++++++++++++++++++++++++++++++++++++++++++----- SCE/SCE/SCE.cpp | 100 ++++---------------------------------------- 4 files changed, 123 insertions(+), 104 deletions(-) create mode 100644 SCE/SCE/2d.cpp create mode 100644 SCE/SCE/2d.h diff --git a/SCE/SCE/2d.cpp b/SCE/SCE/2d.cpp new file mode 100644 index 0000000..3712970 --- /dev/null +++ b/SCE/SCE/2d.cpp @@ -0,0 +1 @@ +#include "2d.h" \ No newline at end of file diff --git a/SCE/SCE/2d.h b/SCE/SCE/2d.h new file mode 100644 index 0000000..6fc16fa --- /dev/null +++ b/SCE/SCE/2d.h @@ -0,0 +1,19 @@ +class Shape +{ +public: + virtual long double compute_area(long double) { return 0; }; + Shape(){}; + virtual ~Shape() {}; +}; + + +class Circle : public Shape +{ +public: + Circle() {} + ~Circle() {} + long double compute_area(long double d) override + { + return (d / 2)*(d / 2) * 3.14159265359; + } +}; diff --git a/SCE/SCE/3d.h b/SCE/SCE/3d.h index f8cce08..1bdc4fd 100644 --- a/SCE/SCE/3d.h +++ b/SCE/SCE/3d.h @@ -1,21 +1,18 @@ #include +#include +#include "2d.h" using namespace std; class Body { -private: +public: long double volume; long double mass; long double electric_resistance; vector heights; vector densities; vector resistances; -//Virtual compute functions - virtual long double compute_volume(){}; - virtual long double compute_mass(){}; - virtual long double compute_electric_resistamce(){}; -public: //Set void set_volume(long double v) { volume = v; }; void set_mass(long double m) { mass = m; }; @@ -28,17 +25,105 @@ public: virtual void data_entry(){}; virtual void compute_options(){}; + virtual long double compute_volume() { return 0; }; + virtual long double compute_mass() { return 0; }; + virtual long double compute_electric_resistamce() { return 0; }; + Body() {}; - virtual ~Body() { heights.clear(); }; + virtual ~Body() {}; }; + + class Cylinder : public Body { -private: - vector radii; - public: - + vector radii; Cylinder() {}; ~Cylinder(){}; + + void data_entry() override + { + char auswahl = 'j'; + long double teil_lange; + long double teil_durchmesser; + long double teil_dichte; + long double teil_widerstand; + + //Data entry + while (auswahl == 'j') + { + //Reseting the variables + teil_lange = 0; + teil_durchmesser = 0; + teil_dichte = 0; + teil_widerstand = 0; + + + cout << "Definieren Sie die Teilabschnitte des Zylinders" << endl; + cout << " Laenge: "; cin >> teil_lange; heights.push_back(teil_lange); + cout << " Durchmesser in mm: "; cin >> teil_durchmesser; radii.push_back(teil_durchmesser); + cout << " Dichte in g/mm^3: "; cin >> teil_dichte; densities.push_back(teil_dichte); + cout << " Spezifischer Widerstand in ohm * (mm^2) / m: "; cin >> teil_widerstand; resistances.push_back(teil_widerstand); + cout << " Gibt es weitere Teilbereiche? j/n "; cin >> auswahl; + } + cout << endl; + } + + void compute_options() override + { + //Compute + char auswahl = 'j'; + while (auswahl == 'j') + { + cout << "Was wollen Sie berechnen?" << endl; + cout << " 1 Volumen" << endl; + cout << " 2 Masse" << endl; + cout << " 3 Elektrischer Widerstand" << endl; + + cin >> auswahl; + switch (auswahl) + { + case '1': cout << "Volumen: " << compute_volume() << " mm^3" << endl; break; + case '2': cout << "Masse: " << compute_mass() << " g" << endl; break; + case '3': cout << "Widerstand: " << compute_electric_resistamce() << " ohm" << endl; break; + + default: cout << "Okay, bye" << endl; break; + } + cout << "Wollen Sie fortfahren? j/n "; cin >> auswahl; + } + } + long double compute_volume() override + { + long double Volume = 0; + Circle circle; + for (int i = 0; heights.size() != i; i++) + { + Volume = Volume + heights[i] * circle.compute_area(radii[i]); + } + set_volume(Volume); + return Volume; + } + long double compute_mass()override + { + long double mass = 0; + Circle circle; + for (int i = 0; heights.size() != i; i++) + { + mass = mass + heights[i] * circle.compute_area(radii[i]) * densities[i]; + } + set_mass(mass); + return mass; + } + long double compute_electric_resistamce() override + { + long double resistance = 0; + Circle circle; + for (int i = 0; heights.size() != i; i++) + { + resistance = resistance + heights[i] * 1000 * circle.compute_area(radii[i]) * resistances[i]; + } + set_electric_resistance(resistance); + return resistance; + } }; \ No newline at end of file diff --git a/SCE/SCE/SCE.cpp b/SCE/SCE/SCE.cpp index 846df34..1ef7e10 100644 --- a/SCE/SCE/SCE.cpp +++ b/SCE/SCE/SCE.cpp @@ -1,14 +1,8 @@ -#include "stdafx.h" #include "3d.h" #include -#include using namespace std; -void Zylinder(); -long double Kreis_flacheninhalt(long double); -long double Zylinder_volumen(vector, vector); -long double Zylinder_masse(vector, vector, vector); -long double Zylinder_widerstand(vector, vector, vector); +void cylinder(); int main() { @@ -23,98 +17,18 @@ int main() cin >> auswahl; switch(auswahl) { - case '1': Zylinder(); break; + case '1': cylinder(); break; - default: cout << "Okay, bye"; break; + default: cout << "Okay, bye" << endl; break; } } return 0; } -void Zylinder() +void cylinder() { - vector zylinder_lange; - vector zylinder_durchmesser; - vector zylinder_dichte; - vector zylinder_widerstand; - - char auswahl = 'j'; - long double teil_lange; - long double teil_durchmesser; - long double teil_dichte; - long double teil_widerstand; - - //Eingabe der Daten - while (auswahl == 'j') - { - //Reset der Variablen für den Durchlauf - teil_lange = 0; - teil_durchmesser = 0; - teil_dichte = 0; - teil_widerstand = 0; - - - cout << "Definieren Sie die Teilabschnitte des Zylinders" << endl; - cout << "Laenge: "; cin >> teil_lange; zylinder_lange.push_back(teil_lange); - cout << endl << "Durchmesser in mm: "; cin >> teil_durchmesser; zylinder_durchmesser.push_back(teil_durchmesser); - cout << endl << "Dichte in g/mm^3: "; cin >> teil_dichte; zylinder_dichte.push_back(teil_dichte); - cout << endl << "Spezifischer Widerstand in ohm * (mm^2) / m: "; cin >> teil_widerstand; zylinder_widerstand.push_back(teil_widerstand); - cout << "Gibt es weitere Teilbereiche? j/n "; cin >> auswahl; - } - auswahl = 'j'; - //Berechungen - while(auswahl=='j') - { - cout << "Was wollen Sie berechnen?" << endl; - cout << "Volumen" << "\t" << "1" << endl; - cout << "Masse" << "\t" << "2" << endl; - cout << "Elektrischer Widerstand" << "\t" << "3" << endl; - - cin >> auswahl; - switch (auswahl) - { - case '1': cout << Zylinder_volumen(zylinder_lange, zylinder_durchmesser) << " mm^3" << endl; break; - case '2': cout << Zylinder_masse(zylinder_lange, zylinder_durchmesser, zylinder_dichte) << " g" << endl; break; - case '3': cout << Zylinder_widerstand(zylinder_lange, zylinder_durchmesser, zylinder_dichte) << " ohm" << endl; break; - - default: cout << "Okay, bye"; break; - } - cout << "Wollen Sie fortfahren? j/n "; cin >> auswahl; - } + Cylinder cylinder; + cylinder.data_entry(); + cylinder.compute_options(); } - -long double Kreis_flacheninhalt(long double durchmesser) -{ - return (durchmesser / 2)*(durchmesser / 2) * 3.14159265359; -} - -long double Zylinder_volumen(vector langen, vector durchmesser) -{ - long double Volumen = 0; - for(int i = 0; langen.size() != i; i++) - { - Volumen = Volumen + langen[i] * Kreis_flacheninhalt(durchmesser[i]); - } - return Volumen; -} - -long double Zylinder_masse(vector langen, vector durchmesser, vector dichten) -{ - long double masse = 0; - for (int i = 0; langen.size() != i; i++) - { - masse = masse + langen[i] * Kreis_flacheninhalt(durchmesser[i]) * dichten[i]; - } - return masse; -} - -long double Zylinder_widerstand(vector langen, vector durchmesser, vector widerstande) -{ - long double widerstand = 0; - for (int i = 0; langen.size() != i; i++) - { - widerstand = widerstand + langen[i]* 1000 * Kreis_flacheninhalt(durchmesser[i]) * widerstande[i]; - } - return widerstand; -}