331929879

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

          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 小飛 閱讀(277) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 图木舒克市| 扶风县| 平阳县| 清苑县| 冕宁县| 红桥区| 金乡县| 德江县| 乃东县| 海淀区| 黎城县| 乌鲁木齐县| 吐鲁番市| 天等县| 华安县| 安平县| 贞丰县| 淮阳县| 沧源| 栾川县| 临西县| 鄂州市| 崇文区| 平潭县| 隆安县| 环江| 中牟县| 双柏县| 射阳县| 韶关市| 渭南市| 台北县| 新绛县| 科技| 石楼县| 出国| 通渭县| 昌都县| 紫阳县| 治县。| 滨海县|