|
復(fù)制代碼 代碼如下:
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("加法運(yùn)算結(jié)果:" + complex_a.complex_add(complex_b).ToString() + "<br />");
Response.Write("減法運(yùn)算結(jié)果:" + complex_a.complex_minus(complex_b).ToString() + "<br />");
Response.Write("乘法運(yùn)算結(jié)果:" + complex_a.complex_multi(complex_b).ToString() + "<br />");
Response.Write("除法運(yùn)算結(jié)果:" + complex_a.complex_divide(complex_b).ToString());
}
//design by 阿會(huì)楠 來(lái)自:搜索吧 sosuo8.com
public class complex
{
//復(fù)數(shù)中的實(shí)部
private double complex_real;
//復(fù)數(shù)中的虛部
private double complex_imagin;
//構(gòu)造函數(shù)
public complex(double r, double i)
{
complex_real = r;
complex_imagin = i;
}
//重寫(xiě)ToString()方法
public override string ToString()
{
return this.complex_real + "+" + this.complex_imagin + "i";
}
//復(fù)數(shù)加法運(yùn)算
public complex complex_add(complex c)
{
//取得加法運(yùn)算后的實(shí)部
double complex_real = this.complex_real + c.complex_real;
//取得加法運(yùn)算后的虛部
double complex_imagin = this.complex_imagin + c.complex_imagin;
//返回一個(gè)復(fù)數(shù)類(lèi)
return new complex(complex_real,complex_imagin);
}
//復(fù)數(shù)減法運(yùn)算
public complex complex_minus(complex c)
{
//取得減法運(yùn)算后的實(shí)部
double complex_real = this.complex_real - c.complex_real;
//取得減法運(yùn)算后的虛部
double complex_imagin = this.complex_imagin - c.complex_imagin;
//返回一個(gè)復(fù)數(shù)類(lèi)
return new complex(complex_real, complex_imagin);
}
//乘法運(yùn)算
public complex complex_multi(complex c)
{
//取得乘法運(yùn)算后的實(shí)部
double complex_real = this.complex_real * c.complex_real - this.complex_imagin * c.complex_imagin;
//取得乘法運(yùn)算后的虛部
double complex_imagin = this.complex_real * c.complex_imagin + this.complex_imagin * c.complex_real;
//返回一個(gè)復(fù)數(shù)類(lèi)
return new complex(complex_real, complex_imagin);
}
//除法運(yùn)算結(jié)果 (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;
//取得除法運(yùn)算后的實(shí)部
double complex_real = (this.complex_real * c.complex_real + this.complex_imagin * c.complex_imagin) / d;
//取得除法運(yùn)算后的虛部
double complex_imagin = (this.complex_real * (-c.complex_imagin) + this.complex_imagin * c.complex_real) / d;
//返回一個(gè)復(fù)數(shù)類(lèi)
return new complex(complex_real, complex_imagin);
}
}
運(yùn)行結(jié)果:
復(fù)制代碼 代碼如下:
加法運(yùn)算結(jié)果:3+3i
減法運(yùn)算結(jié)果:-1+-1i
乘法運(yùn)算結(jié)果:0+4i
除法運(yùn)算結(jié)果:0.5+0i
AspNet技術(shù):asp.net(c#)復(fù)數(shù)類(lèi)(復(fù)數(shù)加減乘除四則運(yùn)算),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。