Package test;
/*
Calculate the value of a mathematical expression
*/
Public class Cal {
Public int CAL (string Str ){
Int result = 0;
Int value1, value2;
STR = Str. Trim ();
STR = Str. replaceall ("","");
While (true ){
Int index1 = Str. indexof ("*");
Int index2 = Str. indexof ("/");
If (index1 =-1 & index2 =-1)
Break;
Int Index = 0;
If (index1> index2 & index2! =-1 ){
Index = index2;
}
If (index2 =-1 ){
Index = index1;
}
If (index1 =-1 ){
Index = index2;
}
If (index2> index1 & index1! =-1 ){
Index = index1;
}
{
String sub1 = NULL;
String sub2 = NULL;
Value1 = 0;
For (Int J = index-1; j> = 0; j --){
Char c = Str. charat (j );
If (C> = '0' & C <= '9 '){
If (j = 0 ){
Sub1 = Str. substring (J, index );
Value1 = integer. parseint (sub1 );
}
Continue;
}
Else {
Sub1 = Str. substring (J + 1, index );
Value1 = integer. parseint (sub1 );
Break;
}
}
Value2 = 0;
For (Int J = index + 1; j <Str. Length (); j ++ ){
Char c = Str. charat (j );
If (C> = '0' & C <= '9 '){
If (j = Str. Length ()-1 ){
Sub2 = Str. substring (index + 1); // modify it to index + 1
Value2 = integer. parseint (sub2 );
}
Continue;
}
Else {
Sub2 = Str. substring (index + 1, J );
Value2 = integer. parseint (sub2 );
Break;
}
}
Int temp = (index1> index2 & index2! =-1) | index1 =-1? Value1/value2: value1 * value2;
STR = (index1> index2 & index2! =-1) | index1 =-1?
Str. Replace (sub1 + "/" + sub2, String. valueof (temp): Str. Replace (sub1 + "*" + sub2, String. valueof (temp ));
}
}
// +-
While (true ){
Int index1 = Str. indexof ("+ ");
Int index2 = Str. indexof ("-");
If (index1 =-1 & (index2 =-1 | index2 = 0) // solves the "-" problem at the beginning.
Break;
Int Index = 0;
If (index1> index2 & index2! =-1 ){
Index = index2;
}
If (index2 =-1 | index2 = 0) {// solve the problem that the start is "-".
Index = index1;
}
If (index1 =-1 ){
Index = index2;
}
If (index2> index1 & index1! =-1 ){
Index = index1;
}
{// Query operations
String sub1 = NULL;
String sub2 = NULL;
Value1 = 0;
For (Int J = index-1; j> = 0; j --){
Char c = Str. charat (j );
If (C> = '0' & C <= '9 '){
If (j = 0 ){
Sub1 = Str. substring (J, index );
Value1 = integer. parseint (sub1 );
}
Continue;
}
Else {
If (j = 0)
Sub1 = Str. substring (J, index );
Else
Sub1 = Str. substring (J + 1, index); // fix the "-" problem.
Value1 = integer. parseint (sub1 );
Break;
}
}
Value2 = 0;
For (Int J = index + 1; j <Str. Length (); j ++ ){
Char c = Str. charat (j );
If (C> = '0' & C <= '9 '){
If (j = Str. Length ()-1 ){
Sub2 = Str. substring (index + 1); // modify it to index + 1
Value2 = integer. parseint (sub2 );
}
Continue;
}
Else {
Sub2 = Str. substring (index + 1, J );
Value2 = integer. parseint (sub2 );
Break;
}
}
// To solve the "-" problem, index2 in the following two rows! =-1 to index2> 0
Int temp = (index1> index2 & index2> 0) | index1 =-1? Value1-value2: value1 + value2;
STR = (index1> index2 & index2> 0) | index1 =-1?
Str. Replace (sub1 + "-" + sub2, String. valueof (temp): Str. Replace (sub1 + "+" + sub2, String. valueof (temp ));
}
}
Result = integer. parseint (STR );
Return result;
}
Public static void main (string [] ARGs ){
String STR = "3-4*5 + 3 ";
Cal = new Cal ();
System. Out. println (Cal. Cal (STR ));
}
}