Infix expressions are used to compute an expression, such as the calculator is implemented
This is accomplished using the data structure of the stack. First, enter a string representing an expression, then store the number with one stack, and another stack store symbol
If the current operator priority is higher than the top of the stack, the stack, if the current operator priority is less than or equal to the top operator precedence, pops two elements
Infix expression-to-suffix expression method:1. Operand encountered: Direct output (added to suffix expression)2. When the stack is empty, the operator is encountered, directly into the stack3. An opening parenthesis is encountered: put it in the stack4. A closing parenthesis is encountered: Performs a stack operation, and outputs the elements of the stack, until the left parenthesis of the pop-up stack, and the left parenthesis does not output.5. Oth
Today learn the infix type of knowledge, to see the actual combat code:def main (args:array[string]) {Object Log {def >>:(data:string): Log.type = {println (data); log}}"Hadoop" >>: "Spark" >>: LogVal list = list ()Val newlist = "A":: "B":: Listprintln (NewList)Class Infix_type[a,b]Val infix:int infix_type String = nullCase Class Cons (first:string,second:string)Val case_class = Cons ("One", "one", "one")Case_class Match {case "one" Cons "one" and "pr
Convert infix to suffix expressionFirst, the suffix expression evaluatesThe suffix expression is also called inverse Polish expression, and its evaluation process can be used to assist the storage of the stack. Assuming that the suffix expression to be evaluated is: 6 5 2 3 + 8 * + 3 + *, the evaluation process is as follows:1) Traversing the expression, the number encountered is first put into the stack, and the stack looks like this:2) then reads "+
Transfer from http://blog.csdn.net/mvpsendoh/article/details/6440559Objective: To convert infix expression (the standard form of expression) to a suffix.Example: a+b*c+ (d*e+f) *g converted to abc*+de*f+g*+Conversion principle:1. When a operand is read, it is immediately placed in the output. The operator is not immediately output and is placed in the stack. The opening parenthesis is also pushed into the stack.2. If a closing parenthesis is encounter
Usually used in the standard arithmetic expression, that is, "9+ (3-1) *3+10/2" is called infix expression. Because all the operational symbols are in the middle of two digits.
This infix expression "9+ (3-1) *3+10/2" into the suffix expression "9 3 1-3*+ 10 2/+", how to do it.
The rules are as follows:
1. Operand encountered: Direct output (added to suffix expression)
2. When the stack is empty, the op
Package Cn.itcast.StackAndQuen;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.Scanner;
/** * Created by Likailong on 2016/10/16. * infix expression to suffix expression */public class Mathcaculate {public static void main (String [] args) {Scanner san=new Scanne R (system.in);//keyboard reads list
Header file:#pragma once#include Main function:#include "exp.h" #include "exp.h"//Check the priority between the symbols, 1 means that the >,0 means =,-1 represents the operation within the Application of "Data structure" stack--infix expression Evaluation (C + +)
()! ='(') - { Aboutcout " "; $ S.pop (); - } - S.pop (); - } A Else //in normal case, the CH and stack top characters are compared priority + { the while(! S.empty () s.top ()! ='(' getpriority (S.top ()) >=getpriority (CH)) - { $cout " " ; the S.pop (); the } the s.push (CH); the -}cin >>ch; in } the the while(!s.empty ()) About { thecout s.top (); the S.pop (); the if(!s.empty ())
This should be a point of knowledge that trained programmers have learned when they learn discrete mathematics.It is easy for you to write the suffix expression yourself with a pen, but it is not possible to do so with code, only to know how to use the stack.Publicclasscaculator{privateintpriority (charc) {if (c== ' * ' || c== '/') return2;elseif (c== ' + ' | | c== '-') return1;elsereturn0;} Privatebooleanleftpirorityisnotless (CHARACTERNBSP;C1,CHARNBSP;C2) {if (c1==null) return false;returnpri
Through the stack to convert infix expression to suffix expression and based on the suffix expression solution, including Header.h for the previous stack operation related function instance program, changed to header file on the line. Practice + record, Master ignore.
OutPut:
The init formula:3+4*5+ (6*7+8) *9The stack is empty.Convert result:345*+67*8+9*+Calculate result:473
Code by pnig0s1992//date:2012,3,21 #include
This article is from the "
An expression consisting of addition, subtraction, multiplication, division, and parentheses is provided to calculate the value of the expression and the prefix and suffix expression of the expression.
# Include
# Include
# Define Inf
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.