A small program for calculating complex number

xiaoxiao2021-03-06  64

// Use the input format of the program: // * 3 4i 5 8i // operator n space first plural n space second plum / ** Auther starshus ** Date 04 / 11/20 * / import java.io.BufferedReader; import java.io.InputStreamReader; // 5.7.10public class Command {private String commandLine; public Command (String line) // constructor, to give input {commandLine = line.trim (); //. Trim () is used to remove the space between the two sides} PUBLIC STRING GETCOMMAND () // A operator {string cmd = ""; int index = commandline.indexof '); If (index> = 0) cmd = commandline.substring (0, index); Else cmd = commandLine; return cmd; //. TouPpercase ();} // assume that the input format is a BI C Di // First extract digital a public string geta () {string a = ""; int index1 = commandline.indexof (''); if (index1> = 0) {a = commandline.substring (INDEX1 1, CommandLine. Length ()). Trim (); int index2 = a.indexof (''); if (index2> = 0) a = a.substring (0, index2); int index3 = a.indexof (' '); IF (INDEX 3> = 0) a = a.substring (0, index3);} else a = ""; return a;} // extract b public string getb () {string b = ""; int index1 = CommandLine.indexof '); If (index1> = 0) {b = commandline.substring (INDEX1 1, CommandLine.Length ()). Trim (); int index2 = B.Indexof (' '); if (index2> = 0 ) B = b.substring (0, index2); int index3 = B.INDEXOF (' '); int index4 =

B.indexOf ('I'); if ((INDEX3 1) == Index4) {b = "1"; return b;} else if (index3> = 0 && index4> = 0) B = B.Substring (INDEX3 1, Index4); Else B = "0";} else b = ""; return b;} // extract C public string getc () {string c = ""; int index1 = commandline.indexof (''); IF (index1> = 0) {c = commandline.substring (index1 1, commandline.Length ()). Trim (); int index2 = c.indexof (''); if (index2> = 0) c = c . Substring (INDEX2 1, C.LENGTH ()). Trim (); int index3 = C.indexOf (' '); if (index3> = 0) c = c.substring (0, index3);} else C = ""; return c;} // extract D public string getd () {string d = ""; int index1 = commandline.indexof (''); if (index1> = 0) {d = commandline.substring Index1 1, CommandLine.Length ()). Trim (); int index2 = d.indexof (''); IF (index2> = 0) D = D.Substring (INDEX2 1, D.Length ()). Trim (); int index3 = D.indexof (' '); int index4 = D.indexof ('I' ); If ((index3 1) == index4) {d = "1"; return d;} else if (index3> = 0 && index4> = 0) D = D.SUBSTRING (INDEX3 1, INDEX4); ELSE D = "0";} else d = "

"RETURN D; PUBLIC STATIC STRING ADD (Double A, Double B, Double C, Double D) // addition {Double RealPart, ImaginaryPart; RealPart = A C; ImaginaryPart = B D; IF (ImaginaryPart> 0) Return (RealPart " ImaginaryPart " I "); Else Return (RealPart " ImaginaryPart "I");} public static string sub (double A, Double B, Double C, Double D) // subtraction {DOUBLE realPart, imaginaryPart; realPart = ac; imaginaryPart = bd; if (imaginaryPart> 0) return (realPart " " imaginaryPart "i"); else return (realPart "" imaginaryPart "i");} public static String mult (Double A, Double B, Double C, Double D) // Multiplication {Double Realpart, ImaginaryPart; RealPart = a * CB * D; imaginaryPart = a * d b * c; ife (imaginarypart> 0) Return (RealPart " " ImaginaryPart "I"); Else Return (RealPart " ImaginaryPart " I ");} public static string di Vision (Double A, Double B, Double C, Double D) // division {Double RealPart, ImaginaryPart; RealPart = (A * C B * D) / (C * C D * D); imaginaryPart = (b * Ca * d) / (C * C D * D); if (ImaginaryPart> 0) Return (RealPart " " ImaginaryPart "I"); Else Return (RealPart " ImaginaryPart " I ");} public Static void main (string [] args) // main method {system.out.println ("Welcome! Press' q"

Or 'q' to quit. "); System.out.Println (" You can use this program: "); system.out.println (" * 20 30i 15 35i "); System.out .println (" 20 -30i 15 -35i"); System.out.Println ("The first character can be '*' or '/' or ' ' or '-'); for ;) {System.out.print ( "$"); String inputLine; try {BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); inputLine = in.readLine ();} catch (Exception exc) {System .out.println ("Your Input can't be used."); Continue;} command commander = new command (inputline); // Generate objects String cmd = commander.getCommand (); if (cmd.equals); if (cmd.equals); ("Q") || cmd.equals ("q")) Break; Else IF (cmd.equals (" ") || cmd.equals ("-") || cmd.equals ("*") | | cmd.equals ("/")) {Double A1, B1, C1, D1; TRY { String a = commander.geta (); a1 = double.Valueof (a) .doublevalue (); string b = commander.getb (); b1 = double.valueof (b) .doubleValue (); string c = commander.getc (); C1 = double value () .doublevalue (); string d = commander.getd (); d1 = double.valueof (d) .doublevalue ();} catch (exception eXC) {system.out.println ("Sorry, Your Input Can '

T be used. "); System.out.Println (" IT Should Be Looks Like: * 1 2i 3 4i "); Continue;} if (cmd.equals (" ")) {system.out.println ("The Result IS:" Add (A1, B1, C1, D1));} else IF (cmd.equals ("-")) {system.out.println ("the result is:" SUB (A1 , B1, C1, D1));} else IF (cmd.equals ("*")) {system.out.println ("The result is:" MULT (A1, B1, C1, D1));} else IF (cmd.equals ("/")) {system.out.println ("The Result is:" Division (A1, B1, C1, D1));}}}} else system.out.println ("Sorry, The First Character Can Be '*' or '/' OR ' ' or '-'. ");}}}

转载请注明原文地址:https://www.9cbs.com/read-89372.html

New Post(0)