NEED HELP WITH C++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ID: 3910917 • Letter: N
Question
NEED HELP WITH C++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
FILES
+++++
COMPLEX.HPP
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
// IMPORTANT: This class has some fundamental differences between that found in the written portion
/*
* GENERAL NOTES:
* - Missing const where you should have it will result in loss of points
* - You are free to implement the operator overloads however you see fit, so long as they pass the tests
* OPERATOR OVERLOADS:
* - All operations bar insertion are between two Complex objects
* - Expected format of a Complex object printed using insertion should look like the following examples:
* * 3.14 + 9.81i
* * -2.2 - 4.5i
* - You may use either 'i' or 'j' to represent the imaginary part
* * 'i' or 'j' MUST come after the imaginary number as shown in the examples above
* * While I say this, my solution uses 'i'
* * I have made allowances for 'j', but if it doesn't work, it will be worth trying 'i' in case my test is
* not as robust as I wanted
* - For equivalance, it is very important NOT to solely check doubles for equivalency
* * Due to the nature of doubles, that is a dangerous game (false negatives everywhere)
* * Instead check that the doubles fall within 0.001 of each other
*/
#include <iostream>
class Complex {
public:
Default Constructor // Initial values are both 0
Value Constructor // Takes two parameters, one for real portion, and one for imaginary
Function real // Getter
Function imaginary // Getter
Function conjugate // Returns the conjugate of the calling object
Addition operator overload
Subtraction operator overload
Multiplication operator overload
Division operator overload
+= operator overload
-= operator overload
*= operator overload
/= operator overload
== operator overload
!= operator overload
<< operator overload // Don't care about alignment, may use either 'i' or 'j' to represent imaginary part
private:
double _real;
double _imaginary;
};
#endif
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
main.cpp
#include <iostream>
#include <iomanip>
#include <limits>
#include <sstream>
#include <string>
#include "Complex.hpp"
bool DEQUIV(const double lhs, const double rhs);
bool DEFCONSTR();
bool VALCONSTR();
bool CONJ();
bool ADD();
bool SUBTR();
bool MULT();
bool DIVSN();
bool SADD();
bool SSUBTR();
bool SMULT();
bool SDIVSN();
bool EQUIVSAME();
bool EQUIVRANGE(bool low);
bool INEQUIV();
bool INEQUIVRANGE(bool low);
bool INSRTN();
void eliminateWhiteSpace(std::string &str);
void process(std::string &str, double &r, char &o, double &i, char &label);
int main()
{
const int WIDTH = 30;
char prev = std::cout.fill('.');
std::cout << std::setw(WIDTH) << std::left << "Default Constructor" << DEFCONSTR() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Value Constructor" << VALCONSTR() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Conjugate" << CONJ() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Addition" << ADD() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Subtraction" << SUBTR() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Multiplication" << MULT() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Division" << DIVSN() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Addition to Self" << SADD() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Subtraction from Self" << SSUBTR() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Multiplication with Self" << SMULT() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Division of Self" << SDIVSN() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Equality ("Same")" << EQUIVSAME() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Equality (Lower Range)" << EQUIVRANGE(true) << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Equality (Upper Range)" << EQUIVRANGE(false) << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Inequality (Different)" << INEQUIV() << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Inequality (Lower Range)" << INEQUIVRANGE(true) << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Inequality (Upper Range)" << INEQUIVRANGE(false) << std::endl;
std::cout << std::setw(WIDTH) << std::left << "Insertion" << INSRTN() << std::endl;
std::cout.fill(prev);
return 0;
}
bool DEQUIV(const double lhs, const double rhs)
{
if (lhs >= rhs - 0.001 && lhs <= rhs + 0.001)
return true;
else
return false;
}
bool DEFCONSTR()
{
Complex test;
return DEQUIV(test.real(), 0) && DEQUIV(test.imaginary(), 0);
}
bool VALCONSTR()
{
Complex test(4.7, -89.3);
return DEQUIV(test.real(), 4.7) && DEQUIV(test.imaginary(), -89.3);
}
bool CONJ()
{
Complex one(1, 1);
Complex two(-1, -1);
one = one.conjugate();
two = two.conjugate();
return DEQUIV(one.real(), 1) && DEQUIV(one.imaginary(), -1) &&
DEQUIV(two.real(), -1) && DEQUIV(two.imaginary(), 1);
}
bool ADD()
{
Complex one(-3, 7);
Complex two(5, -9);
Complex sum = one + two;
return DEQUIV(sum.real(), 2) && DEQUIV(sum.imaginary(), -2);
}
bool SUBTR()
{
Complex one(-3, 7);
Complex two(5, -9);
Complex difference = one - two;
return DEQUIV(difference.real(), -8) && DEQUIV(difference.imaginary(), 16);
}
bool MULT()
{
Complex one(4, 1);
Complex two(7, -3);
Complex real(2, 0);
Complex imagine(0, -3);
Complex realM = two * real;
Complex imagM = one * imagine;
Complex product = one * two;
if ( !(DEQUIV(realM.real(), 14) && DEQUIV(realM.imaginary(), -6)) )
return false;
if ( !(DEQUIV(imagM.real(), 3) && DEQUIV(imagM.imaginary(), -12)) )
return false;
return DEQUIV(product.real(), 31) && DEQUIV(product.imaginary(), -5);
}
bool DIVSN()
{
Complex one(2, -1);
Complex two(-3, 6);
Complex quotient = one / two;
return DEQUIV(quotient.real(), -0.266) && DEQUIV(quotient.imaginary(), -0.2);
}
bool SADD()
{
Complex one(-3, 7);
Complex two(5, -9);
one += two;
return DEQUIV(one.real(), 2) && DEQUIV(one.imaginary(), -2);
}
bool SSUBTR()
{
Complex one(-3, 7);
Complex two(5, -9);
one -= two;
return DEQUIV(one.real(), -8) && DEQUIV(one.imaginary(), 16);
}
bool SMULT()
{
Complex one(4, 1);
Complex two(7, -3);
Complex real(2, 0);
Complex imagine(0, -3);
real *= two;
imagine *= one;
one *= two;
if ( !(DEQUIV(real.real(), 14) && DEQUIV(real.imaginary(), -6)) )
return false;
if ( !(DEQUIV(imagine.real(), 3) && DEQUIV(imagine.imaginary(), -12)) )
return false;
return DEQUIV(one.real(), 31) && DEQUIV(one.imaginary(), -5);
}
bool SDIVSN()
{
Complex one(2, -1);
Complex two(-3, 6);
one /= two;
return DEQUIV(one.real(), -0.266) && DEQUIV(one.imaginary(), -0.2);
}
bool EQUIVSAME()
{
Complex one (5.666, -2.394);
Complex two = Complex(3.4, 6.7) - Complex(-2.266, 9.094);
return two;
}
bool EQUIVRANGE(bool low)
{
Complex one(-4.89992, 5.555555);
if (low) {
Complex two(-4.900, 5.55555);
return two;
} else {
Complex two(-4.8998, 5.556);
return two;
}
}
bool INEQUIV()
{
Complex one(3.14, 42.3);
Complex two(3.14, -3.14);
return one != two;
}
bool INEQUIVRANGE(bool low)
{
Complex one(-4.89992, 5.555555);
if (low) {
Complex two(-4.900, 5.554);
return one != two;
} else {
Complex two(-4.897, 5.5555);
return one != two;
}
}
bool INSRTN()
{
Complex one(3.14, 9.81);
Complex two(-2.5, -4.44);
std::stringstream ss;
std::string first;
std::string second;
// Processing variables
double real;
double imagine;
char oper;
char label;
ss << one;
first = ss.str();
ss.str("");
ss << two;
second = ss.str();
ss.str("");
// Process first
eliminateWhiteSpace(first);
process(first, real, oper, imagine, label);
if ( !(DEQUIV(real, 3.14) && DEQUIV(imagine, 9.81) && oper == '+' && (label == 'i' || label == 'j')) )
return false;
// Process second
eliminateWhiteSpace(second);
process(second, real, oper, imagine, label);
if ( !(DEQUIV(real, -2.5) && DEQUIV(imagine, -4.44) && oper == '-' && (label == 'i' || label == 'j')) )
return false;
return true;
}
void eliminateWhiteSpace(std::string &str)
{
std::size_t found = str.find_first_of(" ");
while (found != std::string::npos) {
str.erase(found, 1);
found = str.find_first_of(" ", found + 1);
}
}
void process(std::string &str, double &r, char &o, double &i, char &label)
{
std::string::size_type loc;
r = std::stod(str, &loc);
o = str.at(loc);
if (str.at(loc) == '-')
str = str.substr(loc);
else
str = str.substr(loc + 1);
i = std::stod(str, &loc);
label = str.at(loc);
}
Explanation / Answer
Given below are the needed files. Use it with your test program
Complex.hpp
-------
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
// IMPORTANT: This class has some fundamental differences between that found in the written portion
/*
* GENERAL NOTES:
* - Missing const where you should have it will result in loss of points
* - You are free to implement the operator overloads however you see fit, so long as they pass the tests
* OPERATOR OVERLOADS:
* - All operations bar insertion are between two Complex objects
* - Expected format of a Complex object printed using insertion should look like the following examples:
* * 3.14 + 9.81i
* * -2.2 - 4.5i
* - You may use either 'i' or 'j' to represent the imaginary part
* * 'i' or 'j' MUST come after the imaginary number as shown in the examples above
* * While I say this, my solution uses 'i'
* * I have made allowances for 'j', but if it doesn't work, it will be worth trying 'i' in case my test is
* not as robust as I wanted
* - For equivalance, it is very important NOT to solely check doubles for equivalency
* * Due to the nature of doubles, that is a dangerous game (false negatives everywhere)
* * Instead check that the doubles fall within 0.001 of each other
*/
#include <iostream>
class Complex {
public:
Complex(); // Initial values are both 0
Complex(double real, double img);// Takes two parameters, one for real portion, and one for imaginary
double real() const; // Getter
double imaginary() const; // Getter
Complex conjugate() const; // Returns the conjugate of the calling object
Complex operator +(const Complex &c2);
Complex operator -(const Complex &c2);
Complex operator *(const Complex &c2);
Complex operator /(const Complex &c2);
Complex& operator +=(const Complex &c2);
Complex& operator -=(const Complex &c2);
Complex& operator *=(const Complex &c2);
Complex& operator /=(const Complex &c2);
bool operator ==(const Complex &c2);
bool operator !=(const Complex &c2);
friend std::ostream& operator << (std::ostream& out, const Complex &c);
private:
double _real;
double _imaginary;
};
#endif
Complex.cpp
--------
#include "Complex.hpp"
Complex::Complex() // Initial values are both 0
{
_real = 0;
_imaginary = 0;
}
Complex::Complex(double real, double img)// Takes two parameters, one for real portion, and one for imaginary
{
_real = real;
_imaginary = img;
}
double Complex::real() const // Getter
{
return _real;
}
double Complex::imaginary() const // Getter
{
return _imaginary;
}
Complex Complex::conjugate() const // Returns the conjugate of the calling object
{
return Complex(real(), -imaginary());
}
Complex Complex::operator +(const Complex &c2)
{
return Complex(real() + c2.real() , imaginary() + c2.imaginary());
}
Complex Complex::operator -(const Complex &c2)
{
return Complex(real() - c2.real() , imaginary() - c2.imaginary());
}
Complex Complex::operator *(const Complex &c2)
{
Complex result;
result._real = _real * c2._real - _imaginary * c2._imaginary;
result._imaginary = _imaginary * c2._real + _real * c2._imaginary;
return result;
}
Complex Complex::operator /(const Complex &c2)
{
double t = c2._real * c2._real + c2._imaginary * c2._imaginary;
Complex result;
result._real = (_real * c2._real + _imaginary * c2._imaginary) / t;
result._imaginary = (_imaginary * c2._real - _real * c2._imaginary) / t;
return result;
}
Complex& Complex::operator +=(const Complex &c2)
{
*this = *this + c2;
return *this;
}
Complex& Complex::operator -=(const Complex &c2)
{
*this = *this - c2;
return *this;
}
Complex& Complex::operator *=(const Complex &c2)
{
*this = *this * c2;
return *this;
}
Complex& Complex::operator /=(const Complex &c2)
{
*this = *this / c2;
return *this;
}
bool Complex::operator ==(const Complex &c2)
{
if(_real == c2._real && _imaginary == c2._imaginary)
return true;
else
return false;
}
bool Complex::operator !=(const Complex &c2)
{
return !(*this == c2);
}
std::ostream& operator << (std::ostream& out, const Complex &c)
{
out << c.real();
if(c.imaginary() < 0)
out << " - " << -c.imaginary();
else
out << " + " << c.imaginary();
out << "i";
return out;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.