前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++新闻检索类

C++新闻检索类

作者头像
Enterprise_
发布2019-02-21 17:22:05
4680
发布2019-02-21 17:22:05
举报
文章被收录于专栏:小L的魔法馆
代码语言:javascript
复制
研究长字符串快速全文检索技术,实现某电力公司新闻中心新闻稿件全文检索统计系统。
1、  设计实现适合新闻稿件的基础类库
2、  新闻稿件全文检索功能实现
3、  新闻稿件按照关键字统计查询

代码如下 Paper.h

代码语言:javascript
复制
#pragma once
#ifndef PAPER_H
// we're here only if PAPER_H has not yet been defined 
#define PAPER_H

// Definition of PAPER_H class and related functions goes here
#include <iostream>
#include <string>
#include <vector>
#define For(o,n) for (int i = 0; i < n; i++)
using namespace std;
class Paper {
protected:
    int yy, mm, dd;
    string unite;
    string title;
    //there declarations for kinds of news
public:
    friend istream& operator >> (istream&, Paper&);
    friend ostream& operator <<(ostream&, const Paper &);
    Paper();
    Paper(const Paper &asp);
    virtual ~Paper() {}
    void SetYMDear(const int year, const int month, const int day);
    int GetYear()const;
    int GetMonth()const;
    int GetDay()const;
    string GetTitle()const;
    string GetUnite()const;
    void SetUnite(const string Unite);
    void SetTitle(const string Title);
    virtual int CountWords(const string text) = 0;
};
class Word :virtual public Paper {
protected:
    string contain;
public:
    friend istream& operator >> (istream&, Word&);
    friend ostream& operator <<(ostream&, const Word &);
    Word();
    Word(const string con);
    ~Word() {

    }
    int CountWords(const string text);
};
class vedio :virtual public Paper {
protected:
    string act;
    string Vname;
public:
    friend istream& operator >> (istream&, vedio&);
    friend ostream& operator <<(ostream&, const vedio&);
    vedio();
    ~vedio() {}
    vedio(const string ac, const string vn);
    string GetAct()const;
    string GetVName()const;
    int CountWords(const string text);
};
class Record :virtual public vedio {
protected:
    string ReName;
public:
    friend istream& operator >> (istream&, Record&);
    friend ostream& operator <<(ostream&, const Record &);
    Record();
    ~Record() {}
    Record(const string re);
    int CountWords(const string text);
};
class Poem :virtual public Word {
public:
    Poem() {}
    ~Poem() {}
    int CountWords(const string text);
};
class Other :virtual public Word {
public:
    Other() {}
    ~Other() {}
    int CountWords(const string text);
};
#endif

Realize.h

代码语言:javascript
复制
#include"Paper.h"
istream& operator >> (istream&in, Paper&rhp) {
    in >> rhp.yy >> rhp.mm >> rhp.dd >> rhp.title >> rhp.unite;
    in.ignore();
    //getline(in, rhp.contain);
    return in;
}
ostream& operator <<(ostream&out, const Paper &rhp) {
    out << rhp.yy << endl
        << rhp.mm << endl
        << rhp.dd << endl
        << rhp.title << endl
        << rhp.unite << endl;
    //  << rhp.contain << endl;
    return out;
}
Paper::Paper() :yy(0), mm(0), dd(0), unite("xxx"), title("!!!") {}
Paper::Paper(const Paper &asp) { yy = asp.yy; mm = asp.mm; dd = asp.dd; unite = asp.unite; title = asp.title; }
void Paper::SetYMDear(const int year, const int month, const int day) { yy = year, mm = month, dd = day; }
void Paper::SetTitle(const string Title) { title = Title; }
void Paper::SetUnite(const string Unite) { unite = Unite; }
int Paper::GetDay()const { return dd; }
int Paper::GetYear()const { return yy; }
int Paper::GetMonth()const { return mm; }
string Paper::GetTitle()const { return title; }
string Paper::GetUnite()const { return unite; }

