331929879

          復(fù)數(shù)運算

          class Complex{
           double real;
              double image;

              public Complex add(Complex another){
            Complex res = new Complex();
            res.real = real + another.real;
            res.image = image + another.image;
            return res;
              }
              
           public Complex minus(Complex another){// 當(dāng)前復(fù)數(shù)與方法參數(shù)another相減;
            Complex r = new Complex();
            r.real = real - another.real;
            r.image = image - another.image;
            return r;
           }
           
           public Complex multi(Complex another){ //當(dāng)前復(fù)數(shù)與方法參數(shù)another相乘(注意復(fù)數(shù)相乘); 
            Complex r = new Complex();
            r.real = real * another.real - image * another.image;
            r.image = real * another.image + image * another.real ;
            return r;
           }
           
           public void disp(){
            if( image == 0 ){
             System.out.print(real);
            }
            else
            {
             System.out.print(real + " + " + image + "i");
            }
           }
           
           public Complex(double real , double image){
            this.real = real;
            this.image = image ;
           }
           public Complex(){
            this(0 , 0);
           }
           
           public static void main (String[] args) {
            Complex c1 = new Complex(2 , 3);
            Complex c2 = new Complex(5 , 4);
            Complex res;
            
            c1.disp();
            System.out.print("  +   ");
            c2.disp();
            System.out.print("  =   ");
            c1.add(c2).disp();
            System.out.println();
            
            c1.disp();
            System.out.print("  -   ");
            c2.disp();
            System.out.print("  =   ");
            c1.minus(c2).disp();
            System.out.println();
            
            
            c1.disp();
            System.out.print("  *   ");
            c2.disp();
            System.out.print("  =   ");
            c1.multi(c2).disp();
            System.out.println();
              }
          }

          posted on 2007-10-12 09:13 小飛 閱讀(281) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 昆山市| 彭州市| 桑植县| 分宜县| 寻乌县| 河曲县| 大同县| 崇阳县| 沛县| 彩票| 上虞市| 海南省| 张家口市| 横峰县| 凯里市| 剑河县| 毕节市| 那曲县| 彰化市| 房山区| 吉木乃县| 大石桥市| 新田县| 莱西市| 如东县| 麻阳| 枣庄市| 秦安县| 黄平县| 集贤县| 明星| 茂名市| 临泉县| 轮台县| 高要市| 黑水县| 襄汾县| 桐城市| 榆林市| 庐江县| 清水县|