Please help! The goal of this project is to write the interface and implementation for the complex number class. THE INSTRUCTIONS ARE BELOW (IN BOLD): Please complete the below STARTER functions (some variables need to be defined) ALL BOLDED SECTIONS CORRESPOND WITH ONE ANOTHER: complex.h: /** * public interface for the complex number class. * */ #ifndef COMPLEX_H #define COMPLEX_H #include using namespace std; /* YOUR TASK HERE IS TO GIVE THE COMPLETE DEFINITION OF THE class Complex. BE SURE TO INCLUDE A DESCRIPTION OF EACH OF THE FUNCTIONS. YOU WILL PROVIDE ONLY THE PUBLIC INTERFACE OF THE MEMBER AND FRIEND FUNCTIONS, NOT THEIR DEFINITIONS. THE MEMBER AND FRIEND FUNCTIONS WILL BE DEFINED IN THE IMPLEMENTATION FILE. DEFINE THE CLASS BELOW. */ #endif complex.cpp: /** * Implementation for the complex class * */ #include #include #include "complex.h" /* SOME FUNCTION HAVE BEEN IMPLEMENTED. IMPLEMENT ALL OTHER FUNCTIONS. */ Complex::Complex() { real = 0.0; imag = 0.0; } double Complex::getReal() const { return real; } Complex Complex::conjugate() const { return Complex(real,-imag); } Complex operator +(const Complex& z1, const Complex& z2) { return Complex(z1.real+z2.real,z1.imag+z2.imag); } ostream& operator<<(ostream& out, const Complex& z) { if (z.real == 0 && z.imag == 0) { out<<"0"; return out; } if (z.real == 0) { if (z.imag < 0) { if (z.imag != -1) out< #include "complex.h" #include using namespace std; int main(int argc, char **argv) { Complex z0(3.6,4.8); Complex z1(4,-2); Complex z2(-4,2); Complex z3(-4,-3); Complex z4(3,-4); Complex z5; Complex z6; Complex z7; double ang; cout<<"z0 = "<
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
Please help! The goal of this project is to write the interface and implementation for the complex number class. THE INSTRUCTIONS ARE BELOW (IN BOLD):
Please complete the below STARTER functions (some variables need to be defined) ALL BOLDED SECTIONS CORRESPOND WITH ONE ANOTHER:
complex.h:
/**
* public interface for the complex number class.
* </pre>
*/
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
/* YOUR TASK HERE IS TO GIVE THE COMPLETE DEFINITION OF
THE class Complex. BE SURE TO INCLUDE A DESCRIPTION OF EACH OF THE FUNCTIONS.
YOU WILL PROVIDE ONLY THE PUBLIC INTERFACE OF THE MEMBER
AND FRIEND FUNCTIONS, NOT THEIR DEFINITIONS.
THE MEMBER AND FRIEND FUNCTIONS WILL BE DEFINED IN THE
IMPLEMENTATION FILE.
DEFINE THE CLASS BELOW.
*/
#endif
complex.cpp:
/**
* Implementation for the complex class
* </pre>
*/
#include <cmath>
#include <cstdlib>
#include "complex.h"
/* SOME FUNCTION HAVE BEEN IMPLEMENTED. IMPLEMENT
ALL OTHER FUNCTIONS.
*/
Complex::Complex()
{
real = 0.0;
imag = 0.0;
}
double Complex::getReal() const
{
return real;
}
Complex Complex::conjugate() const
{
return Complex(real,-imag);
}
Complex operator +(const Complex& z1, const Complex& z2)
{
return Complex(z1.real+z2.real,z1.imag+z2.imag);
}
ostream& operator<<(ostream& out, const Complex& z)
{
if (z.real == 0 && z.imag == 0)
{
out<<"0";
return out;
}
if (z.real == 0)
{
if (z.imag < 0)
{
if (z.imag != -1)
out<<z.imag<<"i";
else
out<<"-i";
}
else
{
if (z.imag != 1)
out<<z.imag<<"i";
else
out<<"i";
}
return out;
}
if (z.imag == 0)
{
out<<z.real;
return out;
}
out <<z.real;
if (z.imag < 0)
{
if (z.imag != -1)
out<<z.imag<<"i";
else
out<<"-i";
}
else
{
if (z.imag != 1)
out<<"+"<<z.imag<<"i";
else
out<<"+i";
}
return out;
}
completest.cpp
/**
* A program to test the complex class implementation
*/
#include <iostream>
#include "complex.h"
#include <cstdlib>
using namespace std;
int main(int argc, char **argv)
{
Complex z0(3.6,4.8);
Complex z1(4,-2);
Complex z2(-4,2);
Complex z3(-4,-3);
Complex z4(3,-4);
Complex z5;
Complex z6;
Complex z7;
double ang;
cout<<"z0 = "<<z0<<" and re(z0) = "<<z0.getReal()<<" and "
<<"im(z0) = "<<z0.getImag()<<"."<<endl;
cout<<"z1 = "<<z1<<endl;
cout<<"z2 = "<<z2<<endl;
cout<<"z3 = "<<z3<<endl;
cout<<"z4 = "<<z4<<endl;
cout<<"z2 x z3 = "<<z2*z3<<endl;
cout<<"z1 + z2 = "<<z1+z2<<endl;
cout<<"z3 = "<<z3<<endl;
cout<<"z4 = "<<z4<<endl;
cout<<"z3-z4 = "<<z3-z4<<endl;
cout<<"((z2+z3)x(z3-z4)) = ";
cout<<(z2+z3)*(z3-z4)<<endl;
cout<<"z1^3 = "<<pow(z1,3)<<endl;
cout<<"1/z2^2 = "<<pow(z2,-2)<<endl;
cout<<"z3^0 = "<<pow(z3,0)<<endl;
try
{
cout<<"((z2+z3)x(z3-z4))/z3 = ";
z6 = ((z2+z3)*(z3-z4))/z3;
cout<<z6<<endl;
cout<<"z3 / z4 = "<<(z3/z4)<<endl;
cout<<"Conj(z4) = "<<z4.conjugate()<<endl;
cout<<"The real part of the conjugate of z4 is "<<(z4.conjugate()).getReal()<<endl;
cout<<"The imaginary part of the conjugate of z4 is "<<(z4.conjugate()).getImag()<<endl;
cout<<"The argument of the conjugate of z4 is "<<(z4.conjugate()).argument()<<"."<<endl;
cout<<"The modulus of the conjugate of z4 is "<<(z4.conjugate()).modulus()<<"."<<endl;
cout<<"(z3*conj(z4))/(z4*conj(z4))= ";
cout<<(z3*z4.conjugate())/(z4*z4.conjugate())<<endl;
/* Add statement here to compute ((z1-z2).(z3/z4))/(z4/z3)
and print the result:*/
ang = (z1+z2).argument();
cout<<"angle = "<<ang<<endl;
/* Put a comment here explaining what happened and why?
*/
}
catch(int e)
{
if (e == -1)
cerr<<"DivideByZero Exception in / operator"<<endl;
else if (e == -2)
cerr<<"DivideByZero Exception in argument function"<<endl;
}
return 0;
}
Step by step
Solved in 4 steps