istream& operator >> (istream&in, Word&rhp) {
    in >> rhp.yy >> rhp.mm >> rhp.dd >> rhp.title >> rhp.unite;
    in.ignore();
    getline(in, rhp.contain);
    return in;
}
ostream& operator <<(ostream&out, const Word &rhp) {
    out << rhp.yy << endl
        << rhp.mm << endl
        << rhp.dd << endl
        << rhp.title << endl
        << rhp.unite << endl
        << rhp.contain << endl;
    return out;
}
Word::Word() :contain("xxx") {

}
Word::Word(const string con) { contain = con; }
int Word::CountWords(const string text) {
    int ans = 0;
    auto pos = contain.find(text);
    while (pos != string::npos) {
        ans++;
        pos = contain.find(text, ++pos);
    }
    return ans;
}
istream& operator >> (istream&in, vedio&rhp) {
    in >> rhp.yy >> rhp.mm >> rhp.dd >> rhp.title >> rhp.unite >> rhp.Vname;
    in.ignore();
    getline(in, rhp.act);
    return in;
}
ostream& operator <<(ostream&out, const vedio &rhp) {
    out << rhp.yy << endl
        << rhp.mm << endl
        << rhp.dd << endl
        << rhp.title << endl
        << rhp.unite << endl
        << rhp.act << endl
        << rhp.Vname << endl;
    return out;
}
vedio::vedio() :act("xxx"), Vname("XXXX") {}
vedio::vedio(const string ac, const string vn) { act = ac, Vname = vn; }
string vedio::GetAct()const { return act; }
string vedio::GetVName()const { return Vname; }
int vedio::CountWords(const string text) {
    int ans = 0;
    int pos = act.find(text);
    while (pos != string::npos) {
        ans++;
        pos = act.find(text, ++pos);
    }
    return ans;
}
istream& operator >> (istream&in, Record&rhp) {
    in >> rhp.yy >> rhp.mm >> rhp.dd >> rhp.title >> rhp.unite >> rhp.ReName;
    in.ignore();
    getline(in, rhp.act);
    return in;
}
ostream& operator <<(ostream&out, const Record &rhp) {
    out << rhp.yy << endl
        << rhp.mm << endl
        << rhp.dd << endl
        << rhp.title << endl
        << rhp.unite << endl
        << rhp.act << endl
        << rhp.ReName << endl;
    return out;
}
Record::Record() :ReName("XXXX") {}
Record::Record(const string re) { ReName = re; }
int Record::CountWords(const string text) {
    return vedio::CountWords(text);
}
int Poem::CountWords(const string text) {
    int ans = 0;
    string flag = GetTitle();
    auto pos = flag.find(text);
    while (pos != string::npos) {
        ans++;
        pos = flag.find(text, ++pos);
    }
    return ans;
}
int Other::CountWords(const string text)
{
    int ans = 0;
    string flag = GetTitle();
    auto pos = flag.find(text);
    while (pos != string::npos) {
        ans++;
        pos = flag.find(text, ++pos);
    }
    return ans;
}
void ShowSTD() {
    cout << "Please input kinds of News in follow fomat." << endl
        << "Word or Poem or Other News:" << endl
        << "Year Month Day Title Unite\nContain(Contain at the end of ENTER)" << endl
        << "vedio or Record News:" << endl
        << "Year Month Day Title Unite Filename\nAbstract(Abstract at the end of ENTER)" << endl;
}

test.cpp

代码语言:javascript
复制
#include"Realize.h"
int main() {
    ShowSTD();
    Paper *tp[5];
    Word a1;
    vedio a2;
    Record a3;
    Poem a4;
    Other a5;
    string flag;
    int n = 5;
    cout << "\nNow Inpt!!\nWord:";
    cin >> a1;
    tp[0] = &a1;
    cout << "vedio:";
    cin >> a2;
    tp[1] = &a2;
    cout << "Record:";
    cin >> a3;
    tp[2] = &a3;
    cout << "Poem:";
    cin >> a4;
    tp[3] = &a4;
    cout << "Other:";
    cin >> a5;
    tp[4] = &a5;
    cout << "Please enter the word you want to find:";
    cin >> flag;
    For(o, n)cout << tp[i]->CountWords(flag) << endl;
    cout << endl;
    For(o, n)cout << *(tp[i]) << endl;
    return 0;
}

测试截图

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年05月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档