C++
OPTION A (Basic): Complex Numbers
A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number:

This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this format are (3, 3), (-1, -5), (1.034, 77.5) and (99.9, -108.5). We will build a class of complex numbers called Complex.
One important property of every complex number, c, is its length, or modulus, defined as follows. If c = (s, t) then:

For example:

Create a class of Complex numbers.
Private Data
double real, double imag - These two doubles define the Complex number and are called therealand imaginary parts. Think of each Complex number as an ordered pair, (real, imag) of doubles.
Public Instance Methods
Constructors - Allow Complex objects to be constructed with zero, one or two double arguments. The zero-parameter constructor initializes the complex number to (0, 0). The one-parameter constructor should set the real member to the argument passed and set the imag member to 0. The two-parameter constructor should set both real and imag according to the parameters. DO ALL THREE VERSIONS IN A SINGLE CONSTRUCTOR USING DEFAULT PARAMETERS.
Accessors/Mutators - The usual -- two mutators, two accessors.
double modulus() - This will return the double |c|, i.e., the modulus, of the complex number. If the Complex object, c, represents the ordered pair (s, t), then the formula above will give the double value to return.
Complex reciprocal() - This defined in the division operator definition, below.
toString() - This will return a string like "(-23.4, 8.9)" to the client. Do not provide a show() or display() method. You will do this using your insertion operator as shown below.
Operators - We will describe several operators that you must implement for the class. Some will be defined externally (which I will call friend operators. However, you may, or may not really need to declare them to be friends - all you need to know is that if I say "friend" I mean implement the operator as a non-member. Otherwise, implement it as a member.
Exception Class -
Division By Zero
Create your own DivByZeroException as a nested class, just like we did when we learned exceptions. Make sure that both your reciprocal() and operator/() functions throw this exception (you can do this by only throwing it for reciprocal() if you do it correctly). Test it out in your client with a try/catch block, attempting both a normal, and a fatal division.
To test for division by zero, look at the reciprocal() and test for that being zero. However, don't test for == 0.0 exactly, because of inaccuracies in computer storage of doubles. Instead, pick a literal like .00000001 and proclaim the a complex object to be zero if its modulus (or modulus squared) is less than that literal value.
Description of the Operators
Operators +, -, * and /
Provide four overloaded operators, +, -, * and /. Implement these operators as friendmethods of the class, so that Complexobjects can be combined using these four operations. Also, allow a mixed mode operation containing a double and a Complex (which would return a Complex), by treating a double x, as if it were the complex number (x,0). This should come about naturally as a result of the Complex/Complex operators and the proper constructor definitions, not by creating 3 overloads for each operator.
The rules for adding complex numbers are:
(r,i) + (s,j) = (r + s, i + j).
Subtraction is defined analogously. Multiplication is defined by the rule:
(r,i) * (s,j) = (r*s - i*j, r*j + s*i).
To define division, first define the operation reciprocal of the complex number c = (r,i) as follows. c.reciprocal() should return the complex number = ( r / (r*r + i*i), -i / (r*r + i*i) ), if (r*r + i*i) is not zero. If (r*r + i*i) is zero, then reciprocal() throws an exception.
Then define division with the help of the reciprocal() function (informally):
(r,i) / (s,j) = (r,i) * reciprocal(s,j)
Notice that if you correspond a normal double number x, with its complex counterpart, (x,0), then you can think of the ordinary double numbers as a subset of the complex numbers. Also, note that, under this correspondence, these four operations result in ordinary addition, multiplication, etc. for the double subset. Try adding or dividing (6,0) and (3,0) if you don't believe me.
In summary, you should be able to handle the combinations below:
Complex a(3,-4), b(1.1, 2.1), c; double x=2, y= -1.7; c = a + b; c = x - a; c = b * y; // and also: c = 8 + a; c = b / 3.2;
To help you confirm your computations, here are some examples:
(1, 2) + (3, 4) = (4, 6) (1, 2) - (3, 4) = (-2, -2) (1, 2) * (3, 4) = (-5, 10) (1, 2) / (3, 4) = (0.44, 0.08) (1, 2) + 10 = (11, 2) 10 / (3, 4) = (1.2, -1.6)
Operators << and =
Overload the insertion and assignment operators in the expected ways.
Operators < and ==
a < b should mean |a| < |b|, that is, a.modulus() < b.modulus(). a == b should mean (a.real == b.real) && (a.imag == b.imag). Define these two operators to make this so. (Note: < is not standard in math; there is no natural ordering mathematically for complex numbers. However, we'll allow it to be defined this way in the problem.)
Pass all Complex parameters as const & and return all values of functions that sensibly return complex numbers (like operator+(), e.g.) asComplex values (not & parameters).
OPTION B-1 (Intermediate): Really Fun Complex Project
In addition to demonstrating the above functionality, create a ComplexNode and ComplexStack inherited from Node and Stack or our modules or, reading ahead, using the stl stack template. Use it to do something fun, where you get to decide what "fun" means. In your client, be sure to first demonstrate all operators in Option A before demonstrating the stack aspect of your Option B.
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
class complex
{
private:
double real; // Real Part
double imag; // Imaginary Part
public:
complex(double r=0.0,double im=0.0) // CONSTRUCTOR
{
real=r;
imag=im;
}
complex add(complex c)
{
complex tmp;
tmp.real=this->real+c.real;
tmp.imag=this->imag+c.imag;
return tmp;
}
complex subtract(complex c)
{
complex tmp;
tmp.real=this->real - c.real;
tmp.imag=this->imag - c.imag;
return tmp;
}
complex multiply(complex c)
{
complex tmp;
tmp.real=(real*c.real)-(imag*c.imag);
tmp.imag=(real*c.imag)+(imag*c.real);
return tmp;
}
complex divide(complex c)
{
double div=(c.real*c.real) + (c.imag*c.imag);
complex tmp;
tmp.real=(real*c.real)+(imag*c.imag);
tmp.real/=div;
tmp.imag=(imag*c.real)-(real*c.imag);
tmp.imag/=div;
return tmp;
}
complex getreciprocal()
{
complex t;
t.real=real;
t.imag=imag * -1;
double div;
div=(real*real)+(imag*imag);
t.real/=div;
t.imag/=div;
return t;
}
double getmodulus()
{
double z;
z=(real*real)+(imag*imag);
z=sqrt(z);
return z;
}
void setdata()
{
cout << "Enter the real and imaginary parts : \n ";
cin >> this->real;
cin >> this->imag;
}
complex getdata()
{
complex t;
t.real=real;
t.imag=imag ;
return t;
}
double getreal()
{
return real;
}
double getimaginary()
{
return imag;
}
void printComplex(void)
{
cout << "Real : " << this->real << endl
<< "Imaginary : " << this->imag << endl;
}
};
int main()
{
complex a, b, c, d;
cout << "Setting first complex number " << endl;
a.setdata();
cout << "Setting second complex number " << endl;
b.setdata();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a.subtract(b);
a.printComplex();
}
C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...
c++
2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...
Design a class Complex for handling Complex numbers and include the following: _ real: a double _ imaginary: a double The class has the following member functions. a. A constructor initializing the number with default parameters. b. Getters and Setters of the class data members as given below _ void setReal(double r) _ double getReal()const _ void setImaginary(double i) _ double getImaginary() const d. Overload unary ! operator which returns true if the real and the imaginary parts are zero,...
This is a C++ programming question. Please provide the correct,
workable code. Use the following three programs to help solve the
problem. Provide comments throughout the code.
Problem to solve:
Code 1: Complex.h
#pragma once
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
private:
double real;
double imag;
public:
// initialize the complex number to 0.0
Complex() : real(0.0), imag(0.0) {}
// initialize the complex number at declaration or
new
Complex(double r, double i) :...
Given two complex numbers, find the sum of the complex numbers using operator overloading. Write an operator overloading function ProblemSolution operator + (ProblemSolution const &P) which adds two ProblemSolution objects and returns a new ProblemSolution object. Input 12 -10 -34 38 where, Each row is a complex number. First element is real part and the second element is imaginary part of a complex number. Output -22 28 Two complex numbers are 12-10i and -34+38i. Sum of complex numbers are =...
C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √ −1. You should represent a complex number here as two values of type double. You should name the variables real and imaginary. You can call the...
C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers. is the imaginary unit with the property of . is called the real part of the complex number and is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...
C++
//add as many comments as possible
5. A complex number consists of two components: the real component and the imaginary component. An example of a complex number is 2+3i, where 2 is the real component and 3 is the imaginary component of the data. Define a class MyComplexClass. It has two data values of float type: real and imaginary This class has the following member functions A default constructor that assigns 0.0 to both its real and imaginary data...
Complex number class:: Question Changed to just the Java portion Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. (the following is for the c++ and python version, not sure if its needed for this) op: Complex × Complex → Complex op: Complex × double → Complex op: double × Complex → Complex Where op is one of +, -, *, or /. In addition, you will need...
USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √−1. You should represent a complex number here as two values of type double. You should name the variables real...
Consider the following C struct that represents a complex number. struct complex { double real; double imaginary; }; (a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class: A default constructor that initializes the real and imaginary parts to 0. A constructor that allows initialization of both real and imaginary parts to any double value. A public member function that returns the magnitude of the complex number....