私は次のようにユーザー入力を取っています:
algo_type "pattern" filename
元。
bf "inging" input_file.txt
現在、ユーザー入力を 3 つの異なる変数に分けています。1 つは algo_type 用、もう 1 つは探しているパターン用、もう 1 つはファイル名用です。パターンとファイル名を取得したら、パターンを Bruteforce アルゴに取り込んで各行を検索し、.txt ファイルの行でパターンが発生する位置を出力しようとしています。今のところ、アルゴに入力を入力するたびに、ブルートフォースが実行されていないことを意味する -1 が返されますか? ここで私は正確に何を間違っていますか?
int BruteForce(const string& line, const string& pattern){
int n , m;
n = line.length();
m = pattern.length();
for(int i = 0 ; i < n - m ; i++){
int j = 0;
while( j < m && line[i + j] == pattern[j]){
j = j+1;
if( j == m){
return i;
}
}
}
return -1;
}
int main(){
string text, algo_type , pattern , fname, line;
getline(cin ,text);
istringstream iss(text);
if(iss >> algo_type >> pattern >> fname){
cout << algo_type << pattern << fname << "'\n'";
}
int i = 0;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != ""){
if( algo_type == "bf"){
cout << "Line " << i++ << ":" << BruteForce(line,pattern) << endl;
}
}
return 0;
}