zoukankan      html  css  js  c++  java
  • 生成逆波兰表达式

    #define MAXSIZE 10
    typedef struct OpNode
    {
        char data;
        struct OpNode *next;
    }OpNode;
    
    typedef struct OpStack
    {
        OpNode *top;
    }OpStack;
    
    void op_pop(OpStack *s, char *d);
    void op_push(OpStack *s, char data);
    int op_len(OpStack *s);
    
    int main()
    {
        OpStack op = {NULL};
        char list[MAXSIZE] = {'0'};
        char el, d, e;
        printf("输入表达式,以=结束:");
        scanf("%c", &el);
        
        while(el != '=')
        {
            while(isdigit(el) || el == '.')
            {
                printf("%c", el);
                scanf("%c", &el);
            }
            printf(" ");
            switch(el)
            {
                case ')':
                    if(op_len(&op)) {
                        do {
                            op_pop(&op, &e);
                            printf("%c", e);
                        }while(op_len(&op) && e != '(');
                    }else {
                        printf("出错
    ");
                        exit(-1);
                    }
                    break;
                case '+':
                case '-':
                case '(':
                    if(op_len(&op))
                    {
                        do {
                            op_pop(&op, &e);
                            if(e == '(')
                            {
                                op_push(&op, e);
                            }else {
                                printf("%c ", e);
                            }
                        }while(op_len(&op) && e != '(');
                    }
                    op_push(&op, el);
                    break;
                case '*':
                case '/':
                    op_push(&op, el);
                    break;
                default:
                    break;
            }
            
            if(el == '=')
            {
                break;
            }
            scanf("%c", &el); 
        }
        
        while(op_len(&op))
        {
            op_pop(&op, &e);
            printf("%c ", e);
        }
        return 0;
     } 
     
    
    void op_push(OpStack *s, char data)
    {
        OpNode *p = (OpNode *)malloc(sizeof(OpNode));
        p->data = data;
        p->next = s->top;
        s->top = p;
    }
    
    void op_pop(OpStack *s, char *d)
    {
        OpNode *p = s->top;
        if(p == NULL)
        {
            return ;
        }
        *d = p->data;
        s->top = p->next;
        free(p);
        
    }
    
    int op_len(OpStack *s)
    {
        int i = 0;
        OpNode *p = s->top;
        while(p != NULL)
        {
            p = p->next;
            i++;
        }
        
        return i;
    }
  • 相关阅读:
    JVM 调优工具
    JVM tomcat 性能调优
    meven 新建web 项目
    垃圾收集器
    JVM 内存溢出
    JVM 常见参数配置
    垃圾回收机制策略
    MongoDB C#驱动:
    基于MSMQ绑定的WCF服务实现总结
    python _、__和__xx__的区别(转)
  • 原文地址:https://www.cnblogs.com/buerr/p/7366865.html
Copyright © 2011-2022 走看看