A small program that computes the plural arithmetic operations

Source: Internet
Author: User

// The input format of the application is:
// * 3 + 4I 5 + 8i
// Operator + N spaces + the first plural number + N spaces + the second plural number
/*
* Auther starshus
*
* Date 04/11/20
*/
Import java. Io. bufferedreader;
Import java. Io. inputstreamreader;
// 5.7.10
Public class command
{
Private string CommandLine;
Public command (string line) // constructor to obtain the input
{
CommandLine = line. Trim (); //. Trim () is used to remove spaces on both sides.
}
Public String getcommand () // get an operator in + ,-, *,/
{
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
// Extract the number a first
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 (index3> = 0)
A = A. substring (0, index3 );
} Else a = "";
Return;
}
// 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 = A-C;
Imaginarypart = B-d;
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 * C-B * D;
Imaginarypart = A * D + B * C;
If (imaginarypart> 0)
Return (realpart + "+" + imaginarypart + "I ");
Else
Return (realpart + "" + imaginarypart + "I ");
}
Public static string Division (double A, double B, double C, double D) // Division
{
Double realpart, imaginarypart;
Realpart = (A * C + B * D)/(c * C + D * D );
Imaginarypart = (B * C-A * D)/(c * C + D * D );
If (imaginarypart> 0)
Return (realpart + "+" + imaginarypart + "I ");
Else
Return (realpart + "" + imaginarypart + "I ");
}
Public static void main (string [] ARGs) // master Method
{
System. Out. println ("Welcome! Press 'q' or 'q' to quit .");
System. Out. println ("You can use this program like the follow example :");
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); // use the command class to generate an object
String cmd = commander. getcommand ();
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. valueof (c). 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 shoshould 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 '-'.");
}
}

}

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.