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