回 帖 发 新 帖 刷新版面

主题:高手帮帮忙!!

我的中缀表达式转后缀表达式的程序怎么只能输出数字没有输出运算符?我刚学数据结构,高手们能帮我一下吗?谢谢了!!
#include<iostream>
using namespace std;
typedef char SElemType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
bool InitStack(SqStack &s)
{
    s.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if(!s.base)
        return 0;
    s.top=s.base;
    s.stacksize=STACK_INIT_SIZE;
    return 1;
}//构建一个空栈s
bool Push(SqStack &s,SElemType e)
      //进栈
{
    if(s.top-s.base>=s.stacksize)
       {
        s.top=new char;
        s.stacksize++;
    }
    *s.top++=e;
    return 1;
}//添加元素
char Pop(SqStack &s)
{
    SElemType e;
    if(s.top==s.base)
        return 0;
    e=*--s.top;
    return e;
}//取栈顶元素并出栈
bool StackEmpty(SqStack s)
{
    if(s.base==s.top)
        return 1;
    else 
        return 0;
}//判空
int youxianji(char a,SqStack s)
{
    if(s.base==s.top)
        return 1;
    else if(a=='+'||a=='-')
    {
        if(*(s.top-1)=='('||*(s.top-1)=='+'||*(s.top-1)=='-')
            return 1;
        else
            return 0;
    }
    else if(a=='*'||a=='/')
    {
        if(*(s.top-1)=='('||*(s.top-1)=='+'||*(s.top-1)=='-'||*(s.top-1)=='*'||*(s.top-1)=='/')
            return 1;
        else
            return 0;
    }
    else if(a=='(')
    {

            return 1;
    }
    else if(a==')')
    {
        if(*(s.top-1)=='(')

            return 0;
    }
    else
        return 3;
}//优先级函数
bool ExpressionConvert(char *src, char *dest,SqStack &s)
{
    int p=0;
    if( !StackEmpty (s))
    {
        cout<<"转换成逆波兰式失败 ";
        return 0;
    }
    if(StackEmpty (s))
    {
         for(int i=0;src[i]!='\0';i++)
        {
            if(src[i]!='+'&&src[i]!='-'&&src[i]!='*'&&src[i]!='/'&&src[i]!='('&&src[i]!=')')
            {
            dest[p]=src[i];
            p++;
            continue;
            }
            else if((src[i]=='+'&&src[i]=='-'&&src[i]=='*'&&src[i]=='/'&&src[i]=='('&&src[i]==')'))
            {
                if(youxianji(src[i],s)==1)
                {    
                    Push(s,src[i]);
                    continue;
                }
                if(youxianji(src[i],s)==0)
                {
                
                    dest[p]=Pop(s);
                p++;
                i--;
               continue;
                }

            }if(src[i]=='\0')
         {
             dest[p]=Pop(s);
         }
         }
         
        dest[p]='\0';
          return 1;
    }
    return 1;
}
void main()
{
    SqStack s;
    char src[30],dest[30];
    cin>>src;
    s.base=NULL;
    InitStack(s);
    if(ExpressionConvert(src,dest,s))
    {
        for(int k=0;dest[k]!='\0';k++)
        {
            cout<<dest[k];
        }
        cout<<endl;
    }
    else
        cout<<"中缀表达式非法!"<<endl;
}

回复列表 (共1个回复)

沙发

这是我些的求后缀表达式的函数,你参考参考吧
有两个函数需要说明:
In(ch)判断字符ch是否为运算符,是返回1,不是返回0
precede(c,ch)判断运算符c和ch的优先级谁高谁低,若c大于等于ch则返回1,否则返回0.

前面的定义:
typedef struct
{
    char *top;
    char *base;
    int charstacksize;
}SqCharStack;

typedef struct
{
    float *top;
    float *base;
    int floatstacksize;
}SqFloatStack;

函数:
//将表达式转换为后缀表达式
void transForm(SqCharStack *suffix,char exp[])
{
    SqCharStack s;
    char *p,c,ch;
    initCharStack(&s,20);
    charPush(&s,'#');
    p = exp;
    ch = *p;
    while(!charIsEmpty(&s))
    {
        if(!In(ch)) charPush(suffix,ch);
        else
        {
            charPush(suffix,'o');
            switch(ch)
            {
                case '(': charPush(&s,ch); break;
                case ')': c = charPop(&s);
                          while(c!='(')
                          {
                              charPush(suffix,c);
                              c = charPop(&s);
                          }
                          break;
                default: c = charGetTop(&s);
                         while(c&&precede(c,ch))
                         {
                             charPush(suffix,c);
                             c = charPop(&s);
                             c = charGetTop(&s);
                             if(c=='#'&&ch=='#')
                                 break;
                         }
                         if(ch!='#')
                             charPush(&s,ch);
                        break;
            }
        }
        if(ch!='#')
        {
            p++;
            ch = *p;
        }
        else
        {
            ch = charPop(&s);
            charPush(suffix,ch);
        }
    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册