• SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式


    数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Problem Description

    对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。

    Input

    输入一个算术表达式,以‘#’字符作为结束标志。

    Output

    输出该表达式转换所得到的后缀式。

    Sample Input

    a*b+(c-d/e)*f#

    Sample Output

    ab*cde/-f*+

    在做这道题之前首先要了解什么是后缀表达式
    百度百科—后缀表达式
    这是一种适合计算机计算的表达式,具体步骤:

    1. 遇到操作数:直接输出(添加到后缀表达式中)
    2. 栈为空时,遇到运算符,直接入栈
    3. 遇到左括号:将其入栈
    4. 遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,括号不输出。
    5. 遇到其他运算符:加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈
    6. 最终将栈中的元素依次出栈,输出。

    引用自 Casionx 的CSDN 博客——原表达式转换为后缀表达式

    附上代码:
    非线性

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node
    {
        char data;
        struct node *next;
    }Node;
    
    typedef struct stack
    {
        Node *base,*top;
    }Stack;
    
    Node *newnode()
    {
        Node *t;
        t = (Node *)malloc(sizeof(Node));
        t->next = NULL;
        return t;
    };
    
    Stack *Newstack()
    {
        Stack *t;
        t = (Stack *)malloc(sizeof(Stack));
        t->top = newnode();
        t->base = t->top;
        return t;
    }
    
    void push(Stack *t,char x)
    {
        Node *p = newnode();
        p->data = x;
        p->next = t->top->next;
        t->top->next = p;
        t->base = p;
    }
    
    char top(Stack *t)
    {
        return t->top->next->data;
    }
    
    void pop(Stack *t)
    {
        Node *p;
        p = t->top->next;
        t->top->next = t->top->next->next;
        free(p);
    }
    
    int empty(Stack *t)
    {
        if(t->top->next==NULL)
            return 1;
        return 0;
    }
    
    int judge(char a,char b)
    {
        if(b=='(')
            return 1;
        if(a=='*'||a=='/')
        {
            if(b=='+'||b=='-')
                return 1;
        }
        return 0;
    }
    
    int main()
    {
        Stack *t;
        char s[100050],s2[100050];
        int i,num = 0;
        scanf("%s",s);
        t = Newstack();
        for(i=0;s[i]!='#';i++)
        {
            if(s[i]=='(')
                push(t,s[i]);
            else if(s[i]==')')
            {
                while(top(t)!='(')
                {
                    s2[num++] = top(t);
                    pop(t);
                }
                pop(t);
            }
            else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
            {
                if(empty(t))
                    push(t,s[i]);
                else
                {
                    while(!empty(t)&&!judge(s[i],top(t)))
                    {
                        s2[num++] = top(t);
                        pop(t);
                    }
                    push(t,s[i]);
                }
            }
            else
                s2[num++] = s[i];
        }
        while(!empty(t))
        {
            s2[num++] = top(t);
            pop(t);
        }
        s2[num] = '';
        printf("%s
    ",s2);
        return 0;
    }
    

    线性

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct stack
    {
        char *top,*base;
        int len;
    }Stack;
    
    Stack newstack()
    {
        Stack t;
        t.top = (char *)malloc(100050*sizeof(char));
        t.base = t.top;
        t.len = 0;
        return t;
    }
    
    char top(Stack t)
    {
        return *(t.top-1);
    }
    
    void pop(Stack *t)
    {
        t->top--;
        t->len--;
    }
    
    void push(Stack *t,char x)
    {
        *(t->top) = x;
        t->top++;
        t->len++;
    }
    
    int judge(char a,char b)
    {
        if(b=='(')
            return 1;
        if(a=='*'||a=='/')
        {
            if(b=='+'||b=='-')
                return 1;
        }
        return 0;
    }
    
    int main()
    {
        Stack t;
        char s[100050],s2[100050];
        int num = 0,i;
        t = newstack();
        scanf("%s",s);
        for(i=0;s[i]!='#';i++)
        {
            if(s[i]=='(')
                push(&t,s[i]);
            else if(s[i]==')')
            {
                while(top(t)!='(')
                {
                    s2[num++] = top(t);
                    pop(&t);
                }
                pop(&t);
            }
            else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
            {
                if(!t.len)
                    push(&t,s[i]);
                else
                {
                    while(t.len&&!judge(s[i],top(t)))
                    {
                        s2[num++] = top(t);
                        pop(&t);
                    }
                    push(&t,s[i]);
                }
            }
            else
                s2[num++] = s[i];
        }
        while(t.len)
        {
            s2[num++] = top(t);
            pop(&t);
        }
        s2[num] = '';
        printf("%s
    ",s2);
        return 0;
    }
    
  • 相关阅读:
    python学习 05 函数switch功能
    python学习 04 函数参数
    python学习 03 函数 (只会执行一次return就不会往下执行)
    【转】Jenkins+Ant+Jmeter接口自动化集成测试实例
    【转】python做一个http接口测试框架
    python学习 02 元组
    【转】使用 Python Mock 类进行单元测试
    【转】使用Python学习selenium测试工具
    【转】利用Python中的mock库对Python代码进行模拟测试
    【转】python测试开发面试题
  • 原文地址:https://www.cnblogs.com/luoxiaoyi/p/9747948.html
Copyright © 2020-2023  润新知