2016-07-25 35 views
1

Ich habe eine Frage zu meiner FraktionType-Klasse. Ich kodiere es für eine Zuweisung und die Zuweisung erfordert, dass ich die Grundrechenarten (+, -, /, *) die logischen Operatoren (==, <,>, = <, =>) und die Systemoperatoren (< < , >>) Bisher habe ich Systemoperatoren, sowie die Multiplikations- und Divisionsoperatoren. Mein Problem ist aufgetreten, als ich die Operatoren + und - getestet habe. Meine Eingabe ist hart codiert als 2/4 und 1/8, aber meine Ausgabe ist 3/8. Das ist nicht richtig.Wie behebe ich die Ausgabe des überladenen + Operators meiner Fraktionsklasse?

Mein Code von fractionType.cpp

#include <iostream> 
#include "fractionType.h" 

fractionType::fractionType(){ 
} 
fractionType::fractionType(int a, int b) { 
    numerator = a; 
    denominator = b; 
} 
fractionType fractionType::operator+(fractionType fraction) { 
    fractionType anotherFraction; 
    if (denominator = fraction.denominator) { 

     anotherFraction.numerator = numerator + fraction.numerator; 

     anotherFraction.denominator = denominator; 
    } 
if (denominator != fraction.denominator) { 
    anotherFraction.numerator = ((numerator) * (fraction.denominator)) +   ((fraction.numerator) * (denominator)); 

    anotherFraction.denominator = denominator * fraction.denominator; 
} 
return anotherFraction; 
} 
fractionType fractionType::operator-(fractionType fraction) { 

fractionType anotherFraction; 

if (denominator = fraction.denominator) { 

    anotherFraction.numerator = numerator - fraction.numerator; 

    anotherFraction.denominator = denominator; 
} 
else { 
    anotherFraction.numerator = (numerator * fraction.denominator) - (fraction.numerator * fraction.denominator); 

    anotherFraction.denominator = denominator * fraction.denominator; 
} 

return anotherFraction; 


} 

Mein Code von fractionType.h

#include <iostream> 

class fractionType { 
public: 
    fractionType(int a, int b); 
    fractionType(); 
    fractionType operator*(fractionType fraction); 
    fractionType operator/(fractionType fraction); 
    fractionType operator+(fractionType fraction); 
    fractionType operator-(fractionType fraction); 
    friend std::ostream& operator<<(std::ostream& out, const fractionType &fraction) { 
    out << fraction.numerator << '/' << fraction.denominator; 
    return out; 
} 
friend std::istream& operator>> (std::istream& in, fractionType &fraction) { 
    char c; 
    in >> fraction.numerator >> c >> fraction.denominator; 
    return in; 
} 
private: 
int numerator, denominator; 
}; 
#endif FRACTIONTYPE_H 

und meine source.cpp

#include <iostream> 
#include "fractionType.h" 

int main() { 
    fractionType aFraction(2, 4); 
    fractionType bFraction(1, 8); 
    fractionType cFraction; 
    cFraction = aFraction + bFraction; 
    std::cout << cFraction << std::endl; 
    system("PAUSE"); 
} 

ich meinen Bediener * ausgeschnitten und Betreiber /, weil sie richtig funktionieren. Ich habe keine Ahnung, was ich falsch gemacht habe, ich habe immer und immer wieder über die Mathematik gegangen, bevor ich diese Frage gestellt habe. Liegt es daran, dass ich keinen Zeiger benutzt habe? eine Konstante? Oder ist es etwas anderes? Vielen Dank im Voraus für die Hilfe!

+0

Verwandeln Sie Ihre Warnstufe und Sie sollten mindestens eine relevante Warnung von der Get Compiler. Verwenden Sie 'const' entsprechend (für Dinge, die Sie nicht ändern wollen) und das wird ein Fehler sein. Derzeit können Sie keine zwei konstanten Brüche hinzufügen, was keinen Sinn ergibt. – chris

+0

Können Sie das für mich durchgehen? Ich möchte ein besserer Programmierer werden, und ich verstehe nicht genau, wo ich das mache. –

+0

@JakeTheSnakeRoberts das ist, was http://codereview.stackexchange.com für –

Antwort

1

Diese Zeile ist falsch:

if (denominator = fraction.denominator) { 

Es hat

if (denominator == fraction.denominator) { 

werden, wenn Warnungen aktiviert sind, Klirren und gcc Ihnen sagen, dass, zum Beispiel:

main.cpp:32:19: warning: using the result of an assignment as a condition without parentheses [-Wparentheses] 

    if (denominator = fraction.denominator) { 

     ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~ 

live example

Wenn Sie die oben genannten Fehler zu korrigieren, der Code funktioniert:

Ausgang

20/32 

live example

+0

Ja, das gab mir die richtige Ausgabe. Vielen Dank, du bist ein Held. –