Last active
August 29, 2015 14:15
-
-
Save Jules-Baratoux/ac5ba2b8f79f054bd18e to your computer and use it in GitHub Desktop.
Homework #4 – Class Complex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::istream; | |
using std::ostream; | |
using std::cout; | |
using std::cin; | |
using std::endl; | |
#include <sstream> | |
using std::stringstream; | |
#include <string> | |
using std::string; | |
#include <cassert> | |
#include "utils.h" | |
#include "Complex.h" | |
using JulesBaratoux::Complex; | |
// ----------------------------------------------------------------------------- | |
Complex::Complex(double real, double imaginary) | |
: real(real), | |
imaginary(imaginary) | |
{} | |
void Complex::tests::constructors(void) | |
{ | |
// Default constructor | |
{ | |
Complex complex; | |
assert(complex.real == 0.0); | |
assert(complex.imaginary == 0.0); | |
} | |
// Parameter constructor | |
{ | |
Complex complex(42.0, 343.0); | |
assert(complex.real == 42.0); | |
assert(complex.imaginary == 343.0); | |
} | |
} | |
// ----------------------------------------------------------------------------- | |
Complex::operator string(void) const | |
{ | |
stringstream stream; | |
stream << real << '+' << imaginary << 'i'; | |
return stream.str(); | |
} | |
void Complex::tests::operator_string(void) | |
{ | |
assert(static_cast<string>( Complex(4.2, 34.3) ) == "4.2+34.3i"); | |
} | |
// ----------------------------------------------------------------------------- | |
ostream& operator<<(ostream& out, const Complex& complex) | |
{ | |
return out << static_cast<string>(complex); | |
} | |
void Complex::tests::operator_rs(void) | |
{ | |
const Complex complex(4.2, 34.3); | |
cout << complex << endl; | |
} | |
// ----------------------------------------------------------------------------- | |
istream& operator>>(istream& in, Complex& complex) | |
{ | |
char i; | |
in >> complex.real; | |
in >> complex.imaginary; | |
in >> i; | |
return in; | |
} | |
void Complex::tests::operator_ls(void) | |
{ | |
Complex complex; | |
cin >> complex; | |
cout << complex << endl; | |
} | |
// ----------------------------------------------------------------------------- | |
bool Complex::operator==(const Complex& other) | |
{ | |
return (real == other.real && imaginary == other.imaginary); | |
} | |
bool Complex::operator!=(const Complex& other) | |
{ | |
return (*this == other) == false; | |
} | |
void Complex::tests::operator_truth(void) | |
{ | |
assert(Complex() == Complex()); | |
} | |
// ----------------------------------------------------------------------------- | |
Complex Complex::operator+(const Complex& other) const | |
{ | |
return Complex(real + other.real, imaginary + other.imaginary); | |
} | |
void Complex::tests::operator_add(void) | |
{ | |
assert(Complex(6,1) + Complex(4,9) == Complex(10,10)); | |
} | |
// ----------------------------------------------------------------------------- | |
Complex Complex::operator-(const Complex& other) const | |
{ | |
return Complex(real - other.real, imaginary - other.imaginary); | |
} | |
void Complex::tests::operator_sub(void) | |
{ | |
assert(Complex(6,1) - Complex(4,1) == Complex(2,0)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
using std::ostream; | |
using std::istream; | |
using std::string; | |
using std::stringstream; | |
namespace JulesBaratoux | |
{ | |
struct Complex; | |
} | |
istream& operator>>(istream& in, JulesBaratoux::Complex& complex); | |
namespace JulesBaratoux | |
{ | |
struct Complex | |
{ | |
explicit Complex(double real = 0.0, double imaginary = 0.0); | |
// Relaying to operator string, this operator dosen't need to be friend | |
// friend ostream& operator<<(ostream& out, const Complex& complex); | |
friend istream& ::operator>>(istream& in, Complex& complex); | |
Complex operator+(const Complex&) const; | |
Complex operator-(const Complex&) const; | |
bool operator==(const Complex&); | |
bool operator!=(const Complex&); | |
explicit operator string(void) const; | |
~Complex(void) {} | |
struct tests | |
{ | |
static void constructors(void); | |
static void operator_string(void); | |
static void operator_rs(void); | |
static void operator_ls(void); | |
static void operator_truth(void); | |
static void operator_add(void); | |
static void operator_sub(void); | |
}; | |
private: | |
double real; | |
double imaginary; | |
friend struct tests; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdlib> | |
#include "Complex.h" | |
using JulesBaratoux::Complex; | |
int main(void) | |
{ | |
Complex::tests::constructors(); | |
Complex::tests::operator_string(); | |
Complex::tests::operator_rs(); | |
Complex::tests::operator_truth(); | |
Complex::tests::operator_add(); | |
Complex::tests::operator_sub(); | |
// User interaction needed | |
Complex::tests::operator_ls(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$> g++ -std=c++11 *.cpp -o hw4
$> ./hw4
4.2+34.3i
3.6+3.2i
3.6+3.2i