Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit cbafdca

Browse files
committed
Finished all assignments
1 parent 04e6775 commit cbafdca

File tree

5 files changed

+434
-6
lines changed

5 files changed

+434
-6
lines changed

03_Overloading/chemie.cpp

+37-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,46 @@ class Flakon
1212

1313
public:
1414
/*****************************************************
15-
Vervollständigen Sie den Code hier
15+
Vervollst�ndigen Sie den Code hier
1616
*******************************************************/
17+
// 3.4 b) constructor to set name, volumen and pH of flakon (no default constructor necessary)
18+
Flakon(std::string name, double volumen, double pH) : name(name), volumen(volumen), pH(pH) {}
1719

20+
// 3.4 c) etikett function to print the flakon state
21+
std::ostream& etikett(std::ostream& ausgabe) const {
22+
ausgabe << name << ": " << volumen << " ml, pH " << pH;
23+
return ausgabe;
24+
}
25+
26+
// 3.4 a) getter for name, volumen and pH
27+
std::string getName(void) const {return name;}
28+
double getVolumen(void) const {return volumen;}
29+
double getpH(void) const {return pH;}
30+
31+
// 3.4 f) internally overload += operator to add a flakon to another flakon
32+
Flakon& operator+=(Flakon const& f2) {
33+
this->name += " + " + f2.getName();
34+
this->volumen += f2.getVolumen();
35+
this->pH = -log10((this->volumen * pow(10.0, -this->pH) + f2.getVolumen() * pow(10.0, -f2.getpH())) / (this->volumen + f2.getVolumen()));
36+
return *this;
37+
}
1838
};
1939

40+
// 3.4 d) externally overload << operator to print flakon using etikett function
41+
std::ostream& operator<<(std::ostream& os, Flakon const& flakon) {
42+
return flakon.etikett(os);
43+
}
44+
45+
// 3.4 e) externally overload + operator to mix two flakons
46+
const Flakon operator+(Flakon const& f1, Flakon const& f2) {
47+
std::string name = f1.getName() + " + " + f2.getName();
48+
double volumen = f1.getVolumen() + f2.getVolumen();
49+
double pH = -log10((f1.getVolumen() * pow(10.0, -f1.getpH()) + f2.getVolumen() * pow(10.0, -f2.getpH())) / (f1.getVolumen() + f2.getVolumen()));
50+
return Flakon(name, volumen, pH);
51+
}
52+
2053
/*******************************************
21-
* Ändern Sie nach dieser Zeile nichts mehr.
54+
* �ndern Sie nach dieser Zeile nichts mehr.
2255
*******************************************/
2356

