• 大小: 7KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-05-26
  • 语言: C/C++
  • 标签: CYK算法  C++  

资源简介

CYK的C++实现,输入的产生式放在文件中,各位需要将产生式以如下格式输入"S AB",文件名自己起了。

资源截图

代码片段和文件信息

#include                                                        //基本输入输出操作等 
#include                                                             //关联容器 
#include                                                        //迭代器 
#include                                                         //文件操作 
#include                                                         //Dev-C++特有 
/** 使用关联容器Map
    存在一个问题,就是最终求出的V[i][j]中可能存在重复的值,如SASC,
    不符合经典集合论的要求 
*/
using namespace std;                                                      
typedef map::value_type valType;                            //自定义数据类型map::value_type
typedef string* *pString;                                                  //定义指向string*的指针 
map P;                                                      //存储产生式的关联容器 

/*------------------------------------读取产生式并存入对应的数据结构中-------------------------------------*/
void readFromFile(){                                                      //从文件中读取产生式 
     string key;                                                          //乔姆斯基范式中产生式右边部分作为key
     string value;                                                        // 左边的非终结符作为value
     string input;                                                        //临时存放读入的字符串
     int location;                                                        //读取文件时存储空格位置 
     ifstream in(“Production.txt“);                                       //以只读方式打开文件
      
     if(!in){                                                             //文件打开失败的异常处理 
         cerr<<“文件打开失败“<         exit(-1);        
     }
     while(in.peek()!=EOF){                                               //开始读取文件内的内容,存入Production 
         getline(ininput);                                               //首先读取一行
         location=input.find(‘ ‘);                                        //查找空格位置 
         value=input.substr(0location);                                  //乔姆斯基范式中产生式左边非终结符作为value
         key=input.substr(location+1input.size());                       //右边部分作为key 
         if(P.count(key)==0){                                             //关联容器中还没有key对应的非终结符 
             P.insert(valType(keyvalue));                                //插入键值对 
         }                  
         else{
             P[key]+=value;                                                //关联容器中已存在key对应的非终结符 
         }
     }  
     /**
     for(map::iterator it=P.begin();it!=P.end();it++)      //测试用 
        cout<<(*it).second<<“-->“<<(*it).first<}

/*----------------------------------------从控制台读入需判断的字符串--------------------------------------*/ 
void readFromConsole(string& w){                                           //返回字符串的长度,以定义数组V的大小 
     cout<<“请输入要判断的字符串:“<     cin>>w;                                                               //从控制台输入需判

评论

共有 条评论