C++建立词的索引表 小白遇到bug

失明后的世界 2016-09-16 11:16:20
题目是 把一个文件的内容 书号和书名建立词的索引表
005 Computer Data Structures
010 Introduction to Data Structures
023 Fundamentals of Data Structures
034 The Design and Analysis of Computer Algorithms
050 Introduction to Numerical Analysis
067 Numerical Analysis
输出关键词和书名
algorithms 034,
analysis 034,050,067,
computer 005,034,
data 005,010,023,
design 034,
fundamentals 023,
introduction 010,050,
numerical 050,067,
structures 005,010,023,

代码如下:
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
using namespace std;

#ifndef WORD_INDEX_H_INCLUDED
#define WORD_INDEX_H_INCLUDED
struct word_Index{
word_Index() = default;
word_Index(string word,string booknum){prioword=word;bookNum.push_back(booknum);}
string prioword; //关键词
vector<string> bookNum; //含有该关键词的书号
};
#endif // WORD_INDEX_H_INCLUDED

vector<string> notprioword{"a","the","of","and","many","more","The","to"}; //非关键词
void InputFile(vector<string> &str,string file1);
void Addtoclass(vector<string> &str,vector<word_Index> &cls);
bool Check(string word);
void Output(vector<word_Index> &cls,string file);

int main()
{
vector<string> tempstr;
vector<word_Index> clswordindex;
string file = "book.txt";
string resultfile = "bookindex.txt";
InputFile(tempstr,file);
Addtoclass(tempstr,clswordindex);
Output(clswordindex,resultfile);

}

void InputFile(vector<string> &str,string file1)
{ //读取文件内容
ifstream Input(file1);
string line;
while(getline(Input,line)){
str.push_back(line);
}
}
void Addtoclass(vector<string> &str,vector<word_Index> &cls)
{ //把str的string按要求转移到类中
string booknum,word;
bool jud=0;
for(auto strline : str){
istringstream record(strline);
record >> booknum;
cout << booknum << "/// \n";
while(record >> word){//处理每一个关键字
cout << word << "处理\n";
if(Check(word)) continue; //是非关键词 处理下一个词
for(auto clspw : cls){
if(clspw.prioword == word){//在类中找到相同的
cout << word << "已存在\n";
//把书名号加进去
clspw.bookNum.push_back(booknum);//这里开始有问题
//检测是否添加进去
for(auto n:clspw.bookNum){cout << n << " ";}
cout << endl;
//全部打印出来
for(auto n : cls){
cout << n.prioword << " ";
for(auto beg = n.bookNum.begin();beg!=n.bookNum.end();beg++){
cout << *beg << " ";
}
cout << endl;
}

jud = 1;
}
}//for

if(jud==0){
cout << "不存在 添加\n";
cls.emplace_back(word,booknum);
}
else
jud=0;

}//while
for(auto n : cls){
cout << n.prioword << " ";
for(auto unit : n.bookNum){
cout << unit << " ";
}
cout << endl;
}
}//for
}
bool Check(string word)
{ //检查是不是非关键词
for(auto str : notprioword){
if(str==word)
return 1; //是非关键字
}
return 0;//是关键字
}
void Output(vector<word_Index> &cls,string file)
{ //输出到文件
ofstream output(file);
for(auto unit : cls){
output << unit.prioword << " ";
for(auto booknum : unit.bookNum){
output << booknum << ",";
}
output << "\n";
}
}

bug:
在把数据放进类的时候 这条代码 clspw.bookNum.push_back(booknum);
已经把有相同关键词的书号放进去了 但是马上输出就显示没加进去???
求大神帮忙!!!!找不出来哪里有错。



...全文
141 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2016-09-16
  • 打赏
  • 举报
回复
单步跟踪相关部分代码,看程序怎么执行,然后分析原因

64,677

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