zoukankan      html  css  js  c++  java
  • 后缀式求值

    /*其实完全不用建立结构体,但是由于刚学栈的结构体形式所以才用一下,希望王旭老师多多指教
    */
     
    #include<stdio.h>  
    #include<stdlib.h>  
    struct stack  
    {  
        int c[100];  
        int top;  
    };  
     
    int Push ( struct stack *p,int n)  
    {  
        if( p->top == 49 )  
            return 0;  
        else 
        {  
            p->top++;  
            p->c[p->top] =n;  
            return 1;  
        }  
    }  
     
    int Pop ( struct stack *p,int *ch)  
    {  
        if( p->top==-1 )return 0;  
        else 
        {     
            *ch = p->c[p->top];  
            p->top--;  
            return 1;  
        }  
     
    }  
    int sw(char c)  
     {  
         if(c=='+' || c=='-') return 1;  
         if(c=='*' || c=='/')  return 2;  
         if(c=='(') return 3;  
         if(c==')') return 4;  
     }  
     
    int jisuan(int x,int y,char c)  
    {  
         if(c=='+') return x+y;  
         if(c=='*')  return x*y;  
         if(c=='-') return x-y;  
         if(c=='/') return x/y;  
         else return 0;  
    }  
    int main()  
    {  
        struct stack *p;   
        char ch;  
        int x,y,*q,i;int n;  
        p = (struct stack *)malloc(sizeof(struct stack));     
        p->top = -1;  //指向栈底
        while((ch=getchar())!='#')  
        {  
              
            if( ch>='1'&&ch<='9')  
            {  
                n = ch-48;  //可以用atoi,而且用ch-'0'得到的n值不对;
     
                  
                Push(p,n);  //数组的话直接a[top]=n,top++;
            }  
     
        else 
            {  
                q=&i;  
                Pop(p,q);  
                x=*q;  
                Pop(p,q);  
                y=*q ;  
                y = jisuan(y,x,ch);  //由于8/2式入栈的时候为82/,出栈的时候为2。/8所以应该换下位置
                Push(p,y);  
            }  
        }  
        printf("%d",y);  
    }  
    

      

  • 相关阅读:
    hdu1233
    zoj 3529
    hdu 2516 取石子游戏
    组合博弈理论
    博弈——sg函数的原理和优化
    博弈及sg函数
    poj2039
    hdu 1250
    C# 类的继承和访问
    C# 索引
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2364045.html
Copyright © 2011-2022 走看看