zoukankan      html  css  js  c++  java
  • 后缀算法

    #include<iostream>
    using namespace std;
    #include<malloc.h>
    #include<stdio.h>
    #define MaxSize 50
    typedef struct{
    char data[MaxSize];
    int top;
    }A;
    void InitStack(A *&L)
    {
    L=(A *)malloc(sizeof(A));
    L->top=-1;
    }
    void DestoryStack(A *&L)
    {
    free(L);
    }
    bool StackEmpty(A *L)
    {
    return (L->top == -1);
    }
    bool Push(A *&L,char e)
    {
    if(L->top==MaxSize-1)
    return false;
    else
    L->top++;
    L->data[L->top]=e;
    return true;
    }
    bool Pop(A *&L,char &e)
    {
    if(L->top==-1)
    return false;
    else
    {
    e=L->data[L->top];
    L->top--;
    return true;
    }
    }
    bool GetTop(A *L,char &e)
    {
    if(L->top==-1)
    return false;
    else
    {
    e=L->data[L->top];
    return true;
    }
    }
    void trans(char *exp,char postexp[])
    {
    char e;
    A *Optr;
    InitStack(Optr);
    int i = 0;
    while(* exp!='')
    {
    switch(* exp)
    {
    case '(':
    Push(Optr,'(');
    exp++;
    break;
    case ')':
    Pop(Optr,e);
    while(e!='(')
    {
    postexp[i++]=e;
    Pop(Optr,e);
    }
    exp++;
    break;
    case '+':
    case '-':
    while(!StackEmpty(Optr))
    {
    GetTop(Optr,e);
    if(e!='(')
    {
    postexp[i++]=e;
    Pop(Optr,e);
    }
    else
    break;
    }
    Push(Optr,*exp);
    exp++;
    break;
    case '*':
    case '/':
    while(!StackEmpty(Optr))
    {
    GetTop(Optr,e);
    if(e=='*'||e=='/')
    {
    postexp[i++]=e;
    Pop(Optr,e);
    }
    else
    break;
    }
    Push(Optr,*exp);
    exp++;
    break;
    default:
    while(* exp>='A'&&*exp<='Z')
    {
    postexp[i++]=*exp;
    exp++;
    }
    //postexp[i++]='#';
    }
    }
    while(!StackEmpty(Optr))
    {
    Pop(Optr,e);
    postexp[i++]=e;
    }
    postexp[i]='';
    DestoryStack(Optr);
    }
    /*
    void Display(char a[])
    {
    for(int i=0;a[i]!='#';i++)
    {
    cout<<a[i]<<endl;
    }
    }
    */
    int main()
    {
    char exp[50],postexp[MaxSize];
    char h;
    for(int i=0;i<50;i++)
    {
    h=getchar();
    if(h=='#')
    {
    exp[i]='';
    break;
    }
    else
    {
    exp[i]=h;
    }
    }

    trans(exp,postexp);
    //Display(postexp);
    printf("%s ",postexp);
    return 0;
    }

  • 相关阅读:
    django显示SQL语句
    AngularJS国际化配置
    Django的国际化
    21天实战caffe笔记_第二天
    图像处理之CSC性能优化(源码)
    图像处理之CSC色彩转换
    21天实战caffe笔记_第一天
    图像处理之色彩转换(CCM)
    图像处理之FPN校正
    图像处理之坏点校正及源码实现
  • 原文地址:https://www.cnblogs.com/xww115/p/10567858.html
Copyright © 2011-2022 走看看