Stack 3 of Data Structure experiment: suffix-based evaluation

Source: Internet
Author: User
Stack 3 of Data Structure experiment: suffix-based evaluation Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ This topic describes the value of the arithmetic expression represented by a suffix representation based on binary operators (Basic operands are all a positive integer. Enter a suffix string of an arithmetic expression and use '#' as the end sign. Calculate the value of the arithmetic expression corresponding to the suffix and output it. Sample Input
59*684/-3*+#
Sample output
57
It indicates that the basic operands are all positive integers! Source sample program

# Include <stdio. h> # include <string. h> # include <stdlib. h >#include <iostream >#include <stack> # include <algorithm> using namespace STD; int main () {stack <int> q; char STR [110]; scanf ("% s", STR); For (INT I = 0; STR [I]! = '#'; I ++) {If (STR [I]> = '0' & STR [I] <= '9 ') // This is the size of the ASCII code value. To become a normal size, subtract 48; q. push (STR [I]-48); else // The following is the repeated inbound and outbound stack, which is equivalent to converting the general formula into a suffix-type reverse application. {If (STR [I] = '+') {int A = Q. top (); q. pop (); int B = Q. top (); q. pop (); int c = a + B; q. push (c);} If (STR [I] = '-') {int A = Q. top (); q. pop (); int B = Q. top (); q. pop (); int c = B-A; q. push (c);} If (STR [I] = '*') {int A = Q. top (); q. pop (); int B = Q. top (); q. pop (); int c = a * B; q. push (c);} If (STR [I] = '/') {int A = Q. top (); q. pop (); int B = Q. top (); q. pop (); int c = B/a; q. push (c) ;}} printf ("% d \ n", Q. top (); // the last remaining element Is the final result. Return 0 ;}


Zookeeper

Stack 3 of Data Structure experiment: suffix-based evaluation

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.