4 Grundrechenarten leserlicher
-
Hallo Leute,
brauche dringend Hilfe bei folgendem Code:
Der markierte Code-Schnippsel soll ungefähr so aussehen, habe ich mir gedacht.
Die Frage lautet:
Ein Programm fordert zwei Zahlen an und verknüpft diese nach den Regeln der 4 Grundrechenarten miteinander.
Folgendes Struktogramm liegt mir zugrunde:Class: Zahlen
Eigenschaften: zahl1, zahl2, ergebnis
Konstruktor initialisiert: (z = 1.0, n = 3.0, e = 1.0)
Variable "arith" vom Typ "Zahlen" definieren
Konstruktor: zahl1 = z, zahl2 = n, ergebnis = eMethode: plus
minus
mal
durch
eingabeIn der Methode durch() soll eine Division durch Null abgefangen werden,
indem dem Nenner dem Nenner der Wert 1 gegeben wird.
Mittels einer Textausgabe:"Zahl 2 darf nicht Null sein"
soll auf eine fehlerhafte Eingabe des Nenners hingewiesen werden.Alle Methoden mit Ausnahme der oben beschriebenen sollen als Inline-Methoden angelegt werden.
Die Textausgabe soll so aussehen:
Zahl 1 = 1
Zahl 2 = 21 + 3 = 4
1 - 3 = -2
1 * 3 = 3
1 / 4 = 0.333333
Bitte Taste drueckenC/C++ Code: #include <stdio.h> #include <conio.h> #include <iostream.h> class Zahlen { private: float zahl1; float zahl2; float ergebnis; public : void plus(void); //INLINE FUNKTIONEN void minus(void); void mal(void); void durch(void); void eingabe(void); void print(void); Zahlen(float zahl1 = 1.0, float zahl2 = 3.0, float ergebnis = 1.0) /*void minus(void) { cout << zahl1 << " - " << zahl2 << " = " << ergebnis << endl; ergebnis = zahl1 - zahl2; }*/ { zahl1 = z; zahl2 = n; ergebnis = e; }; /* ****************Haupfunktion****************************** */ void main(void) { Rechnen arith(1.0, 3.0); arith.print getch(); }
-
[cpp] #include <stdio.h> #include <conio.h> #include <iostream.h> class Zahlen { private: float zahl1; float zahl2; float ergebnis; public : void plus(void) { ergebnis = zahl1 + zahl2; cout << zahl1 << " + " << zahl2 << " = " << ergebnis << endl; }; void minus(void) { ergebnis = zahl1 - zahl2; cout << zahl1 << " - " << zahl2 << " = " << ergebnis << endl; }; void mal(void); { ergebnis = zahl1 * zahl2; cout << zahl1 << " * " << zahl2 << " = " << ergebnis << endl; }; void durch(void); { ergebnis = zahl1 / ( (zahl2==0)?1:zahl2 ); if (zahl2==0) cout << "zahl2 darf nicht 0 sein" << endl; else cout << zahl1 << " / " << zahl2 << " = " << ergebnis << endl; }; void eingabe(void); void print(void) { cout << "Zahl1 = " << zahl1 << endl; cout << "Zahl2 = " << zahl2 << endl; }; Zahlen(float z = 1.0, float n = 3.0,float e = 1.0) { zahl1 = z; zahl2 = n; ergebnis = e; }; /* ****************Haupfunktion****************************** */ void main(void) // void falsch int main(void) ist richtig { Rechnen arith(1.0, 3.0); // Rechnen?? --> Zahlen arith(1.0, 3.0); arith.print // ?? arith.print(); getch(); } [/cpp]