NYOJ-35-expression evaluate

Source: Internet
Author: User

NYOJ-35-expression evaluate

Http://acm.nyist.net/JudgeOnline/problem.php? Pid = 35

 

A good question: Using stacks to calculate the expression value

Rules for arithmetic operations: 1. First multiplication and division, then addition and subtraction; 2. From left to right; 3. Parentheses, and then parentheses

Note that you can use the atof function to convert a string to a floating point number.


# Include <stdio. h> # include <string. h> # include <stdlib. h> int map [7] [7] = // The priority relationship between operators. The value 100 indicates that no occurrence occurs {,-1,-1, 1}, {,-1,-1,-, 1}, {,-, 1}, {,-, 1 }, {-1,-1,-1,-1,-1,-100,}, {, 1}, {-1,-1,-1, -1,-1,100, 0 }}; int cam (char c) {switch (c) {case '+': return 0; case '-': return 1; case '*': return 2; case '/': return 3; case '(': return 4; case ')': return 5; case '#': return 6 ;}} double sol (double X, char c, double y) {switch (c) {case '+': return x + y; case '-': return x-y; case '*': return x * y; case '/': return x/y;} int z (char c) {if ('0' <= c & c <= '9' | c = '. ') return 1; if (c = '') return-1; return 0;} char str [1005]; char optr [1005]; double opnd [1005]; int main () {int t1, t2, k, len; char ch, zz; int temp1, temp2; double a, B; int t; scanf ("% d ", & t); getchar (); while (t --) {gets (str); len = strlen (str); str [len-1] = '#'; // process equal to t1 = T2 = k = 0; optr [t1 ++] = '#'; ch = str [k ++]; while (ch! = '#' | Optr [t1-1]! = '#') {If (z (ch) = 1) // operand into Stack {opnd [t2 ++] = atof (& str [k-1]); // convert the string to a floating point while (z (str [k]) = 1) k ++; ch = str [k ++];} else if (z (ch) =-1) ch = str [k ++]; else {temp1 = cam (optr [t1-1]); temp2 = cam (ch ); if (map [temp1] [temp2] =-1) // The top element of the stack has a low priority {optr [t1 ++] = ch; ch = str [k ++];} else if (map [temp1] [temp2] = 0) // remove the brackets and accept the next character {t1 --; ch = str [k ++];} else // return the stack and return the calculation result {zz = optr [-- t1]; a = opnd [-- t2]; B = opnd [-- t2]; opnd [t2 ++] = sol (B, zz, a) ;}} printf ("%. 2lf \ n ", opnd [0]);} return 0 ;}

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.