Home > Back-end >  The Android calculator code scanning (expression) who can give me analyze! Be urgent!
The Android calculator code scanning (expression) who can give me analyze! Be urgent!

Time:09-24

Import the Java. Util. LinkedList;
Import the Java. Util. List;

Public class GetValue {

Public String bracke_dispose (String String) {
//handle the expression in the brackets, train of thought,
//1. Encountered are parentheses to keep it in a string of index in a linked list & lt; In head & gt;
//2. Hit the first counter will deal with the expression in brackets brackets & lt; Remove the first element on the list, and then based on these two indexes to take out the expression of the will call a special processing
//log, ln, sin, cos, tan, etc special_dispose function, returns a free expression (add, subtract, multiply or divide in alg_dispose function called & gt;
//3. The function returns a may also contain the log after processing, ln, sin, cos, tan, etc but no bracket expressions;

Int flag=0, flag1=0, I=0;
Int leng=string. The length ();
List List=new LinkedList (a);
Character c;
String STR="", TMP=" ", tmp3="", tmp4=" ";

While (true) {
If (I & gt;=leng)
break;

C=string. CharAt (I);

If (' ('==c)
List. Add (0, I);

If (a ') '==c) {
If (0==list. The size ()) {
System. The out. Println (" error ");
return "error";
}

STR=string, the substring (list. Get (0) + 1, I);
TMP=this. Special_dispose (STR);
TMP=this. Alg_dispose (STR);
If (" error ". The equals (TMP))
return "error";

Tmp3=string, the substring (0, the list. The get (0));
Tmp4=string, the substring (I + 1, leng);
String tmp3=+ TMP + tmp4;

Flag=STR. Length () + 2;
Flag1=TMP. The length ();
Leng +=(flag1 - flag);
I +=(flag1 - flag);
List. Remove (0);
}
i++;
}

If (a list. The size ()!=0) {
System. Out.println (" error ");
return "error";
}
Return a string;
}

Public String special_dispose (String String) {
//used to handle special expression;

If (" error ". The equals (string)) {
return "error";
}
String=the dispose (string, "sin");
String=the dispose (string, "cos");
String=the dispose (string, "tan");
String=the dispose (string, "log");
String=the dispose (string, "ln");
String=the dispose (string, "!" );
String=the dispose (string, "^");
String=the dispose (string, ""));
String=the dispose (string, "e");
Return a string;
}

Public String alg_dispose (String String) {

//processing addition, subtraction, multiplication, and divisionString=special_dispose (string);

If (" error ". The equals (string)) {
return "error";
}

Character c;
String STR="";
List List=new LinkedList (a);
Double TMP=0;
Boolean add_flag=false;

for (int i=0; i C=string. CharAt (I);
if (! Judge. Paiduan (c)) {
STR +=c;
} else {
If (STR) length ()!=0)
List. The add (new Double (STR));

STR="";
The switch (c) {
Case: '+'
If (2==list. The size ()) {
If (add_flag) {
TMP=list. Get (0) + list. Get (1);
list.clear();
List. The add (TMP);
} else {
TMP=list. Get (0) - list. Get (1);
list.clear();
List. The add (TMP);
}
break;
}
Add_flag=true;
break;
In case the '-' :
If (2==list. The size ()) {
If (add_flag) {
TMP=list. Get (0) + list. Get (1);
list.clear();
List. The add (TMP);
} else {
TMP=list. Get (0) - list. Get (1);
list.clear();
List. The add (TMP);
}
Add_flag=false;
break;
}
break;
Case 'x' :
If (I==(string length () - 1))
return "error";
while (! (I==(string length () - 1))
& & ! Judge. Paiduan ((c=string. The charAt (I + 1)))) {
STR +=c;
i++;
}

If (STR) length ()!=0)
List. The add (new Double (STR));
STR="";

If (a list. The size ()==2) {
TMP=list. Get (0) * the list in the get (1);
list.clear();
List. The add (0, TMP);
} else {
TMP=list. Get (2) * the list in the get (1);
List. Remove (2);
List. Remove (1);
List. The add (TMP);
}
break;
Case 'present' :
If (I==(string length () - 1))
return "error";
while (! (I==(string length () - 1))
& & ! Judge. Paiduan ((c=string. The charAt (I + 1)))) {
STR +=c;
i++;
}

If (STR) length ()!=0)
List. The add (new Double (STR));
STR="";

If (a list. The size ()==2) {
TMP=list. Get (0)/list. Get (1);
list.clear();
List. The add (0, TMP);
} else {
TMP=list. The get/list. (2) get (1);
List. Remove (2);
List. Remove (1);
List. The add (TMP);
}
break;
Default:
break;
}
}
}

If (STR) length ()!=0)
List. The add (new Double (STR));

If (2==list. The size ()) {
If (add_flag) {
TMP=list. Get (0) + list. Get (1);
list.clear();
List. The add (TMP);
} else {
TMP=list. Get (0) - list. Get (1);
list.clear();
List. The add (TMP);
}
}
If (a list. The size ()!=0)
String="" + list. Get (0);

Return a string;
}

Public String get_r_digit (String String, int index) {
//get the index number to the right of the first;
String TMP="";
Int length=string. The length (), j=index;
Character Character;

If (index & gt;=length)
TMP="error".

While (true) {
If (j==length)
break;

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related