asp.net (C #) complex class (Complex subtraction Arithmetic) _ Practical Tips

Source: Internet
Author: User
Tags arithmetic
One of my Java jobs, I changed it to ASP.net (C #).
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
Complex complex_a = new Complex (1.0, 1.0);
Complex Complex_b = new Complex (2.0, 2.0);
Response.Write ("Addition operation result:" + complex_a.complex_add (Complex_b). ToString () + "<br/>");
Response.Write ("Subtraction Result:" + Complex_a.complex_minus (Complex_b). ToString () + "<br/>");
Response.Write ("Multiplication Result:" + complex_a.complex_multi (Complex_b). ToString () + "<br/>");
Response.Write ("Division operation result:" + complex_a.complex_divide (Complex_b). ToString ());
}
Design by Ahanan from: Search bar sosuo8.com
public class Complex
{
The real part in the plural
Private double complex_real;
Imaginary part in a complex number
Private double complex_imagin;

Constructors
Public complex (double R, double i)
{
Complex_real = R;
Complex_imagin = i;
}

Overriding the ToString () method
public override string ToString ()
{
return this.complex_real + "+" + this.complex_imagin + "I";
}

Complex number addition operation
Public complex Complex_add (complex c)
{
Get the real part after the addition operation
Double complex_real = this.complex_real + c.complex_real;

To obtain the imaginary part after the addition operation
Double Complex_imagin = This.complex_imagin + c.complex_imagin;

Returns a complex number class
return new complex (Complex_real,complex_imagin);
}

Complex subtraction operation
Public complex Complex_minus (complex c)
{
Gets the real part after the subtraction operation
Double complex_real = this.complex_real-c.complex_real;

To obtain the imaginary part after the subtraction operation
Double complex_imagin = This.complex_imagin-c.complex_imagin;

Returns a complex number class
return new Complex (Complex_real, complex_imagin);
}

Multiplication operations
Public complex Complex_multi (complex c)
{
Get the real part after the multiplication operation
Double complex_real = this.complex_real * C.complex_real-this.complex_imagin * c.complex_imagin;

To obtain the imaginary part after multiplication operation
Double Complex_imagin = this.complex_real * c.complex_imagin + this.complex_imagin * c.complex_real;

Returns a complex number class
return new Complex (Complex_real, complex_imagin);
}

Division result (A+BI)/(C+di) = (A+bi) (C-di)/(C+di) (C-di)
Public complex Complex_divide (complex c)
{
Gets the value of (C+di) (C-di)
Double d = c.complex_real * c.complex_real + c.complex_imagin * c.complex_imagin;

Get the real part after the division operation
Double complex_real = (This.complex_real * c.complex_real + this.complex_imagin * c.complex_imagin)/D;

To obtain the imaginary part after division operation
Double Complex_imagin = (This.complex_real * (-c.complex_imagin) + this.complex_imagin * c.complex_real)/D;

Returns a complex number class
return new Complex (Complex_real, complex_imagin);
}
}


Run Result:

Copy Code code as follows:
addition operation result:3+3i  
Subtraction result:-1+-1i  
Multiplication Result: 0+4i& nbsp; 
Division results:0.5+0i 
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.