Enter an expression without parentheses, the number is less than 0-9, the output evaluates, and all the intermediate results are turned into shaping. Example: Input: 3+8x2/9-2 output: 2
Source: Internet
Author: User
public class practiceutil { public static void main (String[] args) { string s = "3+8x2/9-2 "; int result = getmyret (s); SYSTEM.OUT.PRINTLN ("Final result:" + result); } public static int getmyret (STRING&NBSP;S1) { int len = s1.length (); List<String> list = new ArrayList<String> (); for (int i = 0; i < len; i++) &NBSP;&NBsp list.add (S1.charAt (i) + ""); system.out.println ("List--->" + list) ; for (int j = 0; j < list.size (); j++) { if (List.get (j). Equals ("X")) { int ji = integer.parseint (List.get ( j - 1)) * integer.parseint (List.get (j + 1)); list.adD (j - 1, ji + "");// the JI into the previous position of the original X, the original back shift. Move backward from 8 list.remove (j);// Delete 8;remove is the forward move after deleting the current position, so x to the subscript position of J. List.remove (j);// Delete x list.remove (j);// Delete 9 system.out.println ("List--x" + list);// List--x after->[3, +, 16, &nbsP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;/,&NBSP;9,&NBSP;-,&NBSP;2,&NBSP;] j--;// equivalent to this cycle of the wood has a beating next subscript, because immediately to participate in the operation of Ji, rather than skip } else if (List.get (j). Equals ("/")) { int shang = integer.parseint (List.get (j - 1)) / Integer.parseint (List.get (j + 1)); list.add (j - 1, shang + ""); &Nbsp; list.remove (j); list.remove (j); list.remove (j) ; System.out.println ("list--/" + list);// list--/after->[3, +, 1, // -, 2, ] j--; } } for (Int k = 0; k < list.size (); k++) {// This time is the new size if ( List.get (k). Equals ("+")) { int he = integer.parseint (List.get (k - 1)) + integer.parseint (List.get (k + 1)); list.add (k - 1, he + "");
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.