Rational Struct
-
Hallo Zusammen,
ich habe mir einen struct gebastelt (klassen kenne ich noch nicht, dass kommt später noch), der den Zahlenkörper darstellen soll, also die rationalen Zahlen.
Dazu habe ich auch einige Operatoren implementiert. der code sieht wie folgt aus:
#include<iostream> // Define the new type Rational struct Rational{ int n; int d; // INV: d!=0 }; // Grundoperatoren // PRE: Two value of type Rational // POST: return value is a Rational representing the summation Rational operator+ (Rational x, Rational y){ Rational result; result.n=x.n*y.d+y.n*x.d; result.d=x.d*y.d; return result; } // PRE: Two value of type Rational // POST: return value is a Rational representing the subtraction Rational operator- (Rational x, Rational y){ Rational result; result.n=x.n*y.d-y.n*x.d; result.d=x.d*y.d; return result; } // PRE: Two value of type Rational // POST: return value is a Rational representing the product Rational operator* (Rational x, Rational y){ Rational result; result.n=x.n*y.n; result.d=x.d*y.d; return result; } // PRE: Two value of type Rational, y!=0 // POST: return value is a Rational representing the quotient Rational operator/ (Rational x, Rational y){ Rational result; result.n=x.n*y.d; result.d=x.d*y.n; return result; } // Booloperatoren // PRE: Two value of type Rational // POST: return value is true if a==b bool operator== (Rational x, Rational y){ return x.n*y.d==y.n*x.d ; } // PRE: Two value of type Rational // POST: return value is true if a==b bool operator!= (Rational x, Rational y){ return !(x.n*y.d==y.n*x.d) ; }; //Rational operator< (Rational x, Rational y){ // return !(x.n*y.d==y.n*x.d) ; //} // PRE: A Rational // POST: Rational has been written on outstream std::ostream& operator<< (std::ostream& o, Rational x){ return o << x.n << "/" << x.d; }; std::istream& operator>> (std::istream& i, Rational x){ char c; // For the / in input return i >> x.n >> c >> x.d; }; int main () { // Input std::cout << "Your first rational number r:\n"; Rational r; std::cin >> r; std::cout << "Your second rational number s:\n"; Rational s; std::cin >> s; // Computation and Output std::cout << " The sum of 2 Rationals is " << r+s << ".\n"; std::cout << " The subtraction of 2 Rationals is " << r-s << ".\n"; std::cout << " The product of 2 Rationals is " << r*s << ".\n"; std::cout << " The division of 2 Rationals is " << r/s << ".\n"; std::cout << " The equality of 2 Rationals is " <<(r==s) << ".\n"; std::cout << " The inequalitz of 2 Rationals is " <<(r!=s) << ".\n"; return 0; }Eigentlich glaubte ich, dass dies klappen sollte, wenn ich aber zum bsp.
und eingebe, bekomme ich als output:
The sum of 2 Rationals is -760672256/0.
The subtraction of 2 Rationals is -760672256/0.
The product of 2 Rationals is 470474752/0.
The division of 2 Rationals is -760672256/0.
The equality of 2 Rationals is 0.
The inequalitz of 2 Rationals is 1.da läuft doch irgendwas schief?
Merci für die Hilfe,
euer Hugo
-
Du übergibst jeweils Kopien der Objekte, anstatt einer Referenz auf diese.
Bei den Rechenoperatoren verschwendest du dadurch lediglich CPU-Zyklen, aber beistd::istream& operator>> (std::istream& i, Rational x){ char c; // For the / in input return i >> x.n >> c >> x.d; }werden die Werte in die Kopie geschrieben, welche gleich wieder zerstört wird.
Also "Rational x" jeweils durch "Rational& x" bzw. "const Rational& x" ersetzen und es sollte gehen.
-
Bingo, habs geändert und siehe da, es läuft merci
Hugo B
-
Und bitte beachten, dass struct und class in C++ identisch sind (außer default-access: struct => public; class => private)