C++ :: Aufgabe #287 :: Lösung #2

2 Lösungen Lösungen öffentlich
#287

Mathematische Vektoroperationen

Anfänger - C++ von thunderbird - 28.04.2020 um 21:51 Uhr
Erstellen Sie ein Programm/ eine Funktion, mit der Vektorberechnungen durchgeführt werden können.
"Vektor" soll eine eigens erstellte Klasse sein, die aus 3 Fließkommazahlen bestehen.
Zudem beinhaltet die Klasse "Vektor" eine Methode zur Skalarmultiplikation, Addition, Subtraktion, Division und um das Kreuzprodukt zu berrechnen.
Erstellen Sie zur Darstellung eine Ausgabemethode.

Hinweis:
- Anstatt neue Methoden zu erstellen, können (sofern möglich) bereits vorhandene überladen werden.
- Mathematische Rechenregeln unter: https://de.wikipedia.org/wiki/Vektor
#2
vote_ok
von dewe (460 Punkte) - 01.05.2020 um 15:41 Uhr
Quellcode ausblenden C-Code
#ifndef VEKTOR_H
#define VEKTOR_H

using namespace std;

class Vektor
{
    public:                                     
        Vektor(float a = 0, float b = 0, float c = 0);         

    void print_Vektor();
    float rAvalue();
    float rBvalue();
    float rCvalue();

    float skalar(Vektor b);
    void multiplikation(int n);
    void add(Vektor b);
    void sub(Vektor b);
    void product(Vektor a, Vektor b);

    private:
        float A;
        float B;
        float C;
};

#endif // VEKTOR_H


Quellcode ausblenden C-Code
#include <iostream>
#include "vektor.h"

Vektor::Vektor(float a, float b, float c)
{
    A = a;
    B = b;
    C = c;
}

void Vektor::print_Vektor()
{
    cout << A << endl;
    cout << B << endl;
    cout << C << endl;
}

float Vektor::rAvalue()
{
    return A;
}

float Vektor::rBvalue()
{
    return B;
}

float Vektor::rCvalue()
{
    return C;
}

float Vektor::skalar(Vektor b)
{
    return (A*b.rAvalue()+B*b.rBvalue()+C*b.rCvalue());
}

void Vektor::multiplikation(int a)
{
    A = A*a;
    B = B*a;
    C = C*a;
}

void Vektor::add(Vektor b)
{
    A += b.rAvalue();
    B += b.rBvalue();
    C += b.rCvalue();
}

void Vektor::sub(Vektor b)
{
    A -= b.rAvalue();
    B -= b.rBvalue();
    C -= b.rCvalue();
}

void Vektor::product(Vektor a, Vektor b)
{
    A = a.rBvalue()*b.rCvalue() - a.rCvalue()*b.rBvalue();
    B = a.rCvalue()*b.rAvalue() - a.rAvalue()*b.rCvalue();
    C = a.rAvalue()*b.rBvalue() - a.rBvalue()*b.rAvalue();
}


Quellcode ausblenden C-Code
#include <iostream>
#include "vektor.h"

using namespace std;

int main()
{
    Vektor a(1, 2, 1);
    Vektor b(3, 4, 5);
    Vektor c(0, 0, 0);

    cout<< "Skalarprodukt von Vektor a mit Vektor b" << endl;
    cout <<a.skalar(b) << endl;

    cout<< "Vektor a wird mit 4 multipliziert" << endl;
    a.multiplikation(4);
    a.print_Vektor();

    cout<< "Zum Vektor a wird der Vektor b addiert" << endl;
    a.add(b);
    a.print_Vektor();

    cout<< "Zum Vektor a wird der Vektor b wieder subtrahiert" << endl;
    a.sub(b);
    a.print_Vektor();


    cout<< "Das Kreuzprodukt von Vektor a und b" << endl;
    c.product(a, b);
    c.print_Vektor();

    return 0;
}

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben