2012-2013学年第二学期《C++程序设计》上机考试题

关键词统计

当大家需要上网查询资料的时候,自然而然地会进入类似Google或Baidu等提供搜索服务的网站,输入要查找信息的关键词,搜索引擎会在数据库中进行搜寻,如果找到与用户要求内容相符的网站,便采用特殊的算法——通常根据网页中关键词的匹配程度、出现的位置、频次、链接质量等计算出各网页的相关度及排名等级,然后根据关联度高低,按顺序将这些网页链接返回给用户。

你打算暑期到某个搜索网站公司去实习,面试官要求你用C++语言编写一个程序,能迅速准确地发现某个网页(英文网页,只有文字)是否含有某些特定的关键词,并统计出所有关键词出现的次数。

已知每个关键词只含字母(’A’-‘Z’和’a’-‘z’),不区分大小写。但面试官想测试一下你的编程水平有多高,他把关键词混在一些非字母字符中。例如:关键词是abroad, 那么如果网页里出现yA B#ro7 a$ 4D8z也算关键词abroad出现一次(关键词的前后可出现字母,也可出现非字母字符,关键词的各字母之间只能出现0个或多个非字母字符)。

程序名称:keyword.cpp 输入文件(keyword.in)

第1行:只有1个正整数N(0< N <=10),表示关键词的个数;

第2行至第N+1行:关键词,一行一个关键词(一个关键词最大长度不超过40); 第N+2行到最后:网页内容

一个网页不会超过2,000字符(包含所有字符)。网页可能一行或多行,但是每行都不超过80个字符(不包括最后的换行符)。

输出文件(keyword.out)

输出只有一行,这一行只包含一个整数,表示网页中所有关键词出现的次数。如果整个网页中没有关键词,输出结果为0。

输入样例

2

AbRoad

sTudy

So you’re leaving tosTu d78 y @aBro# Ad! You are about to have the time of your life. For most students,st$u dying #ab %Ro 9adis the single most memorable thing they’ll do in their university life.

输出样例

4

2018.11.10号判断关键字有问题,搞了一天才做出来。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;

class keyword{
    private:
        vector<string> vec;
        string text;
    public:
    void read();
    void write();
    int checkWord();
};

void keyword::read()
{
    string s;
    ifstream rst("keyword.in");
    if(!rst)
    {
        cout<<"文件打不开"<<endl;
        return;
    }
    getline(rst,s);
    int x = atoi(s.c_str());
    if(x<=0 && x>10)
    {
        cout<<"文件输入关键字个数错误"<<endl;
        return;
    }
    
    string str;
    for(int i=0;i< x;i++)
    {
        getline(rst,str);
        for (int j= 0; j< str.size(); j++)
        {
            if ((str[j] < 'A' && str[j] > 'Z') || (str[j] < 'a' && str[j] > 'z'))
            {
                cout<<"文件关键字错误"<<endl;
                return;
            }
        }
        vec.push_back(str);
    }
    while(getline(rst,str)){
        text += str;
    }
    rst.close();
}

int keyword::checkWord()
{
    string tmp;
    int i=0;
    int j=0;
    int t=0;    //判断字符串和key是否相等
    int result=0; //检查结果
    vector<string>::iterator p;
    for ( p= vec.begin(); p != vec.end(); ++p)
    {
        tmp = *p;
        for (;j< tmp.size();j++)
        {
            for (;i< text.length();i++)
            {
                if ((text[i]>= 'A' && text[i] <= 'Z')||
                    (text[i]>= 'a' && text[i] <= 'z'))
                {
                    if(tolower(text[i]) == tolower(tmp[j])){
                        t++;
                        break;
                    }
                }
                if(t==tmp.size()-1){
                    t = 0;
                    j=0; //key值重新循环
                    result++;
                }
            }
        }
    }
    return result;
}

void keyword::write(int p)
{
    ofstream wst("keyword.out");
    if(!wst)
    {
        cout<<"文件创建失败"<<endl;
        return;
    }
    //cout<< r <<endl;
    if (p > 0)
    {
        wst << p;
    } else
    {
        wst << 0;
    }
    wst.close();
}

int main()
{
    keyword k;
    k.read();
    int r = k.checkWord();
    k.write(r);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值