• 逆波兰表达式


     以输入正确的逆波兰表达式为前提,计算结果。。。

    #define MAXSIZE 10
    typedef struct Node
    {
        double data;
        struct Node *next;
    }Node;
    
    typedef struct Stack
    {
        Node *top;
    }Stack;
    
    void s_push(Stack *s, double data);
    void s_pop(Stack *s, double *d);
    
    
    int main()
    {
        Stack s = {NULL};
        char l[MAXSIZE] = {0};
        char el;
        double d,e;
        int i = 0;
        
        printf("输入逆波兰表达式,以#结尾:
    ");
        scanf("%c", &el);
        
        while(el!='#')
        {
            if(isdigit(el) || el == '.')
            {
                l[i] = el;
                l[i+1] = '';
            }
            if(el == ' ')
            {
                if(l[0]) {
                    s_push(&s, atof(l));
                    l[0] = '';
                    i = 0;
                }
                
            }
            
            switch(el)
            {
                case '+':
                    s_pop(&s, &e);
                    s_pop(&s, &d);
                    s_push(&s, d+e);
                    break;
                case '-':
                    s_pop(&s, &e);
                    s_pop(&s, &d);
                    s_push(&s, d-e);
                    break;
                case '*':
                    s_pop(&s, &e);
                    s_pop(&s, &d);
                    s_push(&s, d*e);
                    break;
                case '/':
                    s_pop(&s, &e);
                    s_pop(&s, &d);
                    if(e == 0) {
                        printf("除数为0");
                        exit(-1); 
                    }
                    s_push(&s, d/e);
                    break;
            }
            
            scanf("%c", &el);
        }
        
        s_pop(&s, &d);
        printf("计算结果:%f", d);
        
        return 0;
    }
    
    void s_push(Stack *s, double data)
    {
        Node *p = (Node *)malloc(sizeof(Node));
        p->data = data;
        p->next = s->top;
        s->top = p;
    }
    
    void s_pop(Stack *s, double *d)
    {
        Node *p = s->top;
        if(p == NULL)
        {
            return ;
        }
        *d = p->data;
        s->top = p->next;
        free(p);
        
    }
  • 相关阅读:
    PMP-合同类型
    一、JavaScript简介
    编写一程序,从键盘输入10个实数,计算并输出算术平均数
    从键盘输入3个整数,输出其中最大数
    穷举法判断键入的数是不是素数
    2.事件每天执行
    1.mysql 启动服务提示输入密码
    二、linux 用户授权
    1.maven打包乱码
    1.下载谷歌插件
  • 原文地址:https://www.cnblogs.com/buerr/p/7359090.html
Copyright © 2020-2023  润新知