Asp.net (C #) plural class (multiplication, subtraction, division, and division of the plural)

Source: Internet
Author: User

I wrote a Java job into Asp.net (C #). CopyCode The Code is 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 Calculation Result:" + complex_a.complex_add (complex_ B). tostring () + "<br/> ");
Response. Write ("subtraction Calculation Result:" + complex_a.complex_minus (complex_ B). tostring () + "<br/> ");
Response. Write ("Multiplication Result:" + complex_a.complex_multi (complex_ B). tostring () + "<br/> ");
Response. Write ("Division calculation result:" + complex_a.complex_divide (complex_ B). tostring ());
}
// Design by ayunan: Search for sosuo8.com
Public class Complex
{
// Real part in the plural
Private double complex_real;
// Imaginary part in the plural number
Private double complex_imagin;

// Constructor
Public complex (Double R, double I)
{
Complex_real = R;
Complex_imagin = I;
}

// Rewrite the tostring () method
Public override string tostring ()
{
Return this. complex_real + "+" + this. complex_imagin + "I ";
}

// Add multiple numbers
Public complex complex_add (Complex C)
{
// Obtain the real part after the addition operation
Double complex_real = This. complex_real + C. complex_real;

// Obtain the virtual part after the addition operation
Double complex_imagin = This. complex_imagin + C. complex_imagin;

// Return a plural class
Return new complex (complex_real, complex_imagin );
}

// Complex Subtraction
Public complex complex_minus (Complex C)
{
// Obtain the real part after the subtraction operation
Double complex_real = This. complex_real-C. complex_real;

// Obtain the virtual part after the subtraction operation
Double complex_imagin = This. complex_imagin-C. complex_imagin;

// Return a plural class
Return new complex (complex_real, complex_imagin );
}

// Multiplication
Public complex complex_multi (Complex C)
{
// Obtain the real part after Multiplication
Double complex_real = This. complex_real * C. complex_real-This. complex_imagin * C. complex_imagin;

// Obtain the imaginary part after Multiplication
Double complex_imagin = This. complex_real * C. complex_imagin + this. complex_imagin * C. complex_real;

// Return a plural class
Return new complex (complex_real, complex_imagin );
}

// Division calculation result (a + bi)/(C + DI) = (a + bi) (C-DI)/(C + DI) (C-DI)
Public complex complex_divide (Complex C)
{
// Obtain the value of (C + DI) (C-DI)
Double D = C. complex_real * C. complex_real + C. complex_imagin * C. complex_imagin;

// Obtain the real part after division.
Double complex_real = (this. complex_real * C. complex_real + this. complex_imagin * C. complex_imagin)/d;

// Obtain the virtual part after division.
Double complex_imagin = (this. complex_real * (-C. complex_imagin) + this. complex_imagin * C. complex_real)/d;

// Return a plural class
Return new complex (complex_real, complex_imagin );
}
}

Running result:

Copy code The Code is as follows: Addition Calculation Result: 3 + 3I
Subtraction result:-1 +-1i
Multiplication result: 0 + 4I
Division calculation result: 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.