与运行了暴力破解algorithmn错误? [英] Error with running BruteForce algorithmn?
问题描述
我要带一个用户的输入,像这样:
algo_type模式文件名
恩。
BFinginginput_file.txt
截至目前我单独的用户输入到三个不同的变量,一个用于algo_type,一个用于我正在寻找的模式,和一个用于文件名。一旦我得到的格局和文件名,我试图把模式进入猜解算法中,然后搜索各条线和打印模式出现在.txt文件的行的位置。现在,虽然我进入输入到它返回的算法中-1意味着暴力破解每次都没有运行?我究竟做错了什么?
INT暴力破解(常量字符串和放大器;线,常量字符串和放大器;图案){
INT N,M;
N = line.length();
米= pattern.length();
的for(int i = 0;我n种 - 米;我++){
INT J = 0;
而(J< M&安培;&放大器;行[I + J] ==模式[J]){
当J = J + 1;
如果(j == M){
返回我;
}
}
}
返回-1;
}
诠释的main(){
文本字符串,algo_type,图案,FNAME,线;
函数getline(霉素,文本);
istringstream ISS(文本);
如果(ISS>> algo_type>>模式>> FNAME){
COUT<< algo_type<<模式<< FNAME<< '\ N';
}
INT I = 0;
ifstream的IFS;
ifs.open(fname.c_str());
而(函数getline(如果,线)及&安培; FNAME =!){
如果(algo_type ==BF){
COUT<< 行<<我++ LT;< :&其中;&其中;暴力破解(线,图案)<< ENDL;
}
}
返回0;
}
我想你想返回-1
在暴力破解的结束,而不是在的结束第一次迭代。
此外,第一循环条件需要有< =
而不是<
,或匹配尾数很位置不会被发现。
下面是一个完整的,固定的版本:修改作为每个编辑,内部线路列表中的多个匹配:
的#include<字符串>
使用名字空间std;
INT暴力破解(常量字符串和放大器;线,SIZE_T开始,常量字符串和放大器;图案){
常量为size_t N = line.length();
常量为size_t M = pattern.length();
如果(N< M)返回-1;
用于(为size_t我=启动; I< =(N - M);我++){
为(为size_t J = 0; J&其中;米&安培;及(行[I + J] ==图案[j]的); ++ j)条{
如果(j == M-1){
返回我;
}
}
}
返回-1;
}
#包括<的iostream>
#包括< fstream的>
#包括< sstream>
诠释的main(){
文本字符串,algo_type,图案,FNAME,线;
函数getline(霉素,文本);
istringstream ISS(文本);
如果(ISS>> algo_type>>模式>> FNAME){
COUT<< << algo_type<< <<模式<< << FNAME<< \ N的;
}
INT I = 1;
ifstream的IFS;
ifs.open(fname.c_str());
而(函数getline(如果,线)及&安培; FNAME =!){
如果(algo_type ==BF){
INT POS = -1;
而(-1!=(POS =暴力破解(行,POS + 1模式)))
COUT<< 行<< I<< :&其中;&其中; POS<< <<线474;< ENDL;
}
我++;
}
返回0;
}
实时查看关于Coliru: http://coliru.stacked-crooked.com /一/ f1a7693d7d3bd7c5
我已经与测试它
./测试<<< BF ISS的/ etc /词典常见/词| grep的小姐
其中印刷
行10241:1小姐
行10242:1米西索加
行10242:4米西索加
行10243:1密西西比
行10243:4密西西比
行10244:1密西西比
行10244:4密西西比
行10245:1密西西比
行10245:4密西西比
行10246:1密西西比的
行10246:4密西西比的
行10247:1密西西比
行10247:4密西西比
行10248:1密苏里州
行10249:1密苏里州
行10250:1密苏里
行10251:1密苏里州的
行10252:1密苏里州
行10253:1大小姐
行10254:1大小姐的
I'm taking a users input like so:
algo_type "pattern" filename
ex.
bf "inging" input_file.txt
As of now I separate the users input into three different variables, one for the algo_type, one for the pattern I'm looking for, and one for the filename. Once I get the pattern and filename I'm trying to take the pattern into the Bruteforce algo and search each line and print the position that pattern occurs in the line of the .txt file. Right now though every time I enter the input into the algo it returns -1 meaning the BruteForce isn't running? What exactly am I doing wrong here?
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;
}
I suppose you wanted return -1
at the end of BruteForce, rather then at the end of the first iteration.
Also, the first loop condition needs to have <=
instead of <
, or matches ending in the very position won't be found.
Here's a complete, fixed version: EDIT as per the edit, list multiple matches within lines:
#include <string>
using namespace std;
int BruteForce(const string& line, size_t start, const string& pattern) {
const size_t n = line.length();
const size_t m = pattern.length();
if (n<m) return -1;
for(size_t i = start; i <= (n - m); i++) {
for(size_t j=0; j < m && (line[i + j] == pattern[j]); ++j) {
if(j == m-1) {
return i;
}
}
}
return -1;
}
#include <iostream>
#include <fstream>
#include <sstream>
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 = 1;
ifstream ifs;
ifs.open(fname.c_str());
while(getline(ifs, line) && fname != "") {
if(algo_type == "bf") {
int pos = -1;
while (-1 != (pos = BruteForce(line, pos+1, pattern)))
cout << "Line " << i << ":" << pos << " " << line << endl;
}
i++;
}
return 0;
}
See it Live on Coliru: http://coliru.stacked-crooked.com/a/f1a7693d7d3bd7c5
I've tested it with
./test <<< "bf iss /etc/dictionaries-common/words" | grep Miss
Which printed
Line 10241:1 Miss
Line 10242:1 Mississauga
Line 10242:4 Mississauga
Line 10243:1 Mississippi
Line 10243:4 Mississippi
Line 10244:1 Mississippi's
Line 10244:4 Mississippi's
Line 10245:1 Mississippian
Line 10245:4 Mississippian
Line 10246:1 Mississippian's
Line 10246:4 Mississippian's
Line 10247:1 Mississippians
Line 10247:4 Mississippians
Line 10248:1 Missouri
Line 10249:1 Missouri's
Line 10250:1 Missourian
Line 10251:1 Missourian's
Line 10252:1 Missourians
Line 10253:1 Missy
Line 10254:1 Missy's
这篇关于与运行了暴力破解algorithmn错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!