Evaluate the NYOJ 35 expression

Source: Internet
Author: User

This is the value of the arithmetic expression. The operand is a real number greater than or equal to. The operators include +,-, *,/, and (). You only need to open two stacks, one record operator, and one record suffix expression. That is, the original infix expression is converted into a suffix expression (inverse polish expression) and then calculated. Prefix and suffix notation have three common features: the order of the operands is the same as the order of the operands in the equivalent infix expression. The priority of the parentheses operator is not required. The irrelevant infix is converted to the suffix algorithm:. returns an operator or operand. B. if the input is an operand, It is output to the array and converted to a; c. if the input is '(', press the stack, and convert it to a; d. if the input is ')', the stack is pushed out of the stack, and the output is to the array until the stack top is '(', not output to the array (discard), to a; e. if the input is an operator, if the stack is empty or the stack top is '(' or operator. the first level is greater than the stack top operator, the pressure stack, to a if the operator priority is less than the stack top operator, then the stack is output, to the array, to e; d. if the input ends, the output stack is output to the array until the stack is empty. [Cpp] # include <iostream> # include <stdio. h> # include <stdlib. h> # include <string. h> # include <math. h >#include <algorithm> # include <stack> # include <queue> # include <map> # include <string> using namespace std; int priority [100]; // initialization operator priority void initPri () {priority ['('] = 1; priority ['+'] = 2; priority ['-'] = 2; priority ['*'] = 3; priority ['/'] = 3;} // a indicates the top of the symbol stack, and B indicates the current judgment symbol. // a> = B, returns true; a <B; returns false Bool judgeOpePri (char a, char B) {return priority [a]> = priority [B]? True: false;} // two parameters: infixExp, the generated suffix expression void convert (string infixExp, stack <string> & postfixExp) {// operator stack <char> opS; // temporary operand string digitTemp; // temporary operator string operTemp; int len = infixExp. length (); bool hasDigit = false; // filter out '=' for (int I = 0; I <len-1 ;) {// if it is part of the operand while (isdigit (infixExp [I]) | infixExp [I] = '. ') & I <len-1) {hasDigit = true; digitTemp. push_back (infixExp [I]); I ++;} if (hasD Igit = true) {postfixExp. push (digitTemp); digitTemp = ""; hasDigit = false;} else {// if the operator stack is empty, go to the stack if (opS. empty () {opS. push (infixExp [I]); I ++;} // note that for '('equivalent priority is not effective else if (infixExp [I] = '(') {opS. push (infixExp [I]); I ++;} // The else if (infixExp [I] = ') all operators in "()" are displayed ')') {while (! OpS. empty () {char c = opS. top (); operTemp = ""; operTemp. push_back (c); opS. pop (); if (c = '(') {break;} else {postfixExp. push (operTemp) ;}} I ++;} // The higher priority of the operator stack, else if (judgeOpePri (opS. top (), infixExp [I]) = true) {char c = opS. top (); operTemp = ""; operTemp. push_back (c); opS. pop (); postfixExp. push (operTemp); // note that I ++ is not required at this time, because we need to continue to compare} // else {opS with a lower priority at the top of the operator stack. push (infixExp [I]); I ++ ;}}// clear the symbol Stack While (! OpS. empty () {char c = opS. top (); operTemp = ""; operTemp. push_back (c); opS. pop (); postfixExp. push (operTemp) ;}} double answerQ (stack <string> & postfixExp) {stack <string> couterFix; stack <double> resultSta; while (! PostfixExp. empty () {couterFix. push (postfixExp. top (); postfixExp. pop () ;} double a, B; while (! CouterFix. empty () {string c = couterFix. top (); double d; couterFix. pop (); int cas = 5; if (strcmp (c. c_str (), "+") = 0) cas = 0; else if (strcmp (c. c_str (), "-") = 0) cas = 1; else if (strcmp (c. c_str (), "*") = 0) cas = 2; else if (strcmp (c. c_str (), "/") = 0) cas = 3; if (cas! = 5) {double a = resultSta. top (); resultSta. pop (); double B = resultSta. top (); resultSta. pop (); switch (cas) {case 0: {resultSta. push (B + a); break;} case 1: {resultSta. push (B-a); break;} case 2: {resultSta. push (B * a); break;} case 3: {resultSta. push (B/a); break;} default: {}} else {sscanf (c. c_str (), "% lf", & d); resultSta. push (d) ;}return resultSta. top () ;}int main () {/* # ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); # endif */int n; scanf ("% d", & n); initPri (); while (n --) {string infixExp; stack <string> postfixExp; cin> infixExp; convert (infixExp, postfixExp); double ans = answerQ (postfixExp); printf ("%. 2lf \ n ", ans);} return 0 ;}

Related Article

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.