Algorithm Training P1103time limit: 1.0s memory limit: 256.0MB
The programming implements the operation of two complex numbers. There are two complex numbers and they are calculated as:
Requirements: (1) Define a struct type to describe the complex number.
(2) The addition, subtraction, multiplication, and division of the complex numbers are implemented using unused functions respectively.
(3) The method of the struct pointer must be used to return the result of the function's calculation.
Description: User input: Operation symbol (+,-, *,/) a B c d.
Output: A+bi, output, regardless of a, B is less than 0 or equal to 0 is output in this format, the output, A, B are reserved two bits.
Input:
-2.5 3.6 1.5 4.9
Output:
1.00+-1.30i
ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner sc=NewScanner (system.in); Chars; S=sc.next (). CharAt (0); Doublea,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 (); }}
-----------------------
About plural
--------------------------------------
The addition of complex numbers is carried out according to the following rules : set z1=a+bi,z2=c+di is any two complex number,
Then their and is (A+BI) + (C+di) = (a+c) + (b+d) i.
The subtraction of complex numbers is carried out according to the following rules : set z1=a+bi,z2=c+di is any two complex number,
Then their difference is (A+BI)-(C+di) = (a-c) + (b-d) i.
Set z1=a+bi,z2=c+di (a,b,c,d∈ R) is any two complex number, then their product (A+BI) (C+di) = (AC-BD) + (Bc+ad) i.
Division operation rules :
① complex a+bi (a , b ∈ r) divided by c+di (c , d ∈ r) x+yi (x Span style= "font-family: ' Courier New ';" >y ∈ r) ,
That is (a+bi)÷(c+di) =x+yi Denominator has physical and chemical
The denominator is physical.
∵(X+yi) (C+di) = (Cx-dy) + (dx+cy) i.
∴(Cx-dy) + (Dx+cy) i=a+bi.
The definition of cx-dy=a dx+cy=b by the plural equality
Solution to this equation set, get x= (AC+BD)/(c^2+d^2) y= (bc-ad)/(c^2+d^2)
So there is :(A+bi)/(C+di) = (AC+BD)/(c^2+d^2) + (BC-AD)/(c^2+d^2) I
Algorithm Training P1103