题面
经典题目,自己编的。
给一行enum,求分析出每个枚举元素的值。
解
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
/*
enum weekday = { MON = 1, TUE, WED, THU, FRI, BAD = 1, SAD }
*/
struct node
{
string name;
int value;
};
vector<node> nodes;
vector<string> v;
void split(string s, char c)
{
int cp = 0, np = 0;
while (np != -1)
{
np = s.find(c, cp);
v.push_back(s.substr(cp, np - cp));
// cout << s.substr(cp, np - cp) << endl;
cp = np + 2;
}
}
int value = 0;
void analysis()
{
for (int i = 0; i < v.size(); i++)
{
node c;
int eq = v[i].find('=');
if (eq != -1)
{
stringstream ss;
ss << v[i].substr(0, eq - 1) << " " << v[i].substr(eq + 2, v[i].size() - eq - 2);
ss >> c.name >> c.value;
}
else
{
c.name = v[i];
c.value = value + 1;
}
nodes.push_back(c);
value = c.value;
}
}
int main()
{
string s;
getline(cin, s);
int eq = s.find('=', 0);
string _s = s.substr(eq + 4, s.size() - eq - 5);
// cout << _s << endl;
split(_s, ',');
analysis();
for (int i = 0; i < nodes.size(); i++)
{
cout << nodes[i].name << " = " << nodes[i].value << endl;
}
return 0;
}