zoukankan      html  css  js  c++  java
  • 计算中缀表达式

    1.计算后缀表达式

     2.中缀表达式转后缀表达式

    #include <cstdio> 
    #include <cstring> 
    #include <cctype> 
    #include<iostream>
    using namespace std;
    
    //char stack
    char stack[25]; 
    int top = -1; 
    
    void push(char item) {
       stack[++top] = item; 
    } 
    
    char pop() {
       return stack[top--]; 
    } 
    
    //returns precedence of operators
    int precedence(char symbol) {
    
       switch(symbol) {
          case '+': 
          case '-':
             return 2; 
             break; 
          case '*': 
          case '/':
             return 3; 
             break; 
          case '^': 
             return 4; 
             break; 
          case '(': 
          case ')': 
          case '#':
             return 1; 
             break; 
       } 
    } 
    
    //check whether the symbol is operator?
    int isOperator(char symbol) {
    
       switch(symbol) {
          case '+': 
          case '-': 
          case '*': 
          case '/': 
          case '^': 
          case '(': 
          case ')':
             return 1; 
          break; 
             default:
             return 0; 
       } 
    } 
    
    //converts infix expression to postfix
    void convert(char infix[],char postfix[]) 
    {
       int i,symbol,j = 0; 
       stack[++top] = '#'; 
        
       for(i = 0;i<strlen(infix);i++) 
       {
          symbol = infix[i]; 
            
          if(isOperator(symbol) == 0) 
          {
             postfix[j] = symbol; 
             j++; 
          } 
          else 
          {
             if(symbol == '(') 
             {
                push(symbol); 
             } 
             else 
             {
                if(symbol == ')') 
                {
                   while(stack[top] != '(') 
                   {
                      postfix[j] = pop(); 
                      j++; 
                   } 
                        
                   pop();   //pop out (. 
                } 
                else 
                {
                   if(precedence(symbol)>precedence(stack[top])) 
                   {
                      push(symbol); 
                   }
                   else 
                   {
                        
                      while(precedence(symbol)<=precedence(stack[top])) 
                      {
                         postfix[j] = pop(); 
                         j++; 
                      } 
                      push(symbol); 
                   }
                }
             }
          }
       }
        
       while(stack[top] != '#') 
       {
          postfix[j] = pop(); 
          j++; 
       } 
        
       postfix[j]='';  //null terminate string. 
    } 
    
    //int stack
    int stack_int[25]; 
    int top_int = -1; 
    
    void push_int(int item) 
    {
       stack_int[++top_int] = item; 
    } 
    
    char pop_int() 
    {
       return stack_int[top_int--]; 
    } 
    
    //evaluates postfix expression
    int evaluate(char *postfix)
    {
       char ch;
       int i = 0,operand1,operand2;
    
       while( (ch = postfix[i++]) != '') 
       {
        
          if(isdigit(ch)) 
          {
             push_int(ch-'0');  // Push the operand 
          } 
          else 
          {
             //Operator,pop two  operands 
             operand2 = pop_int();
             operand1 = pop_int();
                
             switch(ch) 
             {
                case '+':
                   push_int(operand1+operand2);
                   break;
                case '-':
                   push_int(operand1-operand2);
                   break;
                case '*':
                   push_int(operand1*operand2);
                   break;
                case '/':
                   push_int(operand1/operand2);
                   break;
             }
          }
       }
        
       return stack_int[top_int];
    }
    
    int main() 
    { 
        char infix[100] ,postfix[25]; 
        cin>>infix;
        convert(infix,postfix); 
        
        printf("Infix expression is: %s
    " , infix);
        printf("Postfix expression is: %s
    " , postfix);
        printf("Evaluated expression is: %d
    " , evaluate(postfix));
       
        return 0;
    } 
  • 相关阅读:
    怎么重新启动网卡
    @JsonProperty的使用
    JAVA中的反射机制
    spring的IOC入门案例
    spring的IOC底层原理
    maven+Spring环境搭建
    SpringMVC与Struts2区别与比较总结
    Struts2面试题
    oracle自增序列创建
    Hibernate分页查询报错
  • 原文地址:https://www.cnblogs.com/Cindy-Chan/p/11185736.html
Copyright © 2011-2022 走看看