C言語データ構造の接尾辞ツリー回転接尾辞ツリーの例

6039 ワード

C言語データ構造の接尾辞ツリー回転接尾辞ツリーの例
接尾辞式a+b*c*(d-e/f)を接尾辞に変換する形式abc*def/-+
接尾辞式はかなり役に立つので、接尾辞式に変換して評価するのは簡単です.では、どのように変換すればいいのでしょうか.
ネット上でこの方面の資料について大いに探して、すべてのデータ構造の本の中ですべてこのアルゴリズムに言及して、このアルゴリズムの中で、スタックのこのデータ構造を使います.
1、肝心なのは比較演算子の優先度で、誰の優先度が高くて、誰が前の上の式の中で現れて、括弧がある時括弧の優先度が最も高くて、*/に次いで、+-最後.上の式では+の優先度が*より高くないため、接尾辞式では*が+の前に現れ、
2,オペランドに遭遇したときは常に直接出力し,何の比較もしない
3、左かっこに遭遇するといつも直接スタックに入り、右かっこに遭遇するといつもスタックを弾き、左かっこに遭遇するまで弾き続ける.
4、オペレータに遭遇したときは、まずこのオペレータとその前のオペレータを優先度を比較し、前の優先度を上回ればスタックを押さえ、前のオペレータの優先度を下回れば、前の優先度がそれより高いか等しいかの順序を弾き出し、優先度がそれより低いかスタックの頂上に着くまで弾き出す.オペレータはスタックに再圧入されます.
以上の4つのルールを知ってコード実装を設計することができます.
コードは次のとおりです.

#include 
#include 
#include 
#include 
using namespace std; 
void InerStringDevide(string InerStr,string DeviStr[],int &num) 
{ 
  int count,i; 
  int numbe=InerStr.size(); 
  for(i=0;i=numbe) 
          break; 
      } 
      count++; 
    } 
  } 
  num=count; 
} 
void InerTreeToPostTree(string InerStr,string &PostStr) 
{ 
  PostStr[0]='\0'; 
  mapOpC; 
  typedef map::value_type ValType; 
  OpC.insert(ValType('+',1)); 
  OpC.insert(ValType('-',1)); 
  OpC.insert(ValType('*',2)); 
  OpC.insert(ValType('/',2)); 
  OpC.insert(ValType('%',2)); 
  OpC.insert(ValType('^',3)); 
  OpC.insert(ValType('(',-1)); 
  OpC.insert(ValType(')',0)); 
  int num,i,j,StrNum; 
  num=InerStr.size(); 
  string *DevedeStr=new string[num]; 
  InerStringDevide(InerStr,DevedeStr,StrNum); 
 
  stack ChStack; 
  int count=0; 
  for(int i=0;i=OpC[DevedeStr[i][0]]&&OpC[TopCh]!=-1) 
            { 
              if(!PostStr.empty()) 
              { 
                PostStr.push_back(' '); 
              } 
              PostStr.push_back(TopCh); 
              ChStack.pop(); 
              if(!ChStack.empty()) 
                TopCh=ChStack.top(); 
              else 
                break; 
            } 
            ChStack.push(DevedeStr[i][0]); 
          } 
        } 
      } 
    } 
    //            
    else 
    { 
      int DevideSize=DevedeStr[i].size(); 
      if(!PostStr.empty()) 
      { 
        PostStr.push_back(' '); 
      } 
      for(int j=0;j 
 

以上がヘッダファイルh.このファイルの役割は、接尾辞文字列を入力し、接尾辞文字列を出力することです.接尾辞文字列にはスペースがなく、接尾辞文字列にはスペースがあります.ヘッダファイルのもう1つの関数は、文字列を文字列配列に分け、数値と演算子を格納することです.

#include 
#include 
#include 
using namespace std; 
void StringDevide(string str,int &num,string st1[]) 
{ 
  for(int i=0;i<100;i++) 
    st1[i][0]='\0'; 
  int n=str.size(); 
  int j=0,count=0; 
  for(int i=0;i m_num; 
 
public: 
  InterTreeComputer(string expression):m_expresion(expression) 
  {} 
  //              
  bool IsOperator(char ch)const; 
  //            
  void GetOperands(int &left,int &right); 
  //           ch     
  int computer(int left,int right,char ch)const; 
  //      
  string GetPostoperation()const; 
  void SetPostoperator(); 
  //           
  int Evaluate(); 
}; 
bool InterTreeComputer::IsOperator(char ch)const 
{ 
  switch(ch) 
  { 
  case '+': 
  case '-': 
  case '*': 
  case '/': 
  case '%': 
  case '^': 
    return 1; 
  default: 
    return 0; 
  } 
} 
void InterTreeComputer::GetOperands(int &left,int &right) 
{ 
  if(m_num.empty()) 
  { 
    cout<0) 
    { 
      value*=left; 
      right--; 
    } 
    return value; 
    break; 
  } 
} 
string InterTreeComputer::GetPostoperation()const 
{ 
  return m_expresion; 
} 
void InterTreeComputer::SetPostoperator() 
{} 
int InterTreeComputer::Evaluate() 
{ 
  string *str=new string[100]; 
  int num; 
  StringDevide(m_expresion,num,str); 
  for(int i=0;i 
 

以上のコードはInerTreeComputerである.hヘッダファイルは、接尾辞式を入力し、その式の値を計算する役割を果たす.

#include 
using namespace std; 
#include 
#include 
#include"InterTreeComputer.h" 
#include"InerTreeToPostTree.h" 
int main() 
{ 
  string str="3*(4-2^5)+6"; 
  string st1="2 3 ^ 1 +"; 
  string st2="2 2 3 ^ ^ 4 /"; 
  string StrRe; 
  InerTreeToPostTree(str,StrRe); 
  InterTreeComputer Comp(StrRe); 
  cout< 
 

テストファイルは、以上の2つのヘッダファイルをテストしました.
以上がデータ構造C言語データ構造の接尾辞ツリーの接尾辞ツリーの例であり、疑問があれば伝言を残したり、当駅のコミュニティに行って討論を交流したりして、読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝して、みんなは共に進歩します!