C++實現規則編號器

#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <sstream>

using std::string;
using std::vector;
using std::stringstream;
using std::cin;
using std::cout;
using std::endl;
string GetNextNo(string ruleStr, string curNo);
vector<string> Split(string str, char spChar);
string ReplaceAll(string str, const  string oldValue, string newValue);
string DealSpecialStr(string ruleNo);
string IntToStr(int num);


int main()
{
	char cStr[100] = "Hello ";
	char cStr1[] = "Word";
	//測試字符串
	string testStr = "Hello";
	string testStr1 = "Word";
	//測試向量-list類似
	vector<string> testVec;
	//字符串拼接
	string testStr2 = testStr + " " + testStr1;
	cout << testStr2 << endl;
	strcat(cStr, cStr1);
	printf("cStr=%s\n", cStr);
	string strTmp = "";
	cout << "迭代器輸出字符串每個字符" << endl;
	for (auto c = testStr2.begin(); c < testStr2.end(); c++)
	{
		cout << *c << endl;
		strTmp += *c;
		testVec.push_back(strTmp);
	}
	cout << "迭代器輸出向量每個元素" << endl;
	for (auto v = testVec.begin(); v < testVec.end(); v++)
	{
		cout << *v << endl;
	}
	cout << "規則編號器開始" << endl;
	string ruleStr = "[[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]]";
	string curNo = "000";
	for (int i = 0; i < 20; i++)
	{
		curNo = GetNextNo(ruleStr, curNo);
		cout << curNo << endl;
	}
	cout << "規則編號器測試日期" << endl;
	ruleStr = "[[\"$y1\"],[\"$y2\"],[\"$y3\"],[\"$y4\"],[\"$M1\"],[\"$M2\"],[\"$d1\"],[\"$d2\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]]";
	curNo = "";
	for (int i = 0; i < 20; i++)
	{
		curNo = GetNextNo(ruleStr, curNo);
		cout << curNo << endl;
	}
	return 0;
}

//通過規則串和當前編號得到下一號
//ruleStr:規則串  例如:[[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"],[\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"]]
//curNo:當前號 例如:259
string GetNextNo(string ruleStr, string curNo)
{
	if (ruleStr.length() == 0)
	{
		return "-1^沒有傳入規則串!";
	}
	ruleStr = ReplaceAll(ruleStr, "],[", "^");
	ruleStr = ReplaceAll(ruleStr, "\"", "");
	ruleStr = ReplaceAll(ruleStr, "[", "");
	ruleStr = ReplaceAll(ruleStr, "]", "");
	//分隔第一層二維數組
	vector<string> vecArrRule = Split(ruleStr,'^');
	if (curNo.length()>0&&curNo.length() != vecArrRule.size())
	{
		return "-1^規則定義的串長度和當前號長度不一致!";
	}
	//完全解析的規則串
	vector<vector<string>> vecArrRuleTow;
	//記錄當前號在沒位的位置
	vector<size_t> indexArr;
	//存每位的數量
	vector<size_t> countArr;
	int curOneIndex = -1;
	//迭代遍歷每位規則
	for (auto one = vecArrRule.begin(); one < vecArrRule.end(); one++)
	{
		curOneIndex++;
		string oneRule = *one;
		//每一位的可走範圍
		vector<string> oneRuleArr = Split(oneRule,',');
		vecArrRuleTow.push_back(oneRuleArr);
		size_t curTowIndex = -1;
		bool hasIn = false;
		for (auto tow = oneRuleArr.begin(); tow < oneRuleArr.end(); tow++)
		{
			curTowIndex++;
			//記錄當前位所在的位置
			if (curNo.length()==0||curNo[curOneIndex] == (*tow)[0]|| ((*tow)[0]=='$'))
			{
				indexArr.push_back(curTowIndex);
				countArr.push_back(oneRuleArr.size());
				hasIn = true;
				break;
			}
		}
		if (hasIn == false)
		{
			return "-1^編號與規則不匹配!";
		}
	}
	//得到下一號位置
	if(curNo.length()>0&&indexArr.size()>0)
	{ 
		for (int i = indexArr.size() - 1; i >= 0; i--)
		{
			if (indexArr[i] + 1 < countArr[i])
			{
				indexArr[i]++;
				break;
			}
			else if(i>0)
			{
				indexArr[i] = 0;
			}
			else
			{
				return "-1^已經到最大號了!";
			}
		}
	}
	string retNo = "";
	int curIndex = -1;
	//按位置得到下一號
	for (auto a= indexArr.begin();a< indexArr.end();a++)
	{
		curIndex++;
		retNo += vecArrRuleTow[curIndex][(*a)];
	}
	return DealSpecialStr(retNo);
}

