Blue Bridge Cup training algorithm P1103
Programming two complex operations. There are two complex and then their operation formula is:
Requirements: (1) defines a structure type described complex.
(2) addition, subtraction, multiplication and division among a plurality of functions respectively not be realized.
(3) The method must be used to structure the result of a function pointer is returned.
Description: The user input: an operational sign (+, -, *, / ) abc d.
Output: a + bi, the output regardless of a, b is less than or equal to zero are by the format of the output, the output a, b are retained two.
Input:
- 2.5 3.6 1.5 4.9
Output:
1.00 ± 1.30i
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Java中的复数运算
Scanner sc=new Scanner(System.in);
char s;
s=sc.next().charAt(0);
double a,b,c,d;
a= sc.nextDouble();
b= sc.nextDouble();
c= sc.nextDouble();
d= sc.nextDouble();
if(s=='+')
System.out.printf("%.2f+%.2fi",a+c,b+d);
if(s=='-')
System.out.printf("%.2f+%.2fi",a-c,b-d);
if(s=='*')
System.out.printf("%.2f+%.2fi",a*c-b*d,a*d+b*c);
if(s=='/')
System.out.printf("%.2f+%.2fi",(a*c+b*d)/(c*c+d*d),(b*c-a*d)/(c*c+d*d));
System.out.println();
}
}