asp.net(c#)複數類(複數加減乘除四則運算)

來源:互聯網
上載者:User

我的一個JAVA作業,把它改寫成asp.net(c#)了 複製代碼 代碼如下: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("加法運算結果:" + complex_a.complex_add(complex_b).ToString() + "<br />");
Response.Write("減法運算結果:" + complex_a.complex_minus(complex_b).ToString() + "<br />");
Response.Write("乘法運算結果:" + complex_a.complex_multi(complex_b).ToString() + "<br />");
Response.Write("除法運算結果:" + complex_a.complex_divide(complex_b).ToString());
}
//design by 阿會楠 來自:搜尋吧 sosuo8.com
public class complex
{
//複數中的實部
private double complex_real;
//複數中的虛部
private double complex_imagin;

//建構函式
public complex(double r, double i)
{
complex_real = r;
complex_imagin = i;
}

//重寫ToString()方法
public override string ToString()
{
return this.complex_real + "+" + this.complex_imagin + "i";
}

//複數加法運算
public complex complex_add(complex c)
{
//取得加法運算後的實部
double complex_real = this.complex_real + c.complex_real;

//取得加法運算後的虛部
double complex_imagin = this.complex_imagin + c.complex_imagin;

//返回一個複數類
return new complex(complex_real,complex_imagin);
}

//複數減法運算
public complex complex_minus(complex c)
{
//取得減法運算後的實部
double complex_real = this.complex_real - c.complex_real;

//取得減法運算後的虛部
double complex_imagin = this.complex_imagin - c.complex_imagin;

//返回一個複數類
return new complex(complex_real, complex_imagin);
}

//乘法運算
public complex complex_multi(complex c)
{
//取得乘法運算後的實部
double complex_real = this.complex_real * c.complex_real - this.complex_imagin * c.complex_imagin;

//取得乘法運算後的虛部
double complex_imagin = this.complex_real * c.complex_imagin + this.complex_imagin * c.complex_real;

//返回一個複數類
return new complex(complex_real, complex_imagin);
}

//除法運算結果 (a+bi)/(c+di)=(a+bi)(c-di)/(c+di)(c-di)
public complex complex_divide(complex c)
{
//取得(c+di)(c-di)的值
double d = c.complex_real * c.complex_real + c.complex_imagin * c.complex_imagin;

//取得除法運算後的實部
double complex_real = (this.complex_real * c.complex_real + this.complex_imagin * c.complex_imagin) / d;

//取得除法運算後的虛部
double complex_imagin = (this.complex_real * (-c.complex_imagin) + this.complex_imagin * c.complex_real) / d;

//返回一個複數類
return new complex(complex_real, complex_imagin);
}
}

運行結果:

複製代碼 代碼如下:加法運算結果:3+3i
減法運算結果:-1+-1i
乘法運算結果:0+4i
除法運算結果:0.5+0i
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.