【发布时间】:2015-07-02 16:34:46
【问题描述】:
我有 2 个文本文件:
- 主文件:Library.txt
- 要比较的文件:fileToCompare.txt
主文件(Library.txt)包含很多单词,但仍然不完整。所以我在网上搜索找到更多的单词并将它们保存在 fileToCompare.txt 中。但是Library.txt & fileToCompare.txt中肯定有很多相同的词,所以要消除相同的词我需要比较fileToCompare.txt和Library.txt来确定哪个词是一样的。
我消除相同单词的方法是将每个单词与 Library.txt 逐一比较。这意味着假设如果第一个单词是“apple”,那么“apple”将在 Library.txt 中逐个比较每个单词,当它找到它时,“apple”是这两个文件中出现的同一个单词。如果找不到,“apple”将在控制台中为cout 并将其保存为文本文件(之前要求用户输入文件名以保存不存在的单词)。
我发现如果 fileToCompare.txt 包含很多单词,例如1mb的文件大小,比较所有单词需要一个小时。于是我想了一个办法:
- fileToCompare.txt 是按字母顺序排序的,所以它总是从字母“a”开始(如果是的话)。它像往常一样比较,当它到达字母“b”时,它会在“lib/”目录中创建另一个文本文件Library2.txt。
- 我
ofstream所有单词从字母“b”开始到Library2.txt。现在不是与主文件比较,而是与 Library2.txt 进行比较。或者我可以说 Library2.txt 现在是主文件。 - 比较过程继续从字母“b”开始,如果达到字母“c”,则创建另一个文本文件Library3.txt和
ofstream所有单词都从字母“c”开始依此类推...直到单词的结尾显然是从“z”开始,这是比较过程的结束。
但问题是它不会消除相同的词,实际上有些会,但很多不会。我检查了主文件,输出文件中的一些单词是相同的。
如果您需要,这里是 Library.txt 和 fileToCompre.txt 的下载链接:
图书馆.txt -> https://www.dropbox.com/s/ihqpaju3b33ysgv/Library.txt?dl=0
fileToCompre.txt -> https://www.dropbox.com/s/pioy77g9mfz9och/fileToCompare.txt?dl=0
我上面解释的内容可能会令人困惑,实际上代码很混乱,我知道很难理解,一定要花一整个晚上才能弄清楚。
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int main(){
string txt="fileToCompare.txt";
ifstream lib;
lib2.open(txt.c_str());
if(!lib2){
cout<<"\n Oops! "<<txt<<" is missing!\n If such file exists, be sure to check the file extension is .txt\n";
getch();
main();
}
cout<<"\n Enter the file name to save the non-existing words\n (required an extension at the end)\n";
getline(cin,word);
string libPath="lib/"+word,alphaStr="a",libtxt[26]={"Library.txt","lib/Library2.txt","lib/Library3.txt","lib/Library4.txt","lib/Library5.txt","lib/Library6.txt","lib/Library7.txt","lib/Library8.txt","lib/Library9.txt","lib/Library10.txt","lib/Library11.txt","lib/Library12.txt","lib/Library13.txt","lib/Library14.txt","lib/Library15.txt","lib/Library16.txt","lib/Library17.txt","lib/Library18.txt","lib/Library19.txt","lib/Library20.txt","lib/Library21.txt","lib/Library22.txt","lib/Library23.txt","lib/Library24.txt","lib/Library25.txt","lib/Library26.txt"};
const char* wordChar=libPath.c_str();
const char* libManip=libtxt[0].c_str();
int alphaI=1,boolcheck=1;
lib.open(libManip);
outWord.open(wordChar);
while(getline(lib2,libStr2)){
if(libStr2.substr(0,1)!=alphaStr){
lib.close();
lib.open(libManip);
libMO.open(libtxt[alphaI].c_str());
while(getline(lib,libStr)){
if(libStr.substr(0,1)!=alphaStr){
libMO<<libStr<<endl;
}
}
libManip=libtxt[alphaI].c_str();
libMO.close();
lib.close();
alphaI++;
alphaStr=libStr2.substr(0,1);
boolcheck=1;
}
if(boolcheck==1){
lib.close();
lib.open(libManip);
boolcheck=0;
}
while(getline(lib,libStr)){
if(libStr==libStr2){
found=1;
break;
}
}
if(!found){
cout<<"\n "<<libStr2;
outWord<<libStr2<<endl;
countNF++;
}
count++;
found=0;
}
cout<<"\n\n\n Total words: "<<count<<"\n Total words reserved: "<<countNF;
lib2.close();
lib.close();
getch();
return 0;
}
【问题讨论】:
-
如果您的问题是关于 C++ 的,请不要标记 C,尤其是因为您有
std::string、<fstream>和<iostream> -
你的代码看起来太复杂了,不适合这个任务。
-
您需要一个适当的数据结构,例如 std::set 或哈希映射 (std::unordered_set)。现在你有 O(MN) 时间复杂度,其中 M, N 是两个文件中的单词数。哈希映射会将其降低到 O(M+N)。
标签: c++ text-files fstream string-comparison