Directory
1 Problem Description
2 Solutions
1 problem description Design complex number library, realize the basic complex subtraction operation.
typing only the real and imaginary parts, separated by a space, two complex numbers with operator separation, the output by A+bi format on the screen printing results. Participate in sample input and sample output.
note In special cases, the output string "error" cannot be computed. Sample Input2 4 *-3 2Sample Output-14-8iSample Input3-2 +-1 3Sample Output2+1i
2 Solutions
This problem is mainly in the division operation, the division operation is possible to obtain a decimal, so in the acquisition of input numbers, you have to use a double type to define, but the following code in the system score a set of data can not pass, changed several times, choose to abandon >~<
The specific code is as follows:
ImportJava.util.Scanner; Public classMain { Public voidGetResult (String A) {string result= ""; String[] Temp= A.split (""); DoubleA = integer.valueof (temp[0]); Doubleb = integer.valueof (temp[1]); Doublec = integer.valueof (temp[3]); DoubleD = integer.valueof (temp[4]); if(Temp[2].equals ("+") ) {a= A +C; C= B +D; } Else if(Temp[2].equals ("-") ) {a= A-C; C= B-D; } Else if(Temp[2].equals ("*")) { DoubleA1 =A; A= a*c-b*D; C= A1*d + b*C; } Else if(Temp[2].equals ("/")) { DoubleA1 =A; if(c*c + d*d = = 0) {System.out.println ("Error"); return; } A= (a*c + b*d)/(C*c + d*d); C= (b*c-a1*d)/(C*c + d*d); } if(!temp[2].equals ("/")) { intA1 = (int) A; intC1 = (int) C; if(C1 >= 0) Result= result + A1 + "+" + C1 + "I"; Elseresult= result + A1 + C1 + "I"; } Else { if(c >= 0) Result= result + A + "+" + C + "I"; Elseresult= result + A + C + "I"; } System.out.println (Result); } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); String A=In.nextline (); Test.getresult (A); }}
Algorithm Note _156: Algorithm increases 6-17 complex arithmetic (Java)