Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 解析文件中的数据并存储在对象中_C++_Parsing_C++11 - Fatal编程技术网

C++ 解析文件中的数据并存储在对象中

C++ 解析文件中的数据并存储在对象中,c++,parsing,c++11,C++,Parsing,C++11,好吧,我参加了一次关于编码挑战的工作面试,但我完全失败了 我希望有人能帮我解决这个问题,这样我至少能理解我错过了什么,这样下次我就能做得更好 我被要求为以下数据创建解析器: Data = { Name = "Macrosoft", Launched = 2006, Scores = { Players = { Josh = 46, June = 98 }, Rewards =

好吧,我参加了一次关于编码挑战的工作面试,但我完全失败了

我希望有人能帮我解决这个问题,这样我至少能理解我错过了什么,这样下次我就能做得更好

我被要求为以下数据创建解析器:

Data = {
    Name = "Macrosoft",
    Launched = 2006,
    Scores = {
        Players = {
            Josh = 46,
            June = 98
        },
        Rewards = ( 50, 40, 20 ),
        Min = 1,
        Max = 100
    },
    Games = (
        "Evo",
        "Gun Star",
        "Nuka"
    )
}
所需功能:

  • data.GetStr(“Name”);//返回Macrosoft
  • data.GetNum(“已启动”);//返回2006
  • data.GetData(“分数”).GetData(“球员”).GetNum(“六月”);//返回98
  • data.GetArray(“游戏”).GetStr(1);//返回Evo
  • data.FindNum(“六月”);//返回98
详情:

所有元素都有一个标识符,如数据、名称、启动等

所有元素都有一些位于标识符和字符“=”之后的数据。数据可以是:

  • Int(启动、最小和最大)
  • 数组(在“(”和“)”之间定义的数组)
  • 其他数据(数据、分数、球员)

您可以定义一个数据结构来保存数据。根据您发布的数据结构,我认为您可以定义如下类:

class Data {
public:
    Data(string str);

    string GetStr(string key);
    int GetNum(string key);
    Data GetData(string key);
    Data GetArray(string key);

    // Array data operations
    string GetStr(int index);
    int GetNum(int index);

private:
    map<string, Data> dataMap;
};
Data(string str) {
    vector<string> vecStr = split(str, ',');
    for(auto vecIter = vecStr.begin(); vecIter != vecStr.end(); ++vecIter) {
        if (ifIsArrayAssignment(*vecIter)) {
            // Games = ( ... )
            // you should put every elements into a new Data's dataMap
            // the key to the value can be numbers: 1 2 3 ...
        } else if (ifIsDataAssignment(*vecIter)) {
            // Scores = { ... }
            // you should get the key from *vecIter first
            // and make a new Data object with { ... } part
        } else {
           // Lanched = 123456 or Name = "str"
           // put it into dataMap
        }
    }
}

您可以定义一个数据结构来保存数据。根据您发布的数据结构,我认为您可以定义如下类:

class Data {
public:
    Data(string str);

    string GetStr(string key);
    int GetNum(string key);
    Data GetData(string key);
    Data GetArray(string key);

    // Array data operations
    string GetStr(int index);
    int GetNum(int index);

private:
    map<string, Data> dataMap;
};
Data(string str) {
    vector<string> vecStr = split(str, ',');
    for(auto vecIter = vecStr.begin(); vecIter != vecStr.end(); ++vecIter) {
        if (ifIsArrayAssignment(*vecIter)) {
            // Games = ( ... )
            // you should put every elements into a new Data's dataMap
            // the key to the value can be numbers: 1 2 3 ...
        } else if (ifIsDataAssignment(*vecIter)) {
            // Scores = { ... }
            // you should get the key from *vecIter first
            // and make a new Data object with { ... } part
        } else {
           // Lanched = 123456 or Name = "str"
           // put it into dataMap
        }
    }
}

非常接近json,只需替换
=
->
()
->
[]
string
->
“string”
并使用json解析器!问题是?非常接近json,只需替换
=
->
()
->
[]
string
->
“string”
并使用json解析器!问题是?太好了,非常感谢。解析数据从来都不是我真正掌握的东西,但这让我更清楚了。太多了,我想我能处理其他一切。太好了,非常感谢。解析数据从来都不是我真正掌握的东西,但这让我更清楚了。如此之多以至于我认为我可以处理其他一切。