Reference answers for thinking questions (analyzing mathematical expressions) of a day

Source: Internet
Author: User

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 ));
}

}

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.