"Programming Marathon" "017-emacs Calculator"

Source: Internet
Author: User

"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:
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"

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.