Data structure routine -- Expression evaluate (using stack structure), routine evaluate

Source: Internet
Author: User

Data structure routine -- Expression evaluate (using stack structure), routine evaluate

This article focuses on the basic series of network courses on data structures (3): Application 1-expression evaluation of 5th class stacks in stacks and queues.

For example, you enter a valid mathematical expression that contains "+", "-", "*", "/", positive integers, and parentheses to calculate the operation result of the expression.

Answer:

# Include <stdio. h> # include <stdlib. h> # define MaxOp 100 # define MaxSize 100 struct // sets the operator priority {char ch; // operator int pri; // priority} lpri [] = {'= ', 0}, {'(', 1}, {'*', 5}, {'/', 5}, {'+', 3 },{'-', 3}, {')', 6 }}, rpri [] = {'=', 0}, {'(', 6 },{'*', 4}, {'/', 4}, {'+', 2}, {'-', 2}, {')', 1 }}; int leftpri (char op) // evaluate the op priority of the Left operator {int I; for (I = 0; I <MaxOp; I ++) if (lpri [I]. ch = op) return lpri [I]. pri;} int rightpri (char op) // evaluate the op priority of the right operator {int I; for (I = 0; I <MaxOp; I ++) if (rpri [I]. ch = op) return rpri [I]. pri;} bool InOp (char ch) // determines if ch is an operator {if (ch = '(' | ch = ') '| ch =' + '| ch ='-'| ch =' * '| ch ='/') return true; else return false;} int Precede (char op1, char op2) // comparison result of op1 and op2 operator priority {if (leftpri (op1) = rightpri (op2 )) return 0; else if (leftpri (op1) <rightpri (op2) return-1; else return 1;} void trans (char * exp, char postexp []) // calculate Convert the expression exp to the suffix expression postexp {struct {char data [MaxSize]; // store the int top operator; // Stack pointer} op; // define the operator stack int I = 0; // I is the subscript op of postexp. top =-1; op. top ++; // Add '=' to the stack op. data [op. top] = '; while (* exp! = '\ 0') // The loop {if (! InOp (* exp) // when it is a numeric character {while (* exp> = '0' & * exp <= '9 ') // It is determined to be a number {postexp [I ++] = * exp; exp ++;} postexp [I ++] = '#'; // use # To identify the end of a numeric string} else // switch (Precede (op. data [op. top], * exp) {case-1: // low priority of the stack top operator: Stack op. top ++; op. data [op. top] = * exp; exp ++; // Continue scanning other characters break; case 0: // only parentheses meet this condition op. top --; // run (return stack exp ++; // Continue scanning other characters break; case 1: // return the stack and output it to postexp. postexp [I ++] = op. data [op. top]; op. top --; break ;}} // While (* exp! = '\ 0') while (op. data [op. top]! = ') // At this time, the exp scan is completed and the stack is '=' until {postexp [I ++] = op. data [op. top]; op. top --;} postexp [I] = '\ 0'; // Add an end identifier to the postexp expression} float compvalue (char exp []) // calculate the value of the suffix expression {struct {float data [MaxSize]; // store the value int top; // Stack pointer} st; // define the value stack float d; char ch; int t = 0; // t is the subscript st of exp. top =-1; ch = exp [t]; t ++; while (ch! = '\ 0') // The loop {switch (ch) {case' + ': st. data [st. top-1] = st. data [st. top-1] + st. data [st. top]; st. top --; break; case '-': st. data [st. top-1] = st. data [st. top-1]-st. data [st. top]; st. top --; break; case '*': st. data [st. top-1] = st. data [st. top-1] * st. data [st. top]; st. top --; break; case '/': if (st. data [st. top]! = 0) st. data [st. top-1] = st. data [st. top-1]/st. data [st. top]; else {printf ("\ n \ t Division Error! \ N "); exit (0); // exit unexpectedly} st. top --; break; default: d = 0; // convert a number to a value and store it in d. while (ch> = '0' & ch <= '9 ') // It is a numeric character {d = 10 * d + ch-'0'; ch = exp [t]; t ++;} st. top ++; st. data [st. top] = d;} ch = exp [t]; t ++;} return st. data [st. top];} int main () {char exp [] = "(56-20)/(4 + 2 )"; // you can change exp to the keyboard input char postexp [MaxSize]; trans (exp, postexp); printf ("infix expression: % s \ n", exp ); printf ("suffix expression: % s \ n", postexp); printf ("expression value: % g \ n", compvalue (postexp); return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.