⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithm.cpp

📁 用C++编写的产生文章的程序
💻 CPP
字号:
#include "stdafx.h"
#include "algorithm.h"
#include <cstdlib>
#include<ctime>
using std::iostream ;
using std::cout;
using std::rand;
using std::srand;

void algorithm::build(Prefix &prefix,istream &in )
{
    string buf;
	while(in>>buf)
       add(prefix,buf);
}
void algorithm::add(Prefix &prefix,const string &s)
{
    if(prefix.size ()==NPREF)
	{
	  table[prefix].push_back(s);
	   prefix.pop_front ();
	  
	}
	prefix.push_back (s);
}
void algorithm::generate(int nwords,ostream &out)
{
	Prefix prefix;
	for(int i=0;i<NPREF;i++)
	{
	   add(prefix,NONWORD);
	}
	for(int i=0;i<nwords;i++)
	{
	  vector<string > &suf= table[prefix];
	 // srand((unsigned int) time(0));
      const string &w=suf[rand() % suf.size()];
	  if(w==NONWORD)
		  break;
	  out<<w<<"  ";
	  prefix.pop_front ();
	  prefix.push_back(w); 
	}
  
}   



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -