• 后缀式求值


    /*其实完全不用建立结构体,但是由于刚学栈的结构体形式所以才用一下,希望王旭老师多多指教
    */
     
    #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);  
    }  
    

      

  • 相关阅读:
    centos7开启关闭防火墙
    虚拟机vmnet1和vmnet8找不到,注册表没有删除干净,见图
    dhcp和static的区别
    SQL手册
    canal架构原理
    SQL优化(待完善)
    数仓简介
    java逆变与协变(待完善)
    mysqljoin的原理和优化
    深入理解java虚拟机阅读笔记
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2364045.html
Copyright © 2020-2023  润新知