现在的位置: 首页 > 综合 > 正文

[Pratice]递归形式的暴力破解。

2014年04月05日 ⁄ 综合 ⁄ 共 695字 ⁄ 字号 评论关闭
#include <iostream>
#include <cstring>
using namespace std;

//decode into the key
class Decode
{
public:
	Decode(const char ch[])
	{	
		count = 0;
		memset(key, 0, 128);
		Decode_helper(ch);
	}
	friend ostream& operator<<(ostream& o, Decode& m);
private:
	int count;
	char key[128];
	void Decode_helper(const char str[]);
};

//pratice, only consider alphabet
void Decode::Decode_helper(const char str[])
{
	if (!(str[0]))	return;
	for (int i = 0; i < 26; ++i)
	{
		if (str[0] == 'a' + i || str[0] == 'A' + i)
		{
			key[count] = 'a' + i;
			++count;
			Decode::Decode_helper(str + 1);
			return;
		}
	}	
}

//print
ostream& operator<<(ostream& o, Decode& m)
{
	for (int i = 0; i < m.count; ++i)
		o << m.key[i];
	return o;
}

int main(int argc, char const *argv[])
{
	char ch[128] = {0};
	cout << "Please enter your key : " << endl;
	cin >> ch;

	Decode work(ch);
	cout << work << endl;
}

抱歉!评论已关闭.