2457
void anzeige_mix(Flakon const& f1, Flakon const& f2)
@@ -34,8 +67,8 @@ void anzeige_mix(Flakon const& f1, Flakon const& f2)
3467
int main()
3568
{
3669
Flakon flakon1("Wasser", 600.0, 7.0);
37-
Flakon flakon2("Salzsäure", 500.0, 2.0);
38-
Flakon flakon3("Perchlorsäure", 800.0, 1.5);
70+
Flakon flakon2("Salzs�ure", 500.0, 2.0);
71+
Flakon flakon3("Perchlors�ure", 800.0, 1.5);
3972

4073
anzeige_mix(flakon1, flakon2);
4174
anzeige_mix(flakon2, flakon3);

03_Overloading/komplexeZahl.cpp

+75-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,87 @@ class KomplexeZahl {
2121
};
2222

2323
// 3.2 c) externally overload << operator to print complex number
24-
std::ostream& operator<<(std::ostream& os, const KomplexeZahl& zahl) {
24+
std::ostream& operator<<(std::ostream& os, KomplexeZahl const& zahl) {
2525
os << zahl.getReal() << ((zahl.getImaginary() > 0) ? "+" : "-") << zahl.getImaginary() << "i";
2626
return os;
2727
}
2828

2929
// 3.2 d) externally overload addition operator to add two complex numbers
3030
const KomplexeZahl operator+(KomplexeZahl z1, KomplexeZahl const& z2) {
31-
31+
return KomplexeZahl(z1.getReal() + z2.getReal(), z1.getImaginary() + z2.getImaginary());
32+
}
33+
34+
// 3.2 d) externally overload addition operator to add a complex number to a real number
35+
const KomplexeZahl operator+(double z1, KomplexeZahl const& z2) {
36+
return KomplexeZahl(z1 + z2.getReal(), z2.getImaginary());
37+
}
38+
39+
40+
// 3.2 d) externally overload += operator to add two complex numbers
41+
KomplexeZahl& operator+=(KomplexeZahl& z1, KomplexeZahl const& z2) {
42+
z1 = z1 + z2;
43+
return z1;
44+
}
45+
46+
// 3.2 d) externally overload += operator to add a real number to a complex number
47+
KomplexeZahl& operator+=(KomplexeZahl& z1, double const& z2) {
48+
z1 = z1 + KomplexeZahl(z2, 0.0);
49+
return z1;
50+
}
51+
52+
// 3.2 d) externally overload subtraction operator to subtract two complex numbers
53+
const KomplexeZahl operator-(KomplexeZahl z1, KomplexeZahl const& z2) {
54+
return KomplexeZahl(z1.getReal() - z2.getReal(), z1.getImaginary() - z2.getImaginary());
55+
}
56+
57+
// 3.2 d) externally overload -= operator to subtract two complex numbers
58+
KomplexeZahl& operator-=(KomplexeZahl& z1, KomplexeZahl const& z2) {
59+
z1 = z1 - z2;
60+
return z1;
61+
}
62+
63+
// 3.2 e) externally overload -= operator to subtract a real number from a complex number
64+
KomplexeZahl& operator-=(KomplexeZahl& z1, double const& z2) {
65+
z1 = z1 - KomplexeZahl(z2, 0.0);
66+
return z1;
67+
}
68+
69+
// 3.2 e) externally overload multiplication operator to multiply two complex numbers
70+
const KomplexeZahl operator*(KomplexeZahl z1, KomplexeZahl const& z2) {
71+
return KomplexeZahl(z1.getReal() * z2.getReal() - z1.getImaginary() * z2.getImaginary(), z1.getReal() * z2.getImaginary() + z1.getImaginary() * z2.getReal());
72+
}
73+
74+
// 3.2 e) externally overload division operator to divide two complex numbers
75+
const KomplexeZahl operator/(KomplexeZahl z1, KomplexeZahl const& z2) {
76+
return KomplexeZahl((z1.getReal() * z2.getReal() + z1.getImaginary() * z2.getImaginary()) / (z2.getReal() * z2.getReal() + z2.getImaginary() * z2.getImaginary()), (z1.getImaginary() * z2.getReal() - z1.getReal() * z2.getImaginary()) / (z2.getReal() * z2.getReal() + z2.getImaginary() * z2.getImaginary()));
77+
}
78+
79+
// 3.2 e) externally overload *= operator to multiply two complex numbers
80+
KomplexeZahl& operator*=(KomplexeZahl& z1, KomplexeZahl const& z2) {
81+
z1 = z1 * z2;
82+
return z1;
83+
}
84+
85+
// 3.2 e) externally overload /= operator to divide two complex numbers
86+
KomplexeZahl& operator/=(KomplexeZahl& z1, KomplexeZahl const& z2) {
87+
z1 = z1 / z2;
88+
return z1;
89+
}
90+
91+
// 3.2 f) externally overload * operator to multiply a complex number with a real number
92+
const KomplexeZahl operator*(double z1, KomplexeZahl const& z2) {
93+
return KomplexeZahl(z1 * z2.getReal(), z1 * z2.getImaginary());
94+
}
95+
96+
// 3.2 f) externally overload *= operator to multiply a complex number with a real number
97+
KomplexeZahl& operator*=(KomplexeZahl& z1, double const& z2) {
98+
z1 = z1 * KomplexeZahl(z2, 0.0);
99+
return z1;
100+
}
101+
102+
// 3.2 f) externally overload / operator to divide a complex number by a real number
103+
const KomplexeZahl operator/(KomplexeZahl z1, double const& z2) {
104+
return KomplexeZahl(z1.getReal() / z2, z1.getImaginary() / z2);
32105
}
33106

34107
int main(void) {
Binary file not shown.

0 commit comments

Comments
 (0)