hdu1237 (Simple Calculator) LinkedList class implements stack and queue functions

Source: Internet
Author: User

Click to open link

Problem description reads a non-negative integer that contains only +,-, *,/, evaluates the expression, and computes the value of the formula.

The input test inputs contain several test cases, one row for each test case, and no more than 200 characters per line, separated by a space between integers and operators. There is no illegal expression. When only 0 o'clock input is completed in a row, the corresponding result is not output.

Output outputs 1 rows for each test case, that is, the value of the expression, exactly 2 digits after the decimal point.

Sample Input
1 + 24 + 2 * 5-7/110

Sample Output
3.0013.36

idea: use two sets to separate the symbols and numbers, the said set is because in the storage and calculation, that is, the use of the queue, but also used the idea of the stack, so the individual said with which is not accurate.

Specific operation: In the symbol, if met multiply or divide, then directly use this symbol to calculate the number of the two sides, which need to be the number of the last entry in the set of numbers to take out, that is, LIFO (Stack ), and then put the results in the digital stack, after such processing, all put out, is equivalent to only adding and minus two operations

, so that you can use the queue thinking, first-out , once in order to calculate.

Implementation code:

Package Collection;import Java.util.iterator;import Java.util.linkedlist;import java.util.scanner;public class P1237 {public static void main (string[] args) {Scanner sc=new Scanner (system.in); String str; String[] Strs;while (Sc.hasnext ()) {str=sc.nextline ();//Use Nextlineif (Str.compareto ("0") ==0) {break;} Strs=str.split ("");//Then divide all numbers and symbols by split with a space//for (int i=0;i<strs.length;i++) {//system.out.print (strs[i]);/} System.out.println (); Linkedlist<double> numque=new linkedlist<double> ();//Create a collection of two linked lists that can be run as a stack or as a queue linkedlist< String> signque=new linkedlist<string> ();d ouble mid=0.0;for (int i=0;i<strs.length;i++) {if ((i+1)%2==0) { Because the topic is a normative formula, all even numbers must be symbols, odd is the number if (Strs[i].compareto ("*") ==0) {//If the multiplication symbol, then the number is taken from the digital stack, and the next number will be stored in the corresponding operation mid= Numque.getlast () *double.parsedouble (strs[i+1]);} else if (Strs[i].compareto ("/") ==0) {mid=numque.getlast ()/double.parsedouble (strs[i+1]);} else{//if not, then into the queue Signque.add (Strs[i]); continue;} System.out.println (strs[i]+mid); i=i+1;//if multiplication, because the next has been doneDouble.parsedouble (Strs[i]));//Otherwise the number is enqueued}}//iterator<double> it=numque.iterator ();//while (It.hasNext ()) {// System.out.print (It.next () + "");//}//iterator<string> It2=signque.iterator ();//while (It2.hasnext ()) {// System.out.print (It2.next () + "");//}double Result=numque.pollfirst ();//Because the symbol queue must be one less than the number queue after it has been put out, so first take out a number string sign; while (!signque.isempty ()) {//Then let the symbol out of the team, while the number of children also out of the team, until the empty Sign=signque.pollfirst (); if (Sign.compareto ("+") ==0) {result+= Numque.pollfirst ();} Else{result-=numque.pollfirst ();}} System.out.printf ("%.2f", result); System.out.println ();}}}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1237 (Simple Calculator) LinkedList class implements stack and queue functions

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.