通过清心醉

c++字符模糊匹配并且获取对应得第N个数字



#include<iostream>
#include<vector>
#include<string>

using namespace std;
vector<string> split(const string& s, const string& seperator) {
	vector<string> result;
	typedef string::size_type;
	int i = 0;

	while (i != s.size()) {
		int flag = 0;
		while (i != s.size() && flag == 0) {
			flag = 1;
			for (int x = 0; x < seperator.size(); ++x)
				if (s[i] == seperator[x]) {
					++i;
					flag = 0;
					break;
				}
		}
		flag = 0;
		int j = i;
		while (j != s.size() && flag == 0) {
			for (int x = 0; x < seperator.size(); ++x)
				if (s[j] == seperator[x]) {
					flag = 1;
					break;
				}
			if (flag == 0)
				++j;
		}
		if (i != j) {
			result.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return result;
}

std::string subreplace(std::string resource_str, std::string sub_str, std::string new_str)
{
	std::string dst_str = resource_str;
	std::string::size_type pos = 0;
	while ((pos = dst_str.find(sub_str)) != std::string::npos) 
	{
		dst_str.replace(pos, sub_str.length(), new_str);
	}
	return dst_str;
}

bool CheckStringOrInt(string str) {
	for (int i = 0; i < str.length(); i++) {
		switch (str[i]) {
			case '1':break;
			case '2':break;
			case '3':break;
			case '4':break;
			case '5':break;
			case '6':break;
			case '7':break;
			case '8':break;
			case '9':break;
			case '0':break;
			default:
				return false;
		}
	}
	return true;
}

vector<int>getValue(string result) {
	string str;
	vector<int>values;
	int value;

	for (int i = 0; i < result.length(); i++) {
		switch (result[i]) {
			case '1':	str += "1"; break;
			case '2':	str += "2"; break;
			case '3':	str += "3"; break;
			case '4':	str += "4"; break;
			case '5':	str += "5"; break;
			case '6':	str += "6"; break;
			case '7':	str += "7"; break;
			case '8':	str += "8"; break;
			case '9':	str += "9"; break;
			case '0':	str += "0"; break;
			default:
				if (str.length() > 0) {
					value = stoi(str);
					str = "";
					values.push_back(value);
				}
				break;
		}
	}
	return values;
}

int  getStringIntValue(string result,string str) {
	vector<string>Array = split(str, ","); 
	for (int i = 0; i < Array.size(); i++) { 
		vector<string>arr = split(Array[i], "%");
		int state = 0;
		int number = 0; 
		vector<int>datas;
		for (int k = 0; k < arr.size(); k++) {
			if (CheckStringOrInt(arr[k]) == true) {
				number = stoi(arr[k]);
			}
			else {
				if (result.find(arr[k]) == result.npos) {
					continue;
				}
				else {
					state++;
				}
			}
		}
		if (state + 1 == arr.size()) {
			if (number > 0) { 
				number = (number - 1); 
				datas = getValue(result);
				if (datas.size() > number) { 
					return datas[number];
				}
			}
		}
	}
	return -1;
}

int main()
{
	string result, str; int val;
	result = "等待30秒就可以领取300金币奖励了哦,加油继续观看10秒";
	str = "僵局%这个绝对不存在的%我需要获取第二个数字,奖励%2%等待%领取"; 
	
	val = getStringIntValue(result, str);
	cout << "获取第2个" << endl;
	cout << val << endl;
	return 0;


	result = "等待30秒就可以领取300金币奖励了哦,加油继续观看10秒";
	str = "僵局%这个绝对不存在的%我需要获取第二个数字,奖励%1%等待%领取,就是这么牛欸你能怎么办";
	cout << "获取第1个" << endl;
	val = getStringIntValue(result, str);
	cout << val << endl;

	result = "等待30秒就可以领取300金币奖励了哦,加油继续观看10秒";
	str = "奖励%3%第1";
	cout << "获取第3个" << endl;
	val = getStringIntValue(result, str);
	cout << val << endl;

	result = "等待30秒就可以领取300金币奖励了哦,加油继续观看10秒";
	str = "奖励%5";
	cout << "获取第4个绝对不存在" << endl;
	val = getStringIntValue(result, str);
	cout << val << endl;
}

时间匆忙,刚发布了第一个版本就进行了优化,更多细节上的,看得懂的朋友可以自己进行调整更改

关于作者

清心醉 administrator

发表评论

请输入验证码: