Question

In: Computer Science

PLEASE DO IN C++ (Math: The Complex class) The description of this project is given in...

PLEASE DO IN C++

(Math: The Complex class)
The description of this project is given in Programming Exercise 14.7 in the Chapter 14 Programming Exercise from the Book section. If you get a logical or runtime error, please refer https://liangcpp.pearsoncmg.com/faq.html.

Design a class named Complex for representing complex numbers and the functions add, subtract, multiply, divide, abs for performing complex-number operations, and the toString function for returning a string representation for a complex number. The toString function returns a+bi as a string. If b is 0, it simply returns a.

Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. Also provide the getRealPart() and getImaginaryPart() functions for returning the real and imaginary part of the complex number, respectively.

Overload the operators +, -, *, /, +=, -=, *=, /=, [], unary + and -, prefix ++ and --, postfix ++ and --, <<, >>.

Overload [] so that [0] returns a and [1] returns b.

Overload the relational operators <, <=, ==, !=, >, >= by comparing the absolute values of two complex numbers.

Overload the operators +, -, *, /, <<, >>, <, <=, ==, !=, >, >= as nonmember functions.

PLEASE DO IN C++

Solutions

Expert Solution

program plan:

Define the class complex

1.Declare the global variable to hold real and imaginary values.

2.Define the constructor with zero parameter to set the value 0.

3.Define the constructor set the imaginary value to 0 and real value with parameter value

4.Define the constructor with two parameter set the real and imaginary value's with real parameter values

5.Define two getter method getImaginarypart() and getRealpart() which return the imaginary ang real global variable values.

6.define the all methods addition(),substraction() division() and multiplication() that returns the complex object with single parameter which accept the complex object

7.Display the result in the form of a+bi;

8.call the add(),sub(),mul(),divide() method using first complex class object by passing second complex object.

9. Display the values.

#include<iostream>
using namespace std;
class complex{
int real, img;
public:
complex(){
//default constructor to initialize complex number to 0+0i
real = 0; img = 0;
}
complex(int r, int i){
//parameterized constructor to initialize complex number.
real = r; img = i;
}
void setRealPart();
void setImgPart();

void getRealPart();
void getImgPart();

void display();
friend complex add(complex, complex);
friend complex sub(complex, complex);
friend complex mul(complex, complex);
friend complex division(complex, complex);

};
void complex::setRealPart(){
cout << "Enter Real part: ";
cin >> real;
}
void complex::setImgPart(){

cout << "Enter Imaginary Part: ";
cin >> img;

}
void complex::getRealPart(){
cout << "The complex number is: "<< real << "+" << img << "i" << endl;
}

void complex::getImgPart(){
cout << "The complex number is: "<< real << "+" << img << "i" << endl;
}

void complex::display(){

  
if(img < 0)
if(img == -1)
cout << "The complex number is: "<< real << "-i" << endl;
else
cout << "The complex number is: "<< real << img << "i" << endl;
else
if(img == 1)
cout << "The complex number is: "<< real << " + i"<< endl;
else
cout << "The complex number is: "<< real << " + " << img << "i" << endl;
}
complex add(complex c1, complex c2){
complex res;

res.real = c1.real + c2.real;//addition for real part
res.img = c1.img + c2.img;//addition for imaginary part


return res;//the result after addition
}
complex sub(complex c1, complex c2){
complex res;
res.real = c1.real - c2.real;//subtraction for real part
res.img = c1.img - c2.img;//subtraction for imaginary part
return res;//the result after subtraction
}

complex mul(complex c1, complex c2){
complex res;
res.real = c1.real * c2.real;//multiplication for real part
res.img = c1.img *c2.img;//multiplication for imaginary part
return res;//the result after multiplication
}

complex division(complex c1, complex c2){
complex res;
res.real = c1.real / c2.real;//division for real part
res.img = c1.img /c2.img;//division for imaginary part
return res;//the result after division
}

main(){
complex n1(3, 2), n2(4, -3);
complex result;
result = add(n1,n2);
result.display();
result = sub(n1,n2);
result.display();
result = mul(n1,n2);
result.display();
result=division(n1,n2);
result.display();
  
}

Output is :

$g++ -o main *.cpp
$main
The complex number is: 7-i       :addition
The complex number is: -1 + 5i   :substraction
The complex number is: 12-6i     :multiplication
The complex number is: 0 + 0i    :division
The complex number is: 48 + 1073741824i : extraction

Related Solutions

ASAP (Math: The Complex class) A complex number is a number in the form a +...
ASAP (Math: The Complex class) A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a +...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that...
in java please Project 2: The Triangle Class Problem Description: Design a class named Triangle that extends GeometricObject. The class contains: • Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. • A no-arg constructor that creates a default triangle. • A constructor that creates a triangle with the specified side1, side2, and side3. • The accessor methods for all three data fields. • A method named getArea() that...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has asked for your help. Write a program that will accept a pirate’s starting amount of treasure in number of gold pieces. The program will then run one of two simulations, indicated by the user: 1) The first simulation runs indefinitely, until one of two conditions is met: the pirate’s treasure falls to 0 or below, or the pirate’s treasure grows to 1000 or above....
Class object in C++ programming language description about lesson base class and derived class example.
Class object in C++ programming language description about lesson base class and derived class example.
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given...
DO THIS IN C++: Question: Write a function “reverse” in your queue class (Codes are given below. Use and modify them) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue. NOTE: A humble request, please don't just copy and paste the answer from chegg. I need more specific answer. Also I don't have much question remaining to...
To do this in C++ preferably not in a class but if not possible do in...
To do this in C++ preferably not in a class but if not possible do in a class. Assignment Specifications Your assignment is to write a single-player version of the casino card game Blackjack, also known as 21. The goal of the game Blackjack is to get cards whose total value comes closest to 21 without going over. Getting a card total over 21 is called "busting". The player will play against the dealer. There are 3 possible outcomes. Player...
Please do the math by hand, do not use a program, I need to see the...
Please do the math by hand, do not use a program, I need to see the procedure, the answer itself is less important. Comparison of peak expiratory flow rate (PEFR) before and after a walk on a cold winter's day for a random sample of 9 asthmatics. Use the following data to determine if the patients conditioned changed after a walk. Present your results and make some interpretations. Subject Before After 1 312 300 2 242 201 3 340 232...
C Programme Time limit: 5000ms Memory limit: 256mb Description: Given a series of queue operations, please...
C Programme Time limit: 5000ms Memory limit: 256mb Description: Given a series of queue operations, please complete the queue ADT to output for respective operations. -------------------------Copy the following code, complete it and submit------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef enum {enqueue = 1, dequeue, end, isfull, isempty} Operation; struct Queue { int * arr; int capacity; int front; int rear; }; struct Queue * Create(int capacity) { struct Queue * queue = (struct Queue *)malloc(sizeof(struct Queue)); queue->arr = (int*)malloc(sizeof(int)...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input initial values of dollars and cents and then asks for deposits and withdrawals. The class should include the following information: Operations (Member Functions) 1. Default constructor that sets both dollars and cents to 0. 2. The constructor has 2 parameters that set dollars and cents to the indicated values. 3. Open account (with an initial deposit). This is called to put initial values in...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT