Use the template class to implement the four arithmetic operations of the plural number

Source: Internet
Author: User

Plural: it refers to the number a + bi that can be written in the following form. Here, a and B are real numbers, I is a virtual number unit, and I * I =-1

In the plural a + bi, a is the real part of the duplex, and B is the imaginary part of the plural.


Arithmetic Operation of the plural number:

(A + bi) + (c + di) = (a + c) + (B + d) I;

(A + bi)-(c + di) = (a-c) + (B-d) I;

(A + bi) * (c + di) = (ac-bd) + (ad + bc) I;

(A + bi)/(c + di) = (ac + bd) + (bc-ad) I)/(c * c + d * d );


Program code:

# Include
 
  
Using namespace std; template
  
   
// Declare class Complex {public: Complex () {real = 0; imag = 0;} Complex (T r, T I) {real = r; imag = I;} Complex complex_add (Complex & c2); // The Complex addition Complex complex_minus (Complex & c2); // The Complex subtraction Complex complex_multiply (Complex & c2 ); // multiplication of the Complex number Complex complex_divide (Complex & c2); // division of the Complex number void display (); // display the Complex number private: T real; // real part of the Complex number T imag; // The imaginary part of the plural number}; template
   
    
Complex
    
     
Complex
     
      
: Complex_add (Complex & c2) // Complex addition {Complex c3; c3.real = real + c2.real; c3.imag = imag + c2.imag; return c3;} template
      
        Complex
       
         Complex
        
          : Complex_minus (Complex & c2) // subtraction of Complex numbers {Complex c3; c3.real = real-c2.real; c3.imag = imag-c2.imag; return c3;} template
         
           Complex
          
            Complex
           
             : Complex_multiply (Complex & c2) // multiplication of Complex numbers {Complex c3; c3.real = real * c2.real-imag * c2.imag; c3.imag = real * c2.imag + imag * c2.real; return c3;} template
            
              Complex
             
               Complex
              
                : Complex_divide (Complex & c2) // division of the plural {Complex c3; c3.real = (real * c2.real + imag * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag ); c3.imag = (imag * c2.real-real * c2.imag)/(c2.real * c2.real + c2.imag * c2.imag); return c3;} template
               
                 Void Complex
                
                  : Display () // display the plural {if (imag> 0) // when the imaginary part is greater than 0 {cout <
                 
                   Cmp1 (8,-2), Cmp2 (7, 10), Cmp3; // Add two plural numbers Cmp1.display (); cout <"+"; Cmp2.display (); cout <"="; Cmp3 = Cmp1.complex _ add (Cmp2); Cmp3.display (); cout <
                  
                   


Execution result:


Zookeeper

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.