Question

C++

Addition of Complex Numbers

Background Knowledge

A complex number can be written in the format of a + bi, where LaTeX: a and LaTeX: b are real numbers.  LaTeX: i is the imaginary unit with the property of vĩ--ı .  LaTeX: a is called the real part of the complex number and  LaTeX: b 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 imaginary parts together (the result's imaginary part).

  LaTeX: \left(a_1+b_1i\right)+\left(a_2+b_2i\right)=\left(a_1+a_2\right)+\left(b_1+b_2\right)i

The ComplexNumber Class

Define a class ComplexNumber, which has two double variables, real and imaginary, in its private section. In addition, the class should have the following functions in its public section.

1. A default constructor (no arguments) and a constructor that takes in two double variables as arguments, which represents the real and imaginary parts. You can combine the two constructors into one constructor, by setting default values for the parameters.

2. Setters and getters of the real and imaginary parts.

3. Overload the operator +, which performs the addition of two ComplexNumber objects. You should figure out all the details of the function (e.g. return type, parameters, const function or not, etc.) by yourself.

4. Overload the operator <<, which outputs a ComplexNumber to an output stream. You should figure out all the details of the function (e.g. return type, parameters, const function or not, etc.) by yourself. Please see the sample output file for the output format.

Input File

There are 4 numbers in each line of the input file, which are real part of ComplexNumber #1, imaginary part of ComplexNumber #1, real part of ComplexNumber #2, imaginary part of ComplexNumber #2, in that order from left to right.

You cannot assume how many lines the input file contains. Your program should keep reading data from the input file, until there are no more data to read in.

The main() Function

In the main() function, the program should add the two ComplexNumber objects read from each line of the input file together. The program outputs the addition result to the output file (one result per line).

a + bi



vĩ--ı


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class ComplexNumber
{
public:
   ComplexNumber(double,double);
   void setReal(double);
   void setImaginary(double);
   double getReal();
   double getImaginary();
   ComplexNumber operator +( ComplexNumber&) ;
   friend ostream& operator <<(ostream&, const ComplexNumber&);
private:
   double real;
   double imaginary;
};
ComplexNumber::ComplexNumber(double real=0,double img=0)
{
   this->real=real;
   this->imaginary=img;
}
void ComplexNumber::setReal(double real)
{
   this->real=real;
}
void ComplexNumber::setImaginary(double img)
{
   this->imaginary=img;
}
double ComplexNumber::getImaginary()
{
   return imaginary;
}
double ComplexNumber::getReal()
{
   return real;
}
ComplexNumber ComplexNumber::operator +( ComplexNumber& c2)
{
   return ComplexNumber(real + c2.real, imaginary + c2.imaginary);
}
ostream& operator <<(ostream& os, const ComplexNumber& c)
{
   os << c.real;
if (c.imaginary < 0)
os << c.imaginary << "i";
else if (c.imaginary > 0)
os << "+" << c.imaginary << "i";
return os;
}
int main()
{
   ComplexNumber c1,c2;
   ifstream inFile;
   inFile.open("inputFile.txt");
   if(!inFile)
   {
       cout<<"Can't opent the file please check file name! Exiting!!!!"<<endl;
       return 1;
   }
   double r1,r2,i1,i2;
   while(inFile>>r1>>i1>>r2>>i2)
   {
       c1.setReal(r1);
       c1.setImaginary(i1);
       c2.setReal(r2);
       c2.setImaginary(i2);
       cout<<"("<<c1<<") + ("<<c2<<") = ("<<(c1+c2)<<")"<<endl<<endl;
   }
   return 0;
}

output and text file

x | inputFile . Notepad CAMINDOwS)system32\cmd.exe (10+20i)(34+29i)(44+49i) -10+20i)+(39+77i)-(29+97i) (12-21i)(23+221) (35+1

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
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 r...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. A complex number consists of two components: the real component and the imaginary component. A...

    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...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a...

    C++ Complex Class! Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...

  • C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to...

    C++ Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    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...

  • Coding in C++ Additional files are:- complexInput.txt   (3+4i) * (5-6i) complexOutput.txt 39+2i complexInputWrong.txt (3+4i) - dummy...

    Coding in C++ Additional files are:- complexInput.txt   (3+4i) * (5-6i) complexOutput.txt 39+2i complexInputWrong.txt (3+4i) - dummy USE COMPLEX.H, COMPLEX.CPP AND MAIN.CPP What you will learn Implementing classes .File I/O More operator overloading .Error checking Coding exercise Create a class called complexNumber that stores a complex number of the form a+bi, where i is v-I. a is the real and b is the imaginary part of the number1. You should be able to get the real and imaginary parts of the...

  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...

  • 12.24 HW2: Practice with Classes Objectives: Become familiar with developing a C++ class, to include developing:...

    12.24 HW2: Practice with Classes Objectives: Become familiar with developing a C++ class, to include developing: A constructor An multiplication operator A friend function (input operator) Introduce various C and C++ functions for parsing user input Assignment Details This assignment consists of 6 parts. Part 1: Using an IDE of your own choosing (Visual Studio is recommended), create a new project and copy the starter code ComplexNumber.h ComplexNumber.cpp and TestComplexNumber.cpp to this new project for developing and testing your solution....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT