The input and output operators are reloaded to implement the input and output of plural numbers.

Source: Internet
Author: User

Program code:

# Include
 
  
Using namespace std; class Complex {public: Complex () // defines the default constructor initialization plural {real = 0; imag = 0 ;} // use the initialization table to initialize the Complex (double r, double I): real (r), imag (I) {} Complex operator + (Complex & c2 ); // Complex addition Complex operator-(Complex & c2); // Complex subtraction Complex operator * (Complex & c2 ); // multiplication of Complex numbers Complex operator/(Complex & c2); // division of Complex numbers // heavy load <
  <运算符实现输出复数 friend ostream& operator <<(ostream& output, complex& c); 重载>
   
> The operator implements the input plural friend istream & operator> (istream & input, Complex & c); private: double real; // The real-part double imag of the plural number; // imaginary part of the plural number}; // addition of the plural number Complex: operator + (Complex & c2) {Complex c3; c3.real = real + c2.real; c3.imag = imag + c2.imag; return c3;} // Complex subtraction Complex: operator-(Complex & c2) {Complex c3; c3.real = real-c2.real; c3.imag = imag-c2.imag; return c3 ;} // multiplication of Complex numbers Complex: operator * (Comp Lex & c2) {Complex c3; c3.real = real * c2.real-imag * c2.imag; c3.imag = real * c2.imag + imag * c2.real; return c3;} // division of Complex numbers Complex:: operator/(Complex & c2) {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 ;} // overload> operator to implement the input of the plural istream & operator> (istream & Input, Complex & c) {int a, B; char sign, I; do {cout <"enter a plural number to (a + bi or a-bi) format: "; input> a> sign> B> I;} while (! ('+' = Sign | '-' = sign) & 'I' = I); c. real = a; c. imag = ('+' = sign )? B:-B; return input;} // overload <
   <运算符实现输出复数ostream& operator <<(ostream& output, complex& c){ int num="c.imag;" if(num>
    
0) {output <
    
     
> C1; cout <"enter a plural number:"; cin> c2; // print the first plural cout <"c1 ="; cout <
     
      


Execution result:


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.