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)航:
           
          主站蜘蛛池模板: 岫岩| 和静县| 油尖旺区| 温宿县| 房产| 灵台县| 周宁县| 罗田县| 青田县| 当阳市| 大竹县| 年辖:市辖区| 盘山县| 邹城市| 临漳县| 大港区| 股票| 甘谷县| 郴州市| 丹寨县| 赣州市| 洛宁县| 婺源县| 邳州市| 莲花县| 青海省| 新疆| 师宗县| 昌宁县| 北宁市| 高碑店市| 拉孜县| 平江县| 安平县| 眉山市| 中卫市| 万荣县| 芮城县| 潍坊市| 临沂市| 乐业县|