Evaluate expressions (one of the applications in the data structure book stack)

Source: Internet
Author: User

Main Content: evaluate the expression, submit nyoj to pass...

The idea is to open two stacks, one operator stack, and the other operator stack ..

My code is as follows (relatively simple ):

/***** Author gery ******/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <vector> # include <cmath> # include <string> # include <stack> # include <queue> # define EPS 1e-9 # define ll long # define INF 0x3f3f3fusing namespace std; const int maxn = 1000 + 10; stack <double> LY; stack <char> gery; char STR [maxn], OP [maxn]; char operation [7] [7] // operator priority {'>', '>', '<', '> ', '>'}, // '+' {'>', '>', '<', '>', '> '}, // '-' {'>', '>', '<', '>', '> '}, // '*' {'>', '>', '<', '>', '> '}, // '/' {'<', '<', '= ',','}, // '(' {'>', '> '},//') '{' <',' <',', '='}, // '= '}; int get_index (char ch) {Switch (CH) {Case '+': Return 0; Case '-': return 1; Case '*': return 2; case '/': return 3; Case '(': return 4; Case ')': return 5; Case '=': return 6 ;}} char get_prio (char, char B) {int c = get_index (a); int d = get_index (B); return operation [C] [d];} double cal_value (double A, double B, char c) {Switch (c) {Case '+': Return A + B; Case '-': return a-B; Case '*': return a * B; case '/': return a * 1.0/B;} int main () {int T, PD, CNT, I; double temp, left_value, right_value, Val; char CH, OP; scanf ("% d", & T); While (t --) {scanf ("% s", STR); ly. empty (); Gery. empty (); Gery. push ('='); for (I = 0; I <strlen (STR); I ++) {CNT = 0, Pd = I; // Pd is the character pointer if (isdigit (STR [I]) | STR [I] = '. '| STR [I] ='-') // The number of rounds. {While (isdigit (STR [Pd]) | STR [Pd] = '. '| STR [Pd] ='-') {op [CNT] = STR [Pd]; CNT ++, PD ++ ;} OP [CNT] = '\ 0'; temp = atof (OP); // the system function is short for ASCII to float, similar to atoi, is to convert the string to the Float Type Function ly. push (temp); I = pd-1;} else {CH = get_prio (Gery. top (), STR [I]); Switch (CH) {case' <': Gery. push (STR [I]); break; Case '=': Gery. pop (); break; Case '>': op = Gery. top (), Gery. pop (); right_value = ly. top (), Ly. pop (); left_value = ly. top (), Ly. pop (); val = cal_value (left_value, right_value, OP); ly. push (VAL); I --; break; // After expression operation, the character pointer must be moved back }}} printf ("%. 2lf \ n ", Ly. top ();} return 0;}/* 2 (-2 + 3) * 1.2 + 2) = (-2 + 3) * 10/2) = */

Later, AC saw other people use the method in the book to perform the sub-assembly, but it was too troublesome to know which method is better...



Evaluate expressions (one of the applications in the data structure book stack)

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.