主题:[讨论]单词索引
/*1 单词索引
编写程序,从键盘读入一个文本文件名字(可带路径),
为该文件中的所有单词建立一个词汇索引。
按字母顺序显示所有单词(仅一次),后面紧跟着它们所在的行号。和单词出现的次数
大写与小写字母被认为是相同的。例如,对于下列的输入文件:
To be or
not to be,
that is the question.
产生的词汇索引如下:
be 1 2
is 3
not 2
or 1
question 3
that 3
the 3
to 1 2*/
#include<iostream>
#include<fstream> //IO文件流类
#include<string>
#define MAX 10
//#include<cctype>
using namespace std;
typedef struct BiTNode{
string data;
struct BiTNode *lchild,*rchild;
int count;
int hang;
}BiTNode,*BiTree;
class Wordlist
{
static int hang1;
private:
int length;
int hang;
public:
BiTree head;
Wordlist(){head=NULL;length=0;}
//Wordlist(int i){}
void f(){hang1++;hang=hang1;cout<<hang;}
int Length(){ return length;}
void InOrder(BiTree bt);
int Insert(BiTree &q,string temp,int );
void show();
BiTree search(BiTree bt,string temp);
int compare(string p,string w);
void insert(string temp,int p);
int get_count(string temp);
};
int Wordlist::Insert(BiTree &q,string temp,int p=1)
{
if(q==NULL)
{
q=new BiTNode;
length++;
q->data=temp;
q->count=p;
q->lchild=NULL;
q->rchild=NULL;
return 0;
}
if(compare(temp,q->data)==-1)
Insert(q->lchild,temp);
if(compare(temp,q->data)==1)
Insert(q->rchild,temp);
if(compare(temp,q->data)==0)
q->count++;
}
void Wordlist::InOrder(BiTree bt)
{
if(bt==NULL)
return ;
InOrder(bt->lchild);
cout<<bt->data<<" "<<bt->count<<" "<<bt->hang<<endl;
InOrder(bt->rchild);
}
BiTree Wordlist::search(BiTree bt,string temp)
{
BiTree p;
if(bt){
if(compare(bt->data,temp)==0)
return bt;
if(bt->lchild!=NULL)
{
p=search(bt->lchild,temp);
if(p)
return p;
}
else
if(bt->rchild!=NULL){
p=search(bt->rchild,temp);
if(p)
return p;
}
}
return NULL;
}
int Wordlist::compare(string p,string w)
{
for(int n=0;n<8;n++)
{
if(p[n]<w[n])
return (-1);
if(p[n]>w[n])
return 1;
}
return 0;
}
void Wordlist::insert(string temp,int p)
{
Insert(head,temp,p);
}
int Wordlist::get_count(string temp)
{
BiTree p;
p=search(head,temp);
if(p)
return p->count;
else
{
cout<<"没有此词"<<endl;
return 0;
}
}
void Wordlist::show()
{
cout<<endl;
cout<<"Word"<<" "<<"Count"<<endl;
cout<<"---------------------------"<<endl;
InOrder(head);
cout<<"---------------------------"<<endl;
cout<<"The file contains "<<Length()<<" distinct words"<<endl;
}
int Wordlist::hang1=1;
int main()
{
ifstream txtfile; //ifstream:输入文件流类
Wordlist w;
txtfile.open("D:\\wo.txt");
if(!txtfile)
{
cerr<<"文件打开失败"<<endl;
exit(1);
}
string line ;
while(!txtfile.eof())
{
txtfile>>line;
int n=line.size();
if(ispunct(line[n-1]))
line[n-1]=NULL;
cout<<line<<" "; //输出文件中的内容
if(line!="enter")
{w.Insert(w.head,line); }
else
w.f();
}
cout<<endl;
txtfile.close();
cout<<"'wo' 的个数"<<w.get_count("wo")<<endl;
w.show();
return 0;
}
怎么也不对,我是个小鸟,望各位高手指点!
编写程序,从键盘读入一个文本文件名字(可带路径),
为该文件中的所有单词建立一个词汇索引。
按字母顺序显示所有单词(仅一次),后面紧跟着它们所在的行号。和单词出现的次数
大写与小写字母被认为是相同的。例如,对于下列的输入文件:
To be or
not to be,
that is the question.
产生的词汇索引如下:
be 1 2
is 3
not 2
or 1
question 3
that 3
the 3
to 1 2*/
#include<iostream>
#include<fstream> //IO文件流类
#include<string>
#define MAX 10
//#include<cctype>
using namespace std;
typedef struct BiTNode{
string data;
struct BiTNode *lchild,*rchild;
int count;
int hang;
}BiTNode,*BiTree;
class Wordlist
{
static int hang1;
private:
int length;
int hang;
public:
BiTree head;
Wordlist(){head=NULL;length=0;}
//Wordlist(int i){}
void f(){hang1++;hang=hang1;cout<<hang;}
int Length(){ return length;}
void InOrder(BiTree bt);
int Insert(BiTree &q,string temp,int );
void show();
BiTree search(BiTree bt,string temp);
int compare(string p,string w);
void insert(string temp,int p);
int get_count(string temp);
};
int Wordlist::Insert(BiTree &q,string temp,int p=1)
{
if(q==NULL)
{
q=new BiTNode;
length++;
q->data=temp;
q->count=p;
q->lchild=NULL;
q->rchild=NULL;
return 0;
}
if(compare(temp,q->data)==-1)
Insert(q->lchild,temp);
if(compare(temp,q->data)==1)
Insert(q->rchild,temp);
if(compare(temp,q->data)==0)
q->count++;
}
void Wordlist::InOrder(BiTree bt)
{
if(bt==NULL)
return ;
InOrder(bt->lchild);
cout<<bt->data<<" "<<bt->count<<" "<<bt->hang<<endl;
InOrder(bt->rchild);
}
BiTree Wordlist::search(BiTree bt,string temp)
{
BiTree p;
if(bt){
if(compare(bt->data,temp)==0)
return bt;
if(bt->lchild!=NULL)
{
p=search(bt->lchild,temp);
if(p)
return p;
}
else
if(bt->rchild!=NULL){
p=search(bt->rchild,temp);
if(p)
return p;
}
}
return NULL;
}
int Wordlist::compare(string p,string w)
{
for(int n=0;n<8;n++)
{
if(p[n]<w[n])
return (-1);
if(p[n]>w[n])
return 1;
}
return 0;
}
void Wordlist::insert(string temp,int p)
{
Insert(head,temp,p);
}
int Wordlist::get_count(string temp)
{
BiTree p;
p=search(head,temp);
if(p)
return p->count;
else
{
cout<<"没有此词"<<endl;
return 0;
}
}
void Wordlist::show()
{
cout<<endl;
cout<<"Word"<<" "<<"Count"<<endl;
cout<<"---------------------------"<<endl;
InOrder(head);
cout<<"---------------------------"<<endl;
cout<<"The file contains "<<Length()<<" distinct words"<<endl;
}
int Wordlist::hang1=1;
int main()
{
ifstream txtfile; //ifstream:输入文件流类
Wordlist w;
txtfile.open("D:\\wo.txt");
if(!txtfile)
{
cerr<<"文件打开失败"<<endl;
exit(1);
}
string line ;
while(!txtfile.eof())
{
txtfile>>line;
int n=line.size();
if(ispunct(line[n-1]))
line[n-1]=NULL;
cout<<line<<" "; //输出文件中的内容
if(line!="enter")
{w.Insert(w.head,line); }
else
w.f();
}
cout<<endl;
txtfile.close();
cout<<"'wo' 的个数"<<w.get_count("wo")<<endl;
w.show();
return 0;
}
怎么也不对,我是个小鸟,望各位高手指点!