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); } }