Skip to content

Commit

Permalink
Merge pull request #50 from studas/lista06/ex03
Browse files Browse the repository at this point in the history
Lista06/ex03
  • Loading branch information
CarlosCraveiro authored Oct 31, 2022
2 parents 44b9964 + 4549818 commit 5952e66
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
Binary file added lista06/ex02/src/a.out
Binary file not shown.
31 changes: 31 additions & 0 deletions lista06/ex02/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include "numeros_complexos.hpp"

int main(int argc, char *argv[]) {

// Cria numeros complexos
NumComplexo a = NumComplexo(1, 2);
NumComplexo b = NumComplexo(0, 8);
NumComplexo c = NumComplexo(42, 0);

// Exemplos da biblioteca funcionando
NumComplexo result = b.soma(a);
std::cout << "(" << a.para_texto() << ") + (" << b.para_texto() << ") = " << result.para_texto() << std::endl;

result = a.soma(c);
std::cout << "(" << a.para_texto() << ") + (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

result = a.subtracao(b);
std::cout << "(" << a.para_texto() << ") - (" << b.para_texto() << ") = " << result.para_texto() << std::endl;

result = a.subtracao(c);
std::cout << "(" << a.para_texto() << ") - (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

result = a.multiplicacao(b);
std::cout << "(" << a.para_texto() << ") * (" << b.para_texto() << ") = " << result.para_texto() << std::endl;

result = b.multiplicacao(c);
std::cout << "(" << b.para_texto() << ") * (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

return 0;
}
31 changes: 31 additions & 0 deletions lista06/ex02/src/numeros_complexos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "numeros_complexos.hpp"

auto NumComplexo::soma(NumComplexo num) -> NumComplexo {
return NumComplexo( _sigma + num._sigma, _omega + num._omega );
}

auto NumComplexo::subtracao(NumComplexo num) -> NumComplexo {
return NumComplexo( _sigma - num._sigma, _omega - num._omega );
}

auto NumComplexo::multiplicacao(NumComplexo num) -> NumComplexo {
return NumComplexo( _sigma * num._sigma - _omega * num._omega, _omega * num._sigma + _sigma * num._omega );
}

auto NumComplexo::modulo() -> double {
double expression = _sigma * _sigma + _omega * _omega;
return sqrt(expression);
}

auto NumComplexo::para_texto() -> std::string {
auto str_buffer = std::to_string(_sigma);
str_buffer += ((_omega >= 0)? " + " : " - ");
str_buffer += std::to_string(abs(_omega));
str_buffer += "j";
return str_buffer;
}

NumComplexo::NumComplexo(int module, double theta_deg) {
_sigma = module * cos ( theta_deg * PI / 180.0 );
_omega = module * sin ( theta_deg * PI / 180.0 );
}
26 changes: 26 additions & 0 deletions lista06/ex02/src/numeros_complexos.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef NUMEROS_COMPLEXOS_H_
#define NUMEROS_COMPLEXOS_H_

#define PI 3.14159265

#include <iostream>
#include <string>
#include <cmath>

class NumComplexo {
protected:
int _sigma;
int _omega;

public:
auto soma(NumComplexo num) -> NumComplexo;
auto subtracao(NumComplexo num) -> NumComplexo;
auto multiplicacao(NumComplexo num) -> NumComplexo;
auto modulo() -> double;
auto para_texto()-> std::string;

NumComplexo(int sigma, int omega) : _sigma { sigma }, _omega { omega } {};
NumComplexo(int module, double theta_deg);
};

#endif // NUMEROS_COMPLEXOS_H_
31 changes: 31 additions & 0 deletions lista06/ex03/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include "numeros_complexos.hpp"

int main(int argc, char *argv[]) {

// Cria numeros complexos
NumComplexo a = NumComplexo(1, 2);
NumComplexo b = NumComplexo(0, 8);
NumComplexo c = NumComplexo(42, 0);

// Exemplos da biblioteca funcionando
NumComplexo result = b + a;
std::cout << "(" << a << ") + (" << b << ") = " << result << std::endl;

result = a + c;
std::cout << "(" << a.para_texto() << ") + (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

result = a - b;
std::cout << "(" << a.para_texto() << ") - (" << b.para_texto() << ") = " << result.para_texto() << std::endl;

result = a - c;
std::cout << "(" << a.para_texto() << ") - (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

result = a * b;
std::cout << "(" << a.para_texto() << ") * (" << b.para_texto() << ") = " << result.para_texto() << std::endl;

result = b * c;
std::cout << "(" << b.para_texto() << ") * (" << c.para_texto() << ") = " << result.para_texto() << std::endl;

return 0;
}
51 changes: 51 additions & 0 deletions lista06/ex03/src/numeros_complexos.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "numeros_complexos.hpp"

auto NumComplexo::soma(NumComplexo const num) const -> NumComplexo {
return NumComplexo( _sigma + num._sigma, _omega + num._omega );
}

auto NumComplexo::operator+(NumComplexo const other) const -> NumComplexo {
return this->soma(other);
}

auto NumComplexo::subtracao(NumComplexo const num) const -> NumComplexo {
return NumComplexo( _sigma - num._sigma, _omega - num._omega );
}

auto NumComplexo::operator-(NumComplexo const other) const -> NumComplexo {
return this->subtracao(other);
}

auto NumComplexo::multiplicacao(NumComplexo const num) const -> NumComplexo {
return NumComplexo( _sigma * num._sigma - _omega * num._omega, _omega * num._sigma + _sigma * num._omega );
}

auto NumComplexo::operator*(NumComplexo const other) const -> NumComplexo {
return this->multiplicacao(other);
}

auto NumComplexo::modulo() const -> double {
double expression = _sigma * _sigma + _omega * _omega;
return sqrt(expression);
}

auto NumComplexo::operator~() const -> double {
return this->modulo();
}

auto NumComplexo::para_texto() const -> std::string {
auto str_buffer = std::to_string(_sigma);
str_buffer += ((_omega >= 0)? " + " : " - ");
str_buffer += std::to_string(abs(_omega));
str_buffer += "j";
return str_buffer;
}

NumComplexo::NumComplexo(int module, double theta_deg) {
_sigma = module * cos ( theta_deg * PI / 180.0 );
_omega = module * sin ( theta_deg * PI / 180.0 );
}

std::ostream &operator<<(std::ostream& os, const NumComplexo& num) {
return os << num.para_texto();
}
32 changes: 32 additions & 0 deletions lista06/ex03/src/numeros_complexos.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef NUMEROS_COMPLEXOS_H_
#define NUMEROS_COMPLEXOS_H_

#define PI 3.14159265

#include <iostream>
#include <string>
#include <cmath>

class NumComplexo {
protected:
int _sigma;
int _omega;

public:
auto soma(NumComplexo const num) const -> NumComplexo;
auto operator+(NumComplexo const other) const -> NumComplexo;
auto subtracao(NumComplexo const num) const -> NumComplexo;
auto operator-(NumComplexo const other) const -> NumComplexo;
auto multiplicacao(NumComplexo const num) const -> NumComplexo;
auto operator*(NumComplexo const other) const -> NumComplexo;

auto modulo() const -> double;
auto operator~() const -> double;
auto para_texto() const -> std::string;

NumComplexo(int sigma, int omega) : _sigma { sigma }, _omega { omega } {};
NumComplexo(int module, double theta_deg);
};

std::ostream &operator<<(std::ostream& os, const NumComplexo& num);
#endif // NUMEROS_COMPLEXOS_H_

0 comments on commit 5952e66

Please sign in to comment.