哈利波特检索c++实现

哈利波特检索

点击下载源代码
【问题描述】

将哈利波特的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里调试就好了。
//顺手给个点赞吧!!!!!!!!!

### 关于哈利波特主题的PTA C++编程题目 针对特定主题如“哈利波特”的编程练习,在 PTA 平台上的官方分类中可能不会直接以该名称作为标签。然而,可以建议通过一些具有故事背景的任务来模拟此类场景。 对于希望找到带有魔法学校情境设定的编程挑战,推荐关注以下几个方面: #### 数据结构应用实例 - **霍格沃茨学院分院帽**:设计一个程序实现新生入学时根据性格特征分配到不同学院的功能。这可以通过定义学生类及其属性,并利用条件判断语句完成逻辑处理[^1]。 ```cpp #include <iostream> using namespace std; class Student { public: string name; int bravery, intelligence, loyalty; // 特质评分 void assignHouse() { if(bravery >= 8 && intelligence < 7){ cout << "Gryffindor"; }else if(intelligence >= 8){ cout << "Ravenclaw"; } // 更多分支... } }; ``` #### 算法实践案例 - **魔药配制优化问题**:给定一系列原材料以及所需数量,计算最小成本购买方案。此题涉及背包算法变种,适合用于训练动态规划技巧[^2]。 ```cpp // 动态规划求解最低花费 int minCostToBuyIngredients(vector<int>& prices, vector<vector<int>>& needs) { map<vector<int>, int> memo; function<int(const vector<int>&)> dfs = [&](const vector<int>& remainNeeds)->int{ auto it = memo.find(remainNeeds); if(it != end(memo)) return (*it).second; bool allZero = true; for(auto need : remainNeeds) {if(need!=0){allZero=false;break;}} if(allZero) return 0; int res = INT_MAX / 2; for (auto& offer : offers) { vector<int> newRemainNeeds = remainNeeds; bool validOffer = true; for (size_t i=0;i<newRemainNeeds.size();i++) { newRemainNeeds[i]-=offer[i]; if(newRemainNeeds[i]<0) {validOffer=false;break;} } if(validOffer) res=min(res,dfs(newRemainNeeds)+prices[idx]); } memo.insert({remainNeeds,res}); return res; }; } ``` 尽管上述例子并非严格意义上的“哈利波特”专题试题,但是它们确实融入了一些与原著相似的情节元素,使得学习过程更加有趣味性和沉浸感。同时这些题目也涵盖了重要的计算机科学概念和技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值