zoukankan      html  css  js  c++  java
  • 小菜自写中缀表达式转换为后缀表达式

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

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

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

    #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;
    }

  • 相关阅读:
    springboot学习(一):创建项目
    CS 224D lecture 5 笔记 分类: CS224D notes 2015-07-18 00:04 3人阅读 评论(0) 收藏
    CS224D Lecture 4 札记
    Ubuntu Compress a big file into plenty of small parts(压缩文件为几个小部分,) 分类: Ubuntu Trick 2015-07-13 21:29 1人阅读 评论(0) 收藏
    CS 224D Lecture 3札记 分类: CS224D notes 2015-07-12 12:03 5人阅读 评论(0) 收藏
    Ubuntu设置环境变量 分类: Ubuntu Trick 2015-07-12 01:03 6人阅读 评论(0) 收藏
    CS224D Lecture2 札记 分类: CS224D notes 2015-07-09 16:40 9人阅读 评论(0) 收藏
    CS224D Lecture 1 札记 分类: CS224D notes 2015-07-07 21:48 20人阅读 评论(0) 收藏
    Ubuntu Python Setup 分类: Ubuntu Trick 2015-07-03 23:26 5人阅读 评论(0) 收藏
    Ubuntu Matlab 2014b Setup 分类: Ubuntu Trick 2015-07-03 23:25 12人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/ccmfc/p/1723292.html
Copyright © 2011-2022 走看看