Frage

Ich habe einen Auftrag in C ++ und ich habe Probleme begonnen. Ziel ist es, „entwirft eine Klasse, die die folgenden überladenen Operatoren für komplexe Zahlen verwendet: >> << + - * /“

Meine Frage ist nicht über die Syntax dieses, sondern mehr über die Logik. Ich könnte etwas Hilfe Gehirn Erstürmung verwenden.

Eingabe-Beispiel:
2.5 -2.2
1.0 1.0

OutPut Probe:
A = (2,5) + (-2,2) i
B = (1,0) + (1,0) i

A + B = (3,5) + (-1,2) i
A - B = ..............
A * B = ..............
A / B = ..............

Wie starte ich das? Die Klasse „Complex“ Überlastungen diese Operatoren, so auch das bedeutet, dass ich nur diese Operatoren in der Klasse verwenden kann (das heißt innerhalb der öffentlichen Funktionen)? Wenn ja würde ich will, dass es auf diese Weise zu tun? Oder mag ich es in meinem Client / Treiber-Code tun?

Zweitens wird das Hinzufügen ich nur auf den zweiten Wert jeder Zeile? Das scheint zu einfach. Jede Richtung wäre sehr geschätzt. (Nur für das Protokoll, ich bin nicht für jemanden mich auf meine Hausaufgaben für mich zu tun ... könnte nur eine Eingabe verwenden)

War es hilfreich?

Lösung

Es scheint mir, dass der Punkt ist Klasse Betrieb Überlastung zu demonstrieren, so glaube ich, die Idee ist für Sie eine Klasse Complex zu machen, welche Informationen über die tatsächliche Anzahl und die imaginäre Zahl hält (das i bedeuten, es ist imaginär). Behandeln Sie verschiedene Operationen zwischen komplexen Zahlen in Bediener überschreibt Sie selbst tun.

Sobald Sie haben das, und Sie sehen, dass es funktioniert (eine statische Testmethode machen, die verschiedenen Operationen tut und druckt die Ergebnisse auf dem Bildschirm), dann Sorgen um diese Klasse zu Arbeit mit Eingabe verwendet, da die Eingabe-Parsing wird eine andere Aufgabe in von sich selbst. Manchmal ist es nur einfacher zu dividieren Problemen in kleinere Probleme, als zu versuchen, beides zu tun zugleich.

Ich hoffe, das hilft. Viel Glück!

Andere Tipps

Sie müssen eine Klasse entwerfen genannten Komplex, der mindestens enthält:

  • ein Konstruktor so dass Sie ein komplexes Objekt aus realen und imaginären Komponentenwerten konstruieren z.B. Complex (1, 5)

  • überschreibt den Operator +, so dass Sie zwei Komplexe Objekte hinzufügen können, ein neues komplexes Objekt zurückkehr z.B. Komplex (1, 5) + Complex (3, 7) ist komplex (4, 12)

  • in ähnlicher Weise für andere Betreiber

Aber zuerst müssen Sie die grundlegende Mathematik hinter komplexen Zahlen verstehen, so dass Sie die Betreiber Überlastung Methoden schreiben kann.

Sie sind wie Wertepaare:

A = N1 + I1i
B = N2 + I2i


A + B = (N1 + I1i) + (N2 + I2i)
      = N1 + I1i + N2 + I2i
      = (N1 + N2) + (I1i + I2i)
      = (N1 + N2) + (I1 + I2)i
A - B = (N1 + I1i) - (N2 + I2i)
      = N1 + I1i - N2 - I2i
      = (N1 - N2) + (I1i - I2i)
      = (N1 - N2) + (I1 - I2)i

// N1, N2, I1, I2 are all just normal numbers.
// You can multiply them like normal. You just have to keep track of the `i`
// Also not that i = sqrt(-1)
// Therefore  i * i = sqrt(-1) * sqrt(-1)
//                  = sqrt(-1)^2
//                  = -1
A * B = (N1 + I1i) * (N2 + I2i)
      = (N1 * N2) + (N1 * I2i) + (I1i * N2) + (I1i * I2i)
      = (N1 * N2) + (N1 * I2)i + (N2 * I1)i + (i * i * I1 * I2)
      = (N1 * N2) + i((N1 * I2) + (N2 * I1)) + (-1 * I1 * I2)

      // Simplest form
      = ((N1 * N2) - (I1 * I2)) + ((N1 * I2) + (N2 * I1))i


A / B = Repeat as above.

Es gibt ein paar Dinge, die Sie tun müssen, um diese Aufgabe zu erfüllen:

Definieren einer Klasse (z.B. Komplexe), die die Daten für den Real- und Imaginärteil einer komplexen Zahl halten kann.

Überlast der jeweiligen Betreiber (z.):

class Complex
{
public:
    // other declarations here
    Complex operator+ (const Complex& rhs) const;
    // other stuff here
};

Implementieren Sie die jeweiligen Betreiber tatsächlich die mathematische Operation durchführen (z.):

Complex Complex::operator+ (const Complex& rhs) const
{
    Complex result = *this;
    result.Real += rhs.Real;
    result.Imaginary += rhs.Imaginary;
    return result;
}

Hope Sie bei den Hausaufgaben jetzt fertig sind :) Hier meine Lösung ist, wenn jemand noch Hilfe braucht.

#include <string>
#include <sstream>
#include <iostream>

using namespace std;


class Complex {
    float real_, imaginary_;
  public:
    Complex (float, float);
    Complex operator= (const Complex& rhs);
    Complex operator+ (const Complex& rhs) const;
    Complex operator- (const Complex& rhs) const;
    Complex operator* (const Complex& rhs) const;
    string toString() const;
};

Complex::Complex (float r, float i){
  real_ = r;
  imaginary_ = i;
}

Complex Complex::operator= (const Complex& rhs){
    real_ = rhs.real_;
    imaginary_ = rhs.imaginary_;
    return *this;
}

Complex Complex::operator+ (const Complex& rhs) const{
    Complex result = *this;
    result.real_ += rhs.real_;
    result.imaginary_ += rhs.imaginary_;
    return result;
}

Complex Complex::operator- (const Complex& rhs) const{
    Complex result = *this;
    result.real_ -= rhs.real_;
    result.imaginary_ -= rhs.imaginary_;
    return result;
}

Complex Complex::operator* (const Complex& rhs) const{
    Complex result = *this; // this-> == *this == (*this)
    result.real_ = real_ * rhs.real_ - imaginary_ * rhs.imaginary_;
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    result.imaginary_ = (real_ * rhs.imaginary_) + (rhs.real_ * imaginary_);
    //cout << result.real_ << "R " << result.imaginary_ << "I "<< "|" << rhs.real_ << "R " << rhs.imaginary_ << "I\n";
    return result;
}

string Complex::toString() const {
  stringstream ss;
  if (imaginary_ > 0){
    ss << real_ << " + " << imaginary_ << "i";
  }
  else {
    ss << real_ << " " << imaginary_ << "i";
  }
  return ss.str();
}

int main () {
  Complex a(5, 6);
  Complex b(1, 4);

  Complex sum = a + b;
  Complex dif = a - b;
  Complex pro = a * b;

  cout << "sum: " << sum.toString() << "\n";
  cout << "difference: " << dif.toString() << "\n";
  cout << "product: " << pro.toString() << "\n";

  return 0;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top