程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net(c#)復數類(復數加減乘除四則運算)

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

編輯:ASP.NET基礎
我的一個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 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved