Hej! Chcę rozwiązać zadanie o treści: Zmodyfikuj plik nagłówkowy klasy Vector i plik implementacji jej metod tak, aby obiekty nie przechowywały w składowych wartości długości i odchylenia wektora. Zamiast tego obiekt powinien obliczać i zwracać te wartości w wyniku wywołania metod magval() (łlugość wektora) i angval() (kat wektora). Publiczny interfejs klasy powinien pozostać bez zmian(co do metod publicznych i ich argumentów), sekcje prywatną należy jednak uzupełnić o kilka metod prywatnych i zubożyć o wyeliminowane składowe.
PLIK NAGŁÓWKOWY:
#pragma once
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
//#include <fstream>
namespace VECTOR
{
class Vector
{
public :
enum Mode{RECT, POL};
private:
//static const int RECT = 0;
//static const int POL = 1;
double x;
double y;
double mag;
double ang;
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();
Vector(double n1, double n2, Mode form = RECT);
void reset(double n1, double n2, Mode form = RECT);
~Vector();
double xval() const { return x; }
double yval() const { return y; }
double magval() const { return mag; }
double angval() const { return ang; }
void polar_mode();
void rect_mode();
Vector operator+(const Vector &b) const;
Vector operator-(const Vector &b) const;
Vector operator-() const;
Vector operator*(double n) const;
friend Vector operator*(double n, const Vector &a);
friend std::ostream & operator<<(std::ostream &os, const Vector &v);
};
}
#endif
PLIK IMPLEMENTACJI:
#include "Vector.h"
#include <cmath>
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
namespace VECTOR
{
const double Rad_to_deg = 45.0 / atan(1.0);
void Vector::set_mag()
{
mag = sqrt(x*x + y*y);
}
void Vector::set_ang()
{
ang = atan2(y, x);
}
void Vector::set_x()
{
x = mag*cos(ang);
}
void Vector::set_y()
{
y = mag*sin(ang);
}
Vector::Vector()
{
x = y = mag = ang = 0.0;
mode = RECT;
}
Vector::Vector(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Niepoprawna wartosc trzeciego argumentu!\n";
cout << "Zeruje wektor!\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void Vector::reset(double n1, double n2, Mode form)
{
mode = form;
if (form == RECT)
{
x = n1;
y = n2;
set_mag();
set_ang();
}
else if (form == POL)
{
mag = n1;
ang = n2 / Rad_to_deg;
set_x();
set_y();
}
else
{
cout << "Niepoprawna wartosc trzeciego argumentu!\n";
cout << "Zeruje wektor!\n";
x = y = mag = ang = 0.0;
mode = RECT;
}
}
void Vector::polar_mode()
{
mode = POL;
}
void Vector::rect_mode()
{
mode = RECT;
}
Vector Vector::operator+(const Vector &b) const
{
return Vector(x + b.x, y + b.y);
}
Vector Vector::operator-(const Vector &b) const
{
return Vector(x - b.x, y - b.y);
}
Vector Vector::operator-() const
{
return Vector(-x, -y);
}
Vector Vector:: operator*(double n) const
{
return Vector(n*x, n*y);
}
Vector operator*(double n, const Vector &a)
{
return a*n;
}
std::ostream & operator<<(std::ostream &os, const Vector &v)
{
if (v.mode == Vector::RECT)
os << "(x, y) = (" << v.x << " ," << v.y << ")" << std::endl;
else if (v.mode == Vector::POL)
{
os << "(m, a) = (" << v.mag << ", ";
os << v.ang *Rad_to_deg << ")";
}
else
os << "Niepoprawny tryb reprezentacji obiektu wektora\n";
return os;
}
/*std::fstream & operator<<(std::fstream &os, const Vector &v)
{
if (v.mode == Vector::RECT)
os << "(x, y) = (" << v.x << " ," << v.y << ")" << std::endl;
else if (v.mode == Vector::POL)
{
os << "(m, a) = (" << v.mag << ", ";
os << v.ang *Rad_to_deg << ")";
}
else
os << "Niepoprawny tryb reprezentacji obiektu wektora\n";
return os;
} */
Vector::~Vector()
{
}
}
PROGRAM MAIN:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "Vector.h"
int main()
{
using namespace std;
using namespace VECTOR;
srand(time(0));
Vector result(0.0, 0.0);
Vector step;
double direction;
double step_lenght;
double distance;
unsigned long steps = 0;
ofstream fout;
int i;
fout.open("bladzenie1.txt");
if (!fout.is_open())
{
cout << "Nie mozna otworzyc pliku!\n";
exit(EXIT_FAILURE);
}
cout << "Podaj dystans do przejscia: ";
while (cin >> distance)
{
i = 0;
cout << "Podaj dlugosc kroku: ";
if (!(cin >> step_lenght)) break;
cout << endl;
fout << "Dystans do przejscia: " << distance << ", dlugosc kroku: " << step_lenght << endl;
while (result.magval() < distance)
{
direction = rand() % 360;
step.reset(step_lenght, direction, Vector::POL);
result = result + step;
step.rect_mode();
fout << i << ": ";
fout << step << endl;
steps++;
i++;
}
fout << "Po ";
fout << steps << " krokach delikwent osiagnal polozenie: \n" << result << endl;
result.polar_mode();
fout << "czyli\n";
fout << result << endl;
fout << "Srednia dlugosc kroku pozornego: " << result.magval() / steps << endl;
steps = 0;
result.reset(0.0, 0.0);
cout << "Podaj kolejny dystans lub q aby zakonczyc\n";
}
cout << "Koniec!\n";
fout.close();
return 0;
}
Mój problem polega na tym, że nie wiem o jakie metody prywatne mam wzbogacić ten program i skoro zlikwiduję zmienne ang i mag to w jaki sposób w pliku implementacji w metodzie reset zastosować współrzędne kątowe (przydają się w pliku main w pętli while)?
Z góry dziękuję za pomoc :)