若两个复数分别为:c1=x1+y1i和c2=x2+y2i,则它们的乘积为c1×c2=(x1x2−y1y2)+(x1y2+x2y1)i。
本题要求实现一个函数计算两个复数之积。
函数接口定义: double result_real, result_imag; void complex_prod( double x1, double y1, double x2, double y2 ); 其中用户传入的参数为两个复数x1+y1i和x2+y2i;函数complex_prod应将计算结果的实部存放在全局变量result_real中、虚部存放在全局变量result_imag中。
package taotao.Experiment_six; import java.util.Scanner; public class Demo09 { static double result_real,result_imag; public static void complex_prod(double x1,double y1,double x2,double y2){ result_real=x1*x2-y1*y2; result_imag=x1*y2+x2*y1; } public static void main(String[] args) { double real1,imag1,real2,imag2; Scanner scanner=new Scanner(System.in); real1=scanner.nextDouble(); imag1=scanner.nextDouble(); real2=scanner.nextDouble(); imag2=scanner.nextDouble(); scanner.close(); complex_prod(real1,imag1,real2,imag2); System.out.println(result_real+"+"+result_imag+"i"); } }