2016-05-16 25 views
0

Ich bin fast fertig mit diesem Programm, aber ich habe Probleme mit der Anwendung der unären Negation-Funktion.Unäre Negation in C++

Ich habe ein Programm basierend auf der Berechnung komplexer Zahlen gemacht und bis jetzt funktioniert es perfekt gut, mit Ausnahme der ganzen negativen Zahlen Teil. Ich kann nicht herausfinden, wie ich meine unäre Negation in den Code einbauen kann, ohne dass sie mich warnt und mit ihrem Laufzeitfehler auf mich stürzt.

In meiner h-Datei, ich habe dies:

Complex operator-(const Complex &lhs); 

und in meiner CPP-Datei, ich versuchte, dies aus zu schaffen, was ich oben geschrieben hätte:

Complex operator-(const Complex &lhs) 
{ 
    return Complex(-lhs); 
} 

Der Code, den ich schrieb auf meinem cpp-Datei gibt mir eine Warnung C4717 Fehler, der sagt: "'operator-': rekursiv auf allen Steuerpfaden, Funktion wird Laufzeit Stack-Überlauf verursachen." Und natürlich würde das Anwenden der obigen Nummer Laufzeitfehler verursachen.

Ich bin sicher, einige von Ihnen wollen den vollständigen Code, also hier ist es: Header, CPP und die Testdatei. :)

Ich schätze die Hilfe.

Header-Datei:

#ifndef com_H 
#define com_H 
#include <iostream> 
#include <string> 

using namespace std; 


/********** CLASS **********/ 

class Complex 
{ 

    //The BFFs: Stream Insertion and Operators// 

    friend istream &operator >> (istream &lhs, Complex &rhs); 
    friend ostream &operator << (ostream &lhs, const Complex &rhs); 

private: 

    float real, img; 

public: 
      //Complex Constructor// 

    Complex(float new_real = 0.0, float new_img = 0.0); 

      //Complex Set-and-Get Functions// 

    void setComplex(float new_real, float new_img); 
    float getReal() const { return real; } 
    float getImg() const { return img; } 

      //Complex Functions// 

    Complex &operator+=(const Complex &rhs); 
    Complex &operator-=(const Complex &rhs); 
    Complex &operator*=(const Complex &rhs); 
    Complex &operator/=(const Complex &rhs); 

}; 


/********** O P E R A T O R **********/ 



Complex operator+(const Complex &lhs, const Complex &rhs); 
Complex operator-(const Complex &lhs, const Complex &rhs); 
Complex operator*(const Complex &lhs, const Complex &rhs); 
Complex operator/(const Complex &lhs, const Complex &rhs); 

//COMPARISON OPERATORS*/ 

bool operator==(const Complex &lhs, const Complex &rhs); 
bool operator!=(const Complex &lhs, const Complex &rhs); 
bool operator<(const Complex &lhs, const Complex &rhs); 
bool operator<=(const Complex &lhs, const Complex &rhs); 
bool operator>(const Complex &lhs, const Complex &rhs); 
bool operator>=(const Complex &lhs, const Complex &rhs); 

//NEGATION// 
Complex operator-(const Complex &lhs); 

#endif 

CPP-Datei:

#include <iostream> 
#include <string> 
#include <stdlib.h> 
#include <math.h> 
#include "com.h" 

using namespace std; 

// 
//***************** COMPLEX CONSTRUCTORS *****************// 
// 
Complex::Complex(float new_real, float new_img) 
{ 
    real = new_real; 
    img = new_img; 
} 

void Complex::setComplex(float new_real, float new_img) 
{ 
    real = new_real; 
    img = new_img; 
} 


// 
//***************IF REAL NUMBER IS ZERO CHECK***************// 
// 

void checkNum(char ops, float new_img) 
{ 
    char i = 'i'; 

    if (new_img == 0) 
    { 
     cout << "0"; 
    } 

    else if (new_img != 1 && ops == '-') 
    { 
     new_img *= -1; 

     cout << new_img << i; 
    } 

    else if (new_img == 1 && ops == '-') 
    { 
     cout << "-i"; 
    } 

    else if (new_img == 1) 
    { 
     cout << i; 
    } 

    else 
    { 
     cout << new_img << i; 
    } 

} 

// 
//*****************STREAM INSERTIONS*****************// 
// 

istream &operator >> (istream &lhs, Complex &rhs) 
{ 
    char ops; 
    char i = 'i'; 

    lhs >> rhs.real >> ops >> rhs.img >> i;  //lhs is another name for cin 

    if (ops == '-') 
    { 
     rhs.img *= -1; 
    } 

    return lhs; 
} 

ostream &operator << (ostream &lhs, const Complex &rhs) 
{ 
    char ops; 
    char i = 'i'; 
    float new_real = rhs.real; 
    float new_img = rhs.img; 

    if (new_img < 0) 
    { 
     ops = '-'; 
     new_img *= -1; 
    } 

    else if (new_img >= 0) 
    { 
     ops = '+'; 
    } 

    if (new_real == 0) 
    { 

     checkNum(ops, new_img); 

     return lhs << endl; 

    } 

    else if (new_img == 0) 
    { 
     return lhs << new_real; 
    } 

    else if (new_img == 1) 
    { 
     return lhs << new_real << " " << ops << " " << i; 
    } 

    else 
    { 
     return lhs << new_real << " " << ops << " " << new_img << i; 
    } 

    // lhs << rhs.real << " + " << rhs.img << 'i'; 

} 

