表达式求值问题

1.将中缀表达式转化为后缀表达式

从头到尾读取对象:

运算数  直接输出顺序不变;

运算符  与栈顶符号比较: 优先级大于存入,优先级小于等于栈顶符号弹出,继续与新栈顶比较;

            1.+ - :出栈直到遇到左括号(如果有 出栈不输出)后存入;   

            2.* /  :如果栈顶符号是*/弹出,直到不是后存入;

            3.用while循环弹出符号时注意判空;

左括号  放入堆栈(左括号优先级小于-+*/);

右括号  将栈顶符号弹出直至遇到左括号(出栈不输出);

对象全部处理完  把堆中符号依次拿出;


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

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*+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
    char data[10001];
    int top;
} *s;

void init (struct stack *s)
{
    s->top=0;
}
void push (struct stack* s,char c)
{
    s->data[s->top]=c;
    s->top++;
}

void pop (struct stack *s)
{
    s->top--;
    printf("%c",s->data[s->top]);
}
int empty (struct stack * s)
{
    if(s->top==0) return 1;
    else return 0;
}

int main()
{
    char c[10001];
    gets(c);
    struct stack *s;
    s=(struct stack *)malloc(sizeof(struct stack));
    init(s);
    int i;
    for(i=0; c[i]!='\0'; i++)
    {
        if(c[i]!='#')
        {
            if(c[i]>='a'&&c[i]<='z') printf("%c",c[i]);
            if(c[i]=='(')
            {
                push(s,c[i]);
            }
            if(c[i]==')')
            {
                while(s->data[s->top-1]!='('&&empty(s)!=1)
                {
                    pop(s);
                }
                s->top--;
            }
            if((c[i]=='*')||(c[i]=='/'))
            {
                while((empty(s)!=1)&&(s->data[s->top-1]=='/'||s->data[s->top-1]=='*'))
                {
                    pop(s);
                }
                push(s,c[i]);
            }
            if((c[i]=='+')||(c[i]=='-'))
            {
                while((empty(s)!=1)&&(s->data[s->top-1]!='('))
                {
                    pop(s);
                }
                push(s,c[i]);
            }
        }
    }
    while(empty(s)!=1)
    {
        pop(s);
    }
    return 0;
}



数据结构实验之栈与队列三:后缀式求值

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。

Input

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

Output

求该后缀式所对应的算术表达式的值,并输出之。

Sample Input

59*684/-3*+#

Sample Output

57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
    int data[1001];
    int top;
}*s;

void init(struct stack *s)
{
    s->top=0;
}
void push(int n, struct stack *s)
{
    s->data[s->top]=n;
    s->top++;
}
int pop(struct stack *s)
{
    int n;
    s->top--;
    n=s->data[s->top];
    return n;
}

int main()
{
    char c[1001];
    int i,x1,x2;
    struct stack *s;
    s=(struct stack *)malloc(sizeof(struct stack));
    gets(c);
    for(i=0; c[i]!='#'; i++)
    {
        if(c[i]>'0'&&c[i]<='9')
        {
            push(c[i]-'0',s);
        }
        else
        {
            if(c[i]=='+')
            {
                x1=pop(s);
                x2=pop(s);
                push(x1+x2,s);
            }
            if(c[i]=='-')
            {
                x1=pop(s);
                x2=pop(s);
                push(x2-x1,s);
            }
            if(c[i]=='*')
            {
                x1=pop(s);
                x2=pop(s);
                push(x1*x2,s);
            }
            if(c[i]=='/')
            {
                x1=pop(s);
                x2=pop(s);
                push(x2/x1,s);
            }
        }
    }
    printf("%d",pop(s));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44153125/article/details/88933739