//分隔字符串爲向量數組
//str:字符串
//spChar:分隔字符
vector<string> Split(string str,char spChar)
{
	vector<string> retArr;
	if (str.length() == 0)
	{
		retArr.push_back("");
		return retArr;
	}
	string tmp;
	for (int i = 0; i < str.length(); i++)
	{
		if (str[i] == spChar)
		{
			retArr.push_back(tmp);
			tmp = "";
			continue;
		}
		tmp += str[i];
	}
	if (tmp.length()>0)
	{
		retArr.push_back(tmp);
	}
	return retArr;
}

//替換所有字符串
string ReplaceAll(string str, const  string oldValue,string newValue)
{
	for (auto pos(0); pos != string::npos; pos += newValue.length())
	{
		if ((pos = str.find(oldValue, pos)) != string::npos)
		{
			str.replace(pos, oldValue.length(), newValue);
		}
		else
		{
			break;
		}
	}
	return str;
}

//處理特殊串
string DealSpecialStr(string ruleNo)
{
	// 基於當前系統的當前日期/時間
	time_t now = time(0);
	tm* ltm = localtime(&now);
	int year = ltm->tm_year+1900;
	int month = ltm->tm_mon+1;
	int day = ltm->tm_mday;
	int hour = ltm->tm_hour;
	int min = ltm->tm_min;
	int sec = ltm->tm_sec;
	string yearStr = "";
	ruleNo = ReplaceAll(ruleNo,"$y1",IntToStr(year).substr(0,1));
	ruleNo = ReplaceAll(ruleNo, "$y2", IntToStr(year).substr(1,1));
	ruleNo = ReplaceAll(ruleNo, "$y3", IntToStr(year).substr(2,1));
	ruleNo = ReplaceAll(ruleNo, "$y4", IntToStr(year).substr(3,1));
	string monthStr = IntToStr(month);
	if (monthStr.size() == 1)
	{
		monthStr = "0" + monthStr;
	}
	string dayStr = IntToStr(day);
	if (dayStr.size() == 1)
	{
		dayStr = "0" + dayStr;
	}
	string hourStr = IntToStr(hour);
	if (hourStr.size() == 1)
	{
		hourStr = "0" + hourStr;
	}
	string minStr = IntToStr(min);
	if (minStr.size() == 1)
	{
		minStr = "0" + minStr;
	}
	string secStr = IntToStr(sec);
	if (secStr.size() == 1)
	{
		secStr = "0" + secStr;
	}
	ruleNo = ReplaceAll(ruleNo, "$M1", monthStr.substr(0, 1));
	ruleNo = ReplaceAll(ruleNo, "$M2", monthStr.substr(1, 1));
	ruleNo = ReplaceAll(ruleNo, "$d1", dayStr.substr(0, 1));
	ruleNo = ReplaceAll(ruleNo, "$d2", dayStr.substr(1, 1));
	ruleNo = ReplaceAll(ruleNo, "$h1", hourStr.substr(0, 1));
	ruleNo = ReplaceAll(ruleNo, "$h2", hourStr.substr(1, 1));
	ruleNo = ReplaceAll(ruleNo, "$m1", minStr.substr(0, 1));
	ruleNo = ReplaceAll(ruleNo, "$m2", minStr.substr(1, 1));
	ruleNo = ReplaceAll(ruleNo, "$s1", secStr.substr(0, 1));
	ruleNo = ReplaceAll(ruleNo, "$s2", secStr.substr(1, 1));
	return ruleNo;
}

//整數轉換字符串
string IntToStr(int num)
{
	string res;
	stringstream ss;
	ss << num;
	ss >> res;
	return res;
}

測試效果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章