// 
//***************COMPLEX ARITHMETIC OPERATORS***************// 
//***************** (+ | - | * |/|) ***************** // 
// 

// FORMULA for COMPLEX NUMBERS :: (a + bi) 

Complex operator+(const Complex &lhs, const Complex &rhs) 
{ 

    float a = lhs.getReal(); 
    float b = lhs.getImg(); 

    float c = rhs.getReal(); 
    float d = rhs.getImg(); 

    return Complex(a + c, b + d); 

} 

Complex operator-(const Complex &lhs, const Complex &rhs) 
{ 

    float a = lhs.getReal(); 
    float b = lhs.getImg(); 

    float c = rhs.getReal(); 
    float d = rhs.getImg(); 

    return Complex(a - c, b - d); 

} 


Complex operator*(const Complex &lhs, const Complex &rhs) 
{ 

    float a = lhs.getReal(); 
    float b = lhs.getImg(); 

    float c = rhs.getReal(); 
    float d = rhs.getImg(); 

    return Complex(((a * c) - (b * d)) , ((a * d) + (b * c))); 


} 

Complex operator/(const Complex &lhs, const Complex &rhs) 
{ 

    float a = lhs.getReal(); 
    float b = lhs.getImg(); 

    float c = rhs.getReal(); 
    float d = rhs.getImg(); 

    //Numerator 
    float myReal = (a * c) + (b * d); 
    float myImg = (b * c) - (a * d); 

    //Denominator 
    float myDenom = (pow(c, 2) + pow(d, 2)); 

    return Complex(myReal/myDenom, myImg/myDenom); 

} 

// 
//*****************COMPLEX OPERATORS EQUALS *****************// 
//**************** (+= | -= | *= | /= |) ***************** // 
// 

Complex &Complex::operator+=(const Complex &rhs) 
{ 
    real += rhs.real; 
    img += rhs.img; 

    //*this = *this + rhs; 

    return *this; 
} 

Complex &Complex::operator-=(const Complex &rhs) 
{ 
    real -= rhs.real; 
    img -= rhs.img; 

    return *this; 
} 

Complex &Complex::operator*=(const Complex &rhs) 
{ 
    real *= rhs.real; 
    img *= rhs.img; 

    return *this; 
} 

Complex &Complex::operator/=(const Complex &rhs) 
{ 
    real /= rhs.real; 
    img /= rhs.img; 

    return *this; 
} 


// 
//******************COMPLEX OPERATORS COMPARISON*****************// 
//**************** (== | != | < | <= | > | >= |)*****************// 
// 

bool operator==(const Complex &lhs, const Complex &rhs) 
{ 
    if (lhs.getReal() == rhs.getReal() && lhs.getImg() == rhs.getImg()) 
    { 
     return true; 
    } 

    return false; 
} 

bool operator!=(const Complex &lhs, const Complex &rhs) 
{ 
    return !(lhs == rhs); 
} 

bool operator<(const Complex &lhs, const Complex &rhs) 
{ 
    if (lhs.getReal() < rhs.getReal() || lhs.getReal() == rhs.getReal() && lhs.getImg() < rhs.getImg()) 
    { 
     return true; 
    } 
    return false; 
} 

bool operator<=(const Complex &lhs, const Complex &rhs) 
{ 
    return ((lhs < rhs) || (lhs == rhs)); 
} 

bool operator>(const Complex &lhs, const Complex &rhs) 
{ 
    return !(lhs <= rhs); 
} 

bool operator>=(const Complex &lhs, const Complex &rhs) 
{ 
    return !(lhs < rhs); 
} 

Complex operator-(const Complex &lhs) 
{ 
    return Complex(-lhs); 
} 

Hauptdatei:

#include <iostream> 
#include "com.h" 
#include <string> 

using namespace std; 

int main() 
{ 
    Complex userValue; 

    cout << "Enter a complex value (a + bi): "; 

    cin >> userValue; 

    cout << "Negated: " << -userValue << endl; 

    cout << "Addition: " << userValue + Complex(0.0, 0.0) << endl; 

    if (Complex(-6, 5.2f) == userValue) 
     cout << "Wow! It's equal." << endl; 

    return 0; 

} 

UPDATE 1: Wow! Ich habe es gerade wieder getestet und es stürzt tatsächlich auf jede Zahl, die ich gesetzt habe, positiv und negativ lol! Aber wenn ich die folgenden Codes aus ... von Haupt

// cout << "Negated: " << -userValue << endl; 

von Header-Datei

Complex operator-(const Complex &lhs); 

von CPP-Datei Kommentar

Complex operator-(const Complex &lhs) 
{ 
    return Complex(-lhs); 
} 

... es funktioniert immer noch (ohne die negative Zahlen natürlich.)

+0

Sind Sie bewusst, 'std :: complex'? –

Antwort

4

Wie der Fehler sagt, rufen Sie Ihre ov erloeste Funktion rekursiv. Stattdessen möchten Sie den Konstruktor mit den richtigen Argumenten aufrufen:

Complex operator-(const Complex &lhs) 
{ 
    return Complex(-lhs.real, -lhs.imag); 
} 
+0

Wow! Das war schnell! Vielen Dank! Ich schätze es! : D – miiworld2