Lab project 2 -- arithmetic operation with four complex numbers

Source: Internet
Author: User

Lab content
[Problem description]
Design and implement a demo program that can perform complex operations.
[Basic requirements]
Implement the following six basic operations:
1) The input real and virtual parts generate a plural number;
2) sum of two plural values;
3) calculate the difference between two plural numbers;
4) product of two plural values;
5) extract the real part from the known plural;
6) Remove the virtual part from the known plural;
The calculation result is displayed in the form of a corresponding plural or real number.
[Code]
# Include <iostream>
Using namespace std;
Template <class Elem> class Complex
{
Private:
Elem reality;
Elem falsehood;
Public:
Complex (Elem r, Elem f)
{
Reality = r;
Falsehood = f;
}

Complex operator + (const Complex & c1) // initial condition: There are already two plural numbers;
// Operation Result: add the real and virtual parts of the two plural numbers to a new plural number.
{
Return Complex (reality + c1.reality, falsehood + c1.falsehood );
}

Complex operator-(const Complex & c1) // initial condition: There are already two plural numbers;
// Operation Result: subtract the real and virtual parts of the two plural numbers to obtain a new plural number.
{
Return Complex (reality-c1.reality, falsehood-c1.falsehood );
}

Complex operator * (const Complex & c1) // initial condition: There are already two plural numbers;
// Operation result: the real part of the two plural numbers is multiplied by the real part of the two plural numbers minus the real part of the two plural numbers;
// The result of multiplying the virtual and real parts of two plural numbers and then adding them as the imaginary parts of the new plural number.
{
Return Complex (reality * c1.reality-falsehood * c1.falsehood, reality * c1.falsehood + falsehood * c1.reality );
}

Complex operator/(const Complex & c1) // overload operator/
{
If (falsehood! = 0)
Return Complex (reality * c1.reality + falsehood + c1.falsehood)/(c1.reality * c1.reality + c1.falsehood * c1.falsehood), (falsehood * c1.reality-reality * handle) /(c1.reality * c1.reality + c1.falsehood * c1.falsehood ));
Else
Cout <"the denominator cannot be 0. Enter" <endl;
}

Elem Reality () // initial condition: the plural already exists;
// Operation Result: returns the real part of the plural number;
{
Return reality;
}

Elem Falsehood () // initial condition: the plural already exists;
// Operation Result: returns the imaginary part of the plural number.
{
Return falsehood;
}

Void Conjugate () // obtain the complex number of contices.
{
Falsehood =-falsehood;
}

Void Output () // initial condition: the plural value already exists,
// Operation Result: output the complex number to the screen in the form of a plural number
{
If (falsehood> 0)
{
Cout <"(" <reality <"+" <falsehood <"I" <")" <endl;
}
Else
Cout <"(" <reality <falsehood <"I" <")" <endl;
}
};
Int main () // Test
{
Complex <float> c1 (1, 2 );
Complex <float> c2 (2, 3 );
Cout <"Test Result:" <endl;
Cout <"Two plural numbers: (1 + 2i) AND (2 + 3i)" <endl;
Cout <"(1 + 2i) + (2 + 3i) = ";
(C1 + c2). Output ();
Cout <"\ n (1 + 2i)-(2 + 3i) = ";
(C1-c2). Output ();
Cout <"\ n (1 + 2i) * (2 + 3i) = ";
(C1 * c2). Output ();
Cout <"\ n (1 + 2i)/(2 + 3i) = ";
(C1/c2). Output ();
Cout <"\ nc1:" <c1.Reality () <endl;
Cout <"\ nc1's virtual part is:" <c1.Falsehood () <endl;
Cout <"\ nc2:" <c2.Reality () <endl;
Cout <"\ nc2's virtual part is:" <c2.Falsehood () <endl;
Cout <"\ nc1's combination of the complex numbers is :";
C1.Conjugate ();
C1.Output ();
Cout <"\ nc2's combination of the following :";
C2.Conjugate ();
C2.Output ();
System ("pause ");
Return 0;
}
Note: The above content is for reference only. If you have any questions, please correct them.


Author: Xinhai xinhang"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.