由于有线性表和二叉树两种方式,所以我将线性表放在头文件里
目录
一、头文件:Linear.h
#ifndef _DEV
#define _DEV
#include
#include
#include
#include
#include
#include
using namespace std;
//线性表节点的定义
typedef struct LNode {
string WordName;
int count;
LNode* next;
}LNode, *LinkList;
//线性表
class Linear{
private:
LNode* first;
public:
//链表的创建 构造函数
Linear() {
first = new LNode();
first->next = NULL;
}
//线性表添加单词
void LinearAdd(string tempWord);
//读取文件
void LinearRead();
//删除频率低于5的单词
void LinearDelete();
//高频写入文件
void LinearWrite();
//一步执行
void ONE();
//计算ASL
void FindASL();
};
//线性表添加单词
void Linear::LinearAdd(string tempWord) {
LNode* p = first, * r;
while (p!=NULL && p->WordName != tempWord ) {
p = p->next;
if (p&&p->WordName == tempWord) { //找到频率加1
(p->count)++;
return;
}
}
//找不到添加
r = new LNode();
r->WordName = tempWord;
r->count = 1;
r->next = first->next;
first->next = r;
}
//读取文件
void Linear::LinearRead(){
//打开文件
fstream in;
in.open("Infile.txt", ios::in);
if (!in.is_open()) {//判断文件是否能打开
cout << "读取文件失败" << endl;
}
//读取字符和添加
string tempWord;
while (in >> tempWord) {
if (tempWord.at(tempWord.length() - 1) < 65|| tempWord.at(tempWord.length() - 1)=='"')//判断单词尾部是否是符号
tempWord.erase(tempWord.length() - 1);
if (tempWord.at(0) == '"') //去头
tempWord = tempWord.substr(1, tempWord.length());
LinearAdd(tempWord);//添加
}
in.close();//关闭文件
}
//删除频率低于5的单词
void Linear::LinearDelete() {
LNode* p = first->next,*s=first;
cout << "低频词汇及其频率" << endl;
while (p) {//结束条件
if(p->count<5){ //小于5删除
cout << p->WordName << ' ' << p->count << endl;
s->next = p->next;
p=s->next;
}
else{
p = p->next;//移动
s = s->next;
}
}
}
//按频率高低读入
void Linear::LinearWrite() {
fstream out;
out.open("Ontfile.txt", ios::out);
if (!out.is_open()) {//判断文件是否能打开
cout << "读取文件失败" << endl;
}
int max=0;
LNode* p = first;
while (p) {
if (p->count > max)
max = p->count;
p = p->next;
}