实验3-源文件(g.cpp+subg.h+subg.cpp+test.cpp)

来源:互联网 发布:erp软件销售 编辑:程序博客网 时间:2024/05/22 03:05

//g.cpp

//实验一:设计文法的数据结构

#include<string.h>
#include<iostream.h>

typedef struct Snode{
 char* s;
 struct Snode *nexts;
}Snode;

typedef struct Cnode{
 char c;
 struct Cnode *nextc;
 struct Snode *define;
}Cnode;

class G{
 public: 
  Cnode *vn,*vt,*s,*p;
  
  G(){
   vn=0;
   vt=0;
   s=0;
   p=0;
  }

  void insert_into_vn(char newc){
   Cnode *p;
   Cnode *lastvn;
   p=new Cnode;
   p->c=newc;
   p->nextc=0;
   p->define=0;
   if(!vn){
    vn=p;
    set_s(vn);
    set_p(vn);
   }else{
    lastvn=vn;
    while(lastvn->nextc)
     lastvn=lastvn->nextc;
    lastvn->nextc=p;
   }
  }

  void insert_into_vt(char newc){
            Cnode *p;
   Cnode *lastvt;
   p=new Cnode;
   p->c=newc;
   p->nextc=0;
   p->define=0;
   if(!vt)
    vt=p;
   else{
    lastvt=vt;
    while(lastvt->nextc)
     lastvt=lastvt->nextc;
    lastvt->nextc=p;
   }   
  }

  void insert_into_s(char leftvn,char* rights){
   Cnode* pc;
   Snode *ps,*lasts;
   ps=new Snode;
   ps->s=rights;
   ps->nexts=0;
   pc=find_char_inq(vn,leftvn);
   if(!pc){
    insert_into_vn(leftvn);
    pc=find_char_inq(vn,leftvn);
   }
   lasts=pc->define;
   if(!lasts)
    pc->define=ps;
   else{
    while(lasts->nexts) lasts=lasts->nexts;
    lasts->nexts=ps;
   }
  }

  void set_s(Cnode* nows){
   s=nows;
  }

  void set_p(Cnode* nowp){
   p=nowp;
  }
  
  Cnode* find_char_inq(Cnode* q,char c){
   Cnode* p=q;
   if(!p) return 0;
   while(p&&p->c!=c)
    p=p->nextc;
   if(!p) return 0; else return p;
  }

  void printG(){
   Cnode *pc;
   Snode *ps;
   pc=vn;
   while(pc){
    cout<<pc->c<<"->";
    ps=pc->define;
    while(ps){
     cout<<ps->s;
     ps=ps->nexts;
     if(ps) cout<<"|";
    }
    cout<<"/n";
    pc=pc->nextc;
   }

  }
};

//subg.h

#include<fstream.h>
#include "g.cpp"

class sub_G:public G{//此类的实现在文件subg.cpp中
public:
 void read_G_from(ifstream &infile);
 //此函数用于将文法从文件输入流infile读入到本类对象中
 
 void write_G_to(ofstream &outfile);
 //此函数用于将本对象表示的文法写入到文本文件的输出流outfile中

 int remove_leftDG();
 /*本对象文法左递归消除函数
   实现方法参考书中70页算法,
   在此处用C++实现书中算法即可*/


};

//subg.cpp

#include"subg.h"

void sub_G::read_G_from(ifstream &infile){
 
}

void sub_G::write_G_to(ofstream &outfile){
 
}

int sub_G::remove_leftDG(){
 //
 return 0;
}

//test.cpp

#include<iostream.h>
#include"subg.h"

void main(){
 //利用此处main函数实现实验3测试。
}