博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

小菜自写中缀表达式转换为后缀表达式

Posted on 2010-04-28 21:26  ccmfc  阅读(256)  评论(0)    收藏  举报

昨天写了一个中缀表达式转换为后缀达式(在本子上。。。呵呵。没电嘛)。。。

今天上午在机子上运行了一下。。。看下没有问题。。。赶快上课去了。

到下午来黑盒测试下。。。汗。。。没考虑小数点。。。。我无语了。。。

#include <iostream>
using namespace std;

#define MAXSIZE 256
#define Template template<typename T>


Template
class Stack
{
public:
 Stack();
 bool IsEmpty();
 bool Push(Stack *s, T e);
 bool Pop(Stack *s);
 T Top(Stack *s);

private:
 int m_Count;
 T m_Save[MAXSIZE];
};

Template
Stack<T>::Stack()
{
 this->m_Count = 0;
}

Template
bool Stack<T>::IsEmpty()
{
 if(this->m_Count == 0)
  return true;
 return false;
};


Template
bool Stack<T>::Push(Stack *s, T e)
{
 if(s->m_Count >= MAXSIZE)
 {
  cout<<"\n压栈失败\n";
  return false;
 }
 else
 {
  ++s->m_Count;
  s->m_Save[s->m_Count] = e;
  return true;
 }
}

Template
bool Stack<T>::Pop(Stack *s)
{
 if(s->m_Count <= 0 )
 {
  cout<<"\n弹栈失败\n";
  return false;
 }
 else
 {
  --s->m_Count;
 }
 return true;
}

Template
T Stack<T>::Top(Stack *s)
{
 if(s->m_Count==0)
 {
  cout<<"\n取栈顶元素失败\n";
  return 0;
 }
 else
 {
  return s->m_Save[s->m_Count];
 }
}


//判断优先级
//如果tStack<tPtr  返回1即入栈
//如果tStack>tPtr  返回 -1或者是同级 返回 0  先出栈(tStack)后入栈(tPtr)
int Judge(char tStack, char tPtr)
{
 switch(tStack)
 {
 case '+':
 case '-':
  {
   switch(tPtr)
   {
   case '+':
   case '-':
    return 0;
   // break;
   case '*':
   case '/':
    return 1;
   // break;
   }
  }
  break;

 case '*':
 case '/':
  {
   switch(tPtr)
   {
   case '+':
   case '-':
    return -1;
   case '*':
   case '/':
    return 0;
   }
  }
  break;
 default:
  break;
 }
 return 1;
}

//转换成后缀表达式
Template
int Change(T *tPtr)
{
 T *tChar = tPtr;
 int tRet;
 int i=0;
 char temp;
 Stack<char> s;
 char tSave[MAXSIZE];
 while(*tChar)
 {
  if(*tChar == ' ')
  {
   ++tChar;
   continue;
  }
  else if(::isdigit(*tChar))
  {
   while((*tChar >= '0') && (*tChar <= '9'))
   {
    tSave[i] = *tChar;
    ++i;
    ++tChar;
   }
   --tChar;
   tSave[i] = ' ';
   ++i;

  }
  else if((*tChar == '+') || (*tChar == '-') || (*tChar == '*')||
       (*tChar == '/') || (*tChar == '(') || (*tChar == ')'))
  {
   if(s.IsEmpty() || *tChar == '(')
   {
    s.Push(&s,*tChar);
   }
   else
   {
    if( *tChar == ')')
    {
     while('(' != s.Top(&s))
     {
      tSave[i] = s.Top(&s);
      ++i;
      tSave[i] = ' ';
      ++i;
      s.Pop(&s);
     }
     s.Pop(&s);
    }
    else
    {
    
     temp = s.Top(&s);
     tRet = Judge(temp,*tChar);
     if(tRet == 1)
     {
      s.Push(&s,*tChar);
     }
     else
     {
      tSave[i] = s.Top(&s);
      ++i;
      tSave[i] = ' ';
      ++i;
      s.Pop(&s);
      s.Push(&s,*tChar);
     }
    }
   }
  }
  tChar++;
 }

 while(!s.IsEmpty())
 {
  tSave[i] = s.Top(&s);
  ++i;
  tSave[i] = ' ';
  ++i;
  s.Pop(&s);
 }
 strncpy(tPtr, tSave, i);
 tPtr[i] = '#';
 return 1;
}

 

int main( int argc, char *argv[])
{
 int i=0;
 char tExp[MAXSIZE];
 Stack<char> s;

 cout<<"请输入中缀表达式:";
 gets(tExp);

 int tRet = Change(tExp);
 cout<<"后缀表达式为:";
 while(tExp[i] != '#')
 {
  cout<<tExp[i];
  ++i;
 }
 cout<<endl;
 return EXIT_SUCCESS;
}