程序员面试的那些小事数据挖掘数据结构和算法分析

逆波兰表达式

2017-01-15  本文已影响58人  AceKitty

求表达式(1 - 2) * (4 + 5)的值

逆波兰表达式(后缀表达式)

波兰逻辑学家Jan.Lukasiewicz,发明了一种不需要括号的后缀表达式,我们通常把它称为波兰表达式(RPN)。

用栈来求解表达式(1 - 2) * (4 + 5)的值

图片.png 图片.png 图片.png

*代码实现(c语言)

#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>

#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10
#define MAXBUFFER 10

typedef double ElemType;
typedef struct {
    ElemType *base; //指向栈底的指针
    ElemType *top; //指向栈顶的指针
    int stackSize; //当前可使用的最大容量
}sqStack;

void InitStack(sqStack *s) {
    s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if (!s->base)
    {
        exit(0);
    }
    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}
//入栈操作要在栈顶进行,每次向栈中压入一个数据,top指针就要+1,直到栈满为止
void Push(sqStack *s, ElemType e) {
    if (s->top - s->base >= s->stackSize)
    {
        s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
        if (!s->base)
        {
            exit(0);
        }
    }
    *(s->top) = e;
    s->top++;
}
//出栈操作就是在栈顶取出数据,栈顶指针随之下移操作,每当从栈内弹出一个数据,栈的当前容量就-1
void Pop(sqStack *s, ElemType *e) {
    if (s->top == s->base)
    {
        return;
    }
    *e = *--(s->top);
}
//求栈的长度
int StackLen(sqStack s) {
    return s.top - s.base;
}

//清空栈, 将栈中的元素全部作废,但栈本身物理空间并不发生改变(不是销毁)
void ClearStack(sqStack *s) {
    s->top = s->base;
}
//销毁一个栈, 释放掉该栈所占据的物理内存空间
void DestoryStack(sqStack *s) {
    int i, len;
    len = s->stackSize;
    for (i = 0; i < len; i++)
    {
        free(s->base);
        s->base;
    }
    s->base = s->top = NULL;
    s->stackSize = 0;
}

int main() {
    sqStack s;
    char c;
    double d, e;
    char str[MAXBUFFER];
    int i = 0;

    InitStack(&s);
    printf("请按逆波兰表达式输入待计算的数据,数据与运算符之间用空格隔开,以#作为结束标志:");
    scanf_s("%c", &c);
    while (c != '#')
    {
        while (isdigit(c) || c == '.')
        {
            str[i++] = c;
            str[i] = '\0';


            if (i >= 10)
            {
                printf("出错,输入的单个数据过大!\n");
                return -1;
            }
            scanf_s("%c", &c);
            if (c == ' ')
            {
                d = atof(str);
                Push(&s, d);
                i = 0;
                break;
            }
        }


        switch (c)
        {
        case '+':
            Pop(&s, &e);
            Pop(&s, &d);
            printf("****%f***%f\n", e, d);
            Push(&s, d + e);
            break;
        case '-':
            Pop(&s, &e);
            Pop(&s, &d);
            Push(&s, d - e);
            break;
        case '*':
            Pop(&s, &e);
            Pop(&s, &d);
            Push(&s, d * e);
            break;
        case '/':
            Pop(&s, &e);
            Pop(&s, &d);
            if (e != 0)
            {
                Push(&s, d / e);
            }
            else
            {
                printf("\n出错:除数为0!\n");
            }
            break;
        default:
            break;
        }
        scanf_s("%c", &c);
    }
    Pop(&s, &d);
    printf("最终的计算结果是:%f", d);

    getchar();
    getchar();
    getchar();
    return 0;
}

中缀表达式转换成逆波兰表达式

人类喜欢这样的表达式:(1-2)*(4+5)
而不是这样的:1 2 - 4 5 + *

int main() {
    sqStack s;
    char c, e;
    InitStack(&s);
    printf("请输入中缀表达式,以#作为结束标志:");
    scanf_s("%c", &c);
    while (c != '#')
    {
        while (c >= '0' && c < '9')
        {
            printf("%c", c);
            scanf_s("%c", &c);
            if (c < '0' || c > '9')
            {
                printf(" ");
            }
        }

        if(')' == c)
        {
            Pop(&s, &e);
            while ('(' != e)
            {
                printf("%c", e);
                Pop(&s, &e);
            }
        }
        else if('+' == c || '-' == c)
        {
            if (!StackLen(s))
            {
                Push(&s, c);
            }
            else
            {
                do
                {
                    Pop(&s, &e);
                    if ('(' == e)
                    {
                        Push(&s, e);
                    }
                    else
                    {
                        printf("%c", e);
                    }
                } while (StackLen(s) && '(' != e);

                Push(&s, c);
            }
        }
        else if('*' == c || '/' == c || '(' == c)
        {
            Push(&s, c);
        }
        else if('#' == c)
        {
            break;
        }
        else
        {
            printf("\n出错,输入格式错误!\n");
            return -1;
        }
        scanf_s("%c", &c);
    }

    while (StackLen(s))
    {
        Pop(&s, &e);
        printf("%c", e);
    }
     
    getchar();
    getchar();
    getchar();
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读