Infix expression evaluate, infix evaluate

Source: Internet
Author: User

Infix expression evaluate, infix evaluate

The infix expression is used to calculate an expression, such as a calculator.

This is implemented using the stack data structure. First, enter a string to represent an expression, and then store the number with one stack and the storage symbol of the other stack.

If the priority of the current operator is higher than that of the top element of the stack, the incoming operator is added to the stack. If the priority of the current operator is smaller than or equal to the priority of the top element of the stack, two elements are displayed from the digital stack, A symbol pops up from the symbol stack and the calculation result is pushed to the stack (Digital stack );

The Code is as follows:

# Include <iostream>

Using namespace std;
# Include <cstring>
# Include <stack>
# Include <cmath>
Int judge (char x, char popx );
Int func (int x, char str, int y );
Int main ()
{
Int w;
Cin> w;
While (w --)
{
Stack <int>;
Stack <char> str;
Int I, n;
Char s [2000];
Str. push ('#'); // condition for judging the empty Stack
Cin> s;
For (I = 0; I <strlen (s );)
{
If (s [I]-'0'> = 0 & s [I]-'0' <= 9)
{
Int m [20], j;
For (j = I; s [j]-'0'> = 0 & s [j]-'0' <= 9; j ++) // when calculating a number, because the number may be greater than 10, special computation is required here.
M [j-I] = s [j]-'0 ';
Int sum = 0;
For (int k = j-i-1; k> = 0; k --)
Sum + = m [k] * pow (10, j-i-k-1 );
A. push (sum );
I = j;
}
Else
{
If (judge (str. top (), s [I]) = 2) // inbound Stack
{
Str. push (s [I]);
I ++;
}
Else
If (judge (str. top (), s [I]) = 1) // calculate
{
Int numb1 = a. top ();
A. pop ();
Int numb2 = a. top ();
A. pop ();
A. push (func (numb2, str. top (), numb1 ));
Str. pop ();
}
Else
If (judge (str. top (), s [I] = 3) // parentheses
{
Str. pop ();
I ++;
}
}
}
While (true)
{
If (str. top () = '#')
{
Cout <a. top () <endl;
A. pop ();
Break;
}
Else
{
Int numb5 = a. top ();
A. pop ();
Int numb6 = a. top ();
A. pop ();
A. push (func (numb6, str. top (), numb5 ));
Str. pop ();
}
}
}
}
Int judge (char popx, char x) // determine the priority
{
If (popx = '*' | popx = '/')
{
If (x = '(')
Return 2; // directly go to the stack
Else
If (x = '+' | x = '-' | x = ') '| x =' * '| x ='/') // calculate the output stack first.
Return 1;
}
Else
If (popx = '+' | popx = '-')
{
If (x = '*' | x = '/' | x = '(') // directly enters the stack
Return 2;
Else
If (x = '+' | x = '-' | x = ')')
Return 1;
}
Else
If (popx = '(' & x! = ') | Popx = '#')
Return 2; // directly go to the stack
Else
If (popx = '(' & x = ')')
Return 3;

}
Int func (int x, char str, int y) // calculates the value.
{
If (str = '+ ')
Return x + y;
If (str = '-')
Return x-y;
If (str = '*')
Return x * y;
If (str = '/')
Return x/y;
}
Complete the value operation of the standard infix arithmetic expression

I used to write this inverse Polish algorithm, but it was written in C. In addition, we did not consider the exponential operation when writing this algorithm. We only considered four arithmetic operations. If necessary, you can send your e-mail and send the source code of C # To your reference. There are a lot of codes that cannot be pasted here.
The following is a demo of the C # version of the inverse Polish algorithm:



 
Calculate the infix expression

Just now we have a computer question: we need to change the suffix to the suffix, and then evaluate the expression. I have completed all the code. The program is okay. Let's see it for yourself.
# Include "stdio. h"
# Include "stdlib. h"
# Include "conio. h"

/* Stack node type */
Typedef struct node
{
Char data;
Struct node * next;
} Snode, * slink;
Typedef struct node_post
{
Int data;
Struct node_post * next;
} Pnode, * plink;

/* Global variable definition */
Slink top = NULL;
Plink ptop = NULL;

/* Stack empty detection. The value is 1 */
Int Emptystack ()
{
If (top = NULL) return 1;
Else return 0;
}

/* Output stack, return data in top */
Char Pop ()
{
Char e;
Slink p;
If (top = NULL) return (-1 );
Else
{
P = top;
Top = top-> next;
E = p-> data;
Free (p );
Return e;
}
}
Int Pop_post ()
{
Int e;
Plink p;
If (ptop = NULL) return (-1 );
Else
{
P = ptop;
Ptop = ptop-> next;
E = p-> data;
Free (p );
Return e;
}
}

/* Stack entry */
Void Push (char e)
{
Slink p;
P = (slink) malloc (sizeof (snode ));
P-> data = e;
P-> next = top;
Top = p;
}
Void Push_post (int e)
{
Plink p;
P = (plink) malloc (sizeof (pnode ));
P-> data = e;
P-> next = ptop;
Ptop = p;
}

/* Obtain the top stack */
Char Gettop ()
{If (! Emptystack (top) return (top-> data );
Else return 0;
}

/* Symbol priority comparison */
Int Precede (char x, char y)
{
Int a, B;
Switch (x)
{
Case '#': a =-1; break;/* insert this line */
Case '(': a = 0; break;
Case '+ ':
Case '-': a = 1; break;
Case '*':
Case '/':... the remaining full text>

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.