"Programming Marathon algorithm Directory"
" 017-emacs Calculator" "Project download >>>"
1 Topic Description
Emacs is called God's editor, and it comes with a calculator. Unlike other calculators, it is based on the suffix expression, which is the operator after the operand. For example, "2 3 +" is equivalent to the infix expression of "2 + 3".
Ask you to implement a suffix expression for the calculator.
1.1 Input Description:
The input contains multiple sets of data.
Each set of data consists of two rows: the first line is a positive integer n (3≤n≤50), and the second row contains n a list of numbers and operators.
"+-*/" is subtraction arithmetic, where division is divided evenly, that is, "5/3=1".
1.2 Output Description:
corresponding to each set of data, output their operation results.
1.3 Input Example:
32 3 +52 2 + 3 *52 2 3 + *
1.4 Output Example:
51210
2 ideas for solving problems
Because the input is a suffix (also called inverse polish), you can use a stack to store the input operand, when the operand is encountered, the operand is stored in the stack. When the operator is encountered, two operands are popped from the stack, and the result is stored in the stack again. Loop the above operation until all the inputs have been processed, and the last stack has only one element, which is the result of the request.
3 Algorithm Implementation
ImportJava.util.ArrayDeque;ImportJava.util.Deque;ImportJava.util.Scanner;/** * Author: Wang Junshu * time:2016-05-12 07:44 * CSDN:HTTP://BLOG.CSDN.NET/DERRANTCM * github:https://github.com/wang-ju N-chao * declaration:all rights Reserved!!! */ Public class Main { Public Static void Main(string[] args) {Scanner Scanner =NewScanner (system.in);//Scanner Scanner = new Scanner (Main.class.getClassLoader (). getResourceAsStream ("Data.txt")); while(Scanner.hasnext ()) {intnum = Scanner.nextint (); string[] suffix =NewString[num]; for(inti =0; i < num; i++) {Suffix[i] = Scanner.next (); } System.out.println (Calculate (suffix)); } scanner.close (); }/** * Calculation Inverse Polish * * @param suffix Reverse Polish * @return Results */ Private Static int Calculate(string[] suffix) {Deque<integer> stack =NewArraydeque<> (); for(String S:suffix) {Charc = S.charat (0);//If it is an operator if(s.length () = =1&& (c = =' + '|| c = ='-'|| c = =' * '|| c = ='/')) {intb = Stack.removefirst ();intA = Stack.removefirst (); Stack.addfirst (Calculate (A, B, c)); }//operand in stack Else{Stack.addfirst (Integer.parseint (s)); } }returnStack.removefirst (); }/** * Calculation of ACB * * @param a operand * @param b operand * @param c operator * @r Eturn Results * / Private Static int Calculate(intAintBCharc) {Switch(c) { Case ' + ':returnA + b; Case '-':returnA-B; Case ' * ':returnA * b; Case '/':returnA/b;default:// do nothing}Throw NewIllegalArgumentException ("operator can only be (+-*/):"+ c); }}
4 Test Results
5 Other information
Because Markddow is not good for editing, uploading a picture of a document for reading. PDF and Word documents can be "downloaded >>>" on GitHub.
"Programming Marathon" "017-emacs Calculator"