我有兩個txt文件,多次使用相同的詞。我設法將它們都拉入數組中,並通過插入排序格式化非格式化的txt文件之一。查找兩個文本文件中使用最多的詞
現在我需要比較兩個格式化的數組以找出最常用的單詞以及它們已被使用的次數。
我知道我可以使用for循環,通過每個數組,但我不知道如何。 有什麼幫助嗎?
編輯: 這是我到目前爲止。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
const int size = 100;
void checkIF(string x)
{
fstream infile;
cout << "Attempting to open ";
cout << x;
cout << "\n";
infile.open(x);
if(!infile)
{
cout << "Error: File couldn't be opened.\n";
}
else
{
cout << "File opened succsesfully.\n";
}
}
void checkFile()
{
string f1 = "text1.txt", f2 = "abbreviations.txt";
checkIF(f1);
checkIF(f2);
}
string* readFiles(string txt1[],string abb[])
{
fstream intxt1("text1.txt");
fstream inabb("abbreviations.txt");
int i = 0;
while (!intxt1.eof())
{
intxt1 >> txt1[i];
//cout << txt1[i];
i++;
}
while (!inabb.eof())
{
inabb >> abb[i];
//cout << abb[i];
i++;
}
return txt1;
return abb;
}
string* insertionSort(string txt1[], int arraySize)
{
int i, j;
string insert;
for (i = 1; i < arraySize; i++)
{
insert = txt1[i];
j = i;
while ((j > 0) && (txt1[j - 1] > insert))
{
txt1[j] = txt1[j - 1];
j = j - 1;
}
txt1[j] = insert;
}
return txt1;
}
void compare(string txt1[],string abb[])
{
}
void main()
{
string txt1Words[size];
string abbWords[size];
checkFile();
readFiles(txt1Words,abbWords);
insertionSort(txt1Words,100);
compare(txt1Words,abbWords);
system("Pause");
}
向我們展示你已經嘗試了什麼。如果你這樣做,你更有可能回答。 – john
根據您提供的內容,開始修改您的代碼對我們任何人都是無效的。 –