设计三的源代码

由于有线性表和二叉树两种方式,所以我将线性表放在头文件里

目录

一、头文件:Linear.h

二、主函数


一、头文件: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;
    }

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值