我的一個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