Algorithm Note _156: Algorithm increases 6-17 complex arithmetic (Java)

Source: Internet
Author: User
Tags arithmetic

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.