一、需求
从字符串std::string left_card = “0/1/2/3/5/7”;
提取std::liststd::string left_card_list = {“0”, “1”, “2”, “3”, “5”, “7”};
二、实现
#include<iostream>
#include<vector>
#include<string>
std::string left_card = "0/1/2/3/5/7";
std::string left_card_amount = "6";
//std::string right_card = "0/1/2/3/4/5/6/7";
enum CARD_TYPE{
LEFT_CARD,
RIGHT_CARD,
};
#define ARRAY_SIZEOF(a) (sizeof(a) / sizeof(a[0]))
#define FINISH_CHANGE "255"
struct card_info
{
CARD_TYPE type;
std::string src;
std::string dest;
};
card_info cardArray[] = {
/* type */ /* src */ /* dest */
{LEFT_CARD, "0", "0"},
{LEFT_CARD, "1", "10"},
{LEFT_CARD, "2", "10"},
{LEFT_CARD, "3", "13"},
{LEFT_CARD, "5", "14"},
{LEFT_CARD, "6", "15"},
{LEFT_CARD, "7", "16"},
{RIGHT_CARD, "0", "0"},
{RIGHT_CARD, "1", "17"},
{RIGHT_CARD, "2", "19"},
{RIGHT_CARD, "3", "12"},
{RIGHT_CARD, "4", "18"},
{RIGHT_CARD, "5", "14"},
{RIGHT_CARD, "6", "15"},
{RIGHT_CARD, "7", "16"},
};
bool left_ignore_flag = false;
std::string CardChangeRule(CARD_TYPE card_type, std::string src)
{
std::string dest = "";
int index;
int size = ARRAY_SIZEOF(cardArray);
for (index = 0; index < size; index++) {
if (src == cardArray[index].src && card_type == cardArray[index].type) {
if ((src == "1" || src == "2")&&(card_type == LEFT_CARD)) {
if (left_ignore_flag == false)
{
left_ignore_flag = true;
}
else
{
break;
}
}
dest = cardArray[index].dest;
break;
}
}
if (index == size) {
dest = FINISH_CHANGE;
}
return dest;
}
std::vector<std::string> CardCompaibleChange(CARD_TYPE card_type, const std::string& str)
{
std::vector<std::string> dest;
std::string separator = "/";
std::string tmp_str;
std::string tmp_change;
int start = 0;
int index = 0;
index = str.find_first_of(separator, start);
while(index != std::string::npos)
{
tmp_str = str.substr(start, index - start);
tmp_change = CardChangeRule(card_type, tmp_str);
if(tmp_change == FINISH_CHANGE)
return dest;
if(tmp_change != ""){
dest.push_back(tmp_change);
}
start = str.find_first_not_of(separator, index);
if(start == std::string::npos)
return dest;
index = str.find_first_of(separator, start);
}
tmp_str = str.substr(start);
tmp_change = CardChangeRule(card_type, tmp_str);
if(tmp_change == FINISH_CHANGE)
return dest;
if(tmp_change != ""){
dest.push_back(tmp_change);
}
return dest;
}
void change_left_card()
{
std::string new_left_card;
left_ignore_flag = false;
std::vector<std::string> left_card_list = CardCompaibleChange(LEFT_CARD, left_card);
left_ignore_flag = false;
if(left_card_list.size() > 1)
{
for(int i = 0; i< left_card_list.size(); i++)
{
new_left_card += left_card_list[i].c_str();
if(i != left_card_list.size()-1)
new_left_card += "/";
}
left_card = new_left_card;
left_card_amount = std::to_string(left_card_list.size());
}
}
void ota_card_compatible()
{
change_left_card();
//change_right_card();
}
int main()
{
printf("left_card=%s\n", left_card.c_str());
printf("left_card_amount=%s\n", left_card_amount.c_str());
ota_card_compatible();
printf("---- change later ----\n");
printf("left_card=%s\n", left_card.c_str());
printf("left_card_amount=%s\n", left_card_amount.c_str());
return 0;
}
三、运行结果
left_card=0/1/2/3/5/7
left_card_amount=6
---- change later ----
left_card=0/10/13/14/16
left_card_amount=5