Infix expression Evaluation

Source: Internet
Author: User

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 from the stack, pops a symbol from the symbol stack, and calculates the result into 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>a;
stack<char>str;
int i,n;
Char s[2000];
Str.push (' # '); Determine the condition of the stack empty
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 numbers, because the number may be greater than 10, so here is a special calculation
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)//into stack
{
Str.push (S[i]);
i++;
}
Else
if (Judge (Str.top (), s[i]) ==1)//out Stack First
{
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))//Bracket
{
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)//prioritization
{
if (popx== ' * ' | | popx== '/')
{
if (x== ' (')
return 2; Direct into stack
Else
if (x== ' + ' | | x== '-' | | x== ') ' | | x== ' * ' | | x== '/')//out Stack first count
return 1;
}
Else
if (popx== ' + ' | | popx== '-')
{
if (x== ' * ' | | x== '/' | | x== ' (')//direct into stack
return 2;
Else
if (x== ' + ' | | x== '-' | | x== ') ")
return 1;
}
Else
if ((popx== ' (' &&x!= ')) | | popx== ' # ')
return 2; Direct into stack
Else
if (popx== ' (' &&x== '))
return 3;

}
int func (int x,char str,int y)/computed value
{
if (str== ' + ')
return x+y;
if (str== '-')
return x-y;
if (str== ' * ')
return x*y;
if (str== '/')
return x/y;
}

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.