哈利波特检索
点击下载源代码
【问题描述】
将哈利波特的7本书(txt格式)读入,然后在指定了人名/地名后,显示查询结果,选择指定查询结果序号(选择查询内容),能够显示指定查询结果所在位置前后的一段文字。
【输入形式】
哈利波特的7本书,txt文件
【输出形式】
人名/地名输出:
显示查找到的人名/地名,以及出现的页码和章节,书名, 按照出现的页码顺序显示,每个查询结果都对应序号。
序号 人名/地名 页码 章节 书名
1 Harry 1 1 Harry_Potter_and_the_Chamber_of_Secrets_Book_2
选择查询结果记录项时,显示指定的人名/地名位置前后的一段文字。
选择序号,或者单击查询记录行,能够显示到指定位置人名/地名前后的一段文字。如选择序号1,应显示:
Not for the first time, an argument had broken out over breakfast at number four, Privet Drive. Mr. Vernon Durs-
ley had been woken in the early hours of the morning by a loud, hooting noise from his nephew Harry’s room.
【其他要求】
其他要求:
界面无要求,可以选择图形界面,也可以只是罗列查询结果即可。支持输入人名/地名,显示查询结果。选择序号,或者单击查询记录行,能够显示到指定位置人名/地名前后的一段文字。如选择序号1,应显示:
提交方式,需要提供一个word文档,对程序设计和实现结果进行说明。模板参看附件。还需要提供源程序代码。
【解答】
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
#include<time.h>
using namespace std;
bool daxie(string a)
{
if (a[0] == 'C')
return true;
return false;
}
string name, temp, he[10000];
int chapter;
char a[10000];
int index = 0, page = 1, ix;
void magic(string prename, string strname)
{
page = 1;
chapter = 1;
name = strname;
ifstream infile;
string txtname = prename + ".txt";
infile.open(txtname.c_str());
if (!infile)
{
cerr << "File could not be open!" << endl;
abort();
}
while (infile.getline(a, 10000))
{
temp = a;
if (temp[0] > '0'&&temp[0] <= '9')
page++;
if (temp.find("Chapter") != string::npos&&daxie(temp) || temp.find("CHAPTER") != string::npos&&daxie(temp))
{
chapter++;
}
if (temp.find(name) != string::npos)
{
ix = -1;
while (temp.find(name, ix + 1) != string::npos)
{
ix = temp.find(name, ix + 1);
index++;
cout << index << " " << name << '\t'<< " " << page << '\t' <<" "<< chapter << '\t' << prename<< '\t' << endl;
he[index] = temp;
}
}
}
}
int main()
{cout<<"�Ѷ�ȡ�������ص�8���飬������Ҫ��ѯ�ĵ����ݣ�"<<endl;
getline(cin, name);
clock_t startTime,endTime; startTime = clock();
cout << "��� ���� / ����\t ҳ��\t �½�\t ����\t" << endl;
magic("HP2--Harry_Potter_and_the_Chamber_of_Secrets_Book_2_", name);
magic("HP7--Harry_Potter_and_the_Deathly_Hallows_Book_7_", name);
magic("J.K. Rowling - HP 0 - Harry Potter Prequel", name);
magic("J.K. Rowling - HP 3 - Harry Potter and the Prisoner of Azkaban", name);
magic("J.K. Rowling - HP 4 - Harry Potter and the Goblet of Fire", name);
magic("J.K. Rowling - HP 6 - Harry Potter and the Half-Blood Prince", name);
magic("J.K. Rowling - Quidditch Through the Ages", name);
magic("J.K. Rowling - The Tales of Beedle the Bard", name);
endTime = clock(); cout << '\n'<<"��ѯ������ʱ��Ϊ: " <<(double)(endTime - startTime)/CLOCKS_PER_SEC << "s" << endl;
int i;
cout<<"\n��ѡ�����,������ʾ��ָ��λ������/����ǰ���һ�����֣�"<<endl;
cin >> i;
cout << he[i - 1] << he[i] << he[i + 1] << endl;
system("pause");
return 0;
}
//采用编码格式问题,所以会有乱码,在IDE里调试就好了。
//顺手给个点赞吧!!!!!!!!!