Java澶嶆暟鐨勫姞涔橀櫎杩愮畻

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java澶嶆暟鐨勫姞涔橀櫎杩愮畻相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/turn' title='turn'>turn   new   rate   dep   a*   鎸囨   rgs   lex   javac   

//涓昏鏄闆剁殑澶勭悊锛屾湁浠€涔堜笉瀵圭殑鍦版柟娆㈣繋鎵硅瘎鎸囨锛屼竴璧疯繘姝?br />class complex{
    double a,b;
     public String toString() {
        return("瀹為儴锛?+a+"铏氶儴:"+b);
    }
    void add(complex x,complex y) {
        double m,n;
        m = x.a+y.a;
        n = x.b+y.b;
        print(m,n);
    }
   void mult(complex x,complex y) {
        double m,n;
        n = x.b*y.a+x.a*y.b;
        m =x.a*y.a-x.b*y.b;
        print(m,n);
    }
   void dep(complex x,complex y) {
        double n = (x.a*y.a-x.b*y.b)/(y.a*y.a+y.b*y.b);
        double m = (x.b*y.a+x.a*y.b)/(y.a*y.a+y.b*y.b);
        if(y.a==0&&y.b==0)
            System.out.println("闄ゆ硶鏃犳剰涔塡n");
        else
        {
            print(m,n);
        }
    }
    void print(double m,double n) {
        if(m!=0)
        {
            if(n>0) {
                System.out.println(m+"+");
                System.out.println(n+"i");
            }
            else if(n<0) {
                System.out.print(m);
                System.out.println(n+"i");
            }
            else
                System.out.println(m);
        }
        else {
            if(n!=0) {
                System.out.println(n+"i");
            }
            else
                System.out.println(m);
        }
    }
}
public class Javacomplex {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         complex n1,n2,n;
         n1 = new complex();
         n2 = new complex();
         n = new complex();
         n1.a = 1;n1.b=2;
         n2.a = -1;n2.b = 1;
         System.out.println(n1.toString());
         System.out.println(n2.toString());
         n.add(n1, n2);
         n.mult(n1, n2);
         n.dep(n1, n2);
         
    }

}

 

以上是关于Java澶嶆暟鐨勫姞涔橀櫎杩愮畻的主要内容,如果未能解决你的问题,请参考以下文章

涓嶇敤鍔犲噺涔橀櫎鍋氬姞娉昿ython

鍥涘垯杩愮畻

澶嶄範涓€涓?Python涓夊厓杩愮畻

C璇█瀹炵幇鍥涘垯杩愮畻

Java鎬荤粨

[51NOD]2 3 5 7鐨勫€嶆暟锛堝鏂ュ師鐞嗭級