请用C++实现一个链表,把rating.json文件数据读入到链表中,文件里的每条记录包含如下字段信息comment(int), from,comments,to,score,corp, time,样例记录为{ "comment": 0, "from": "北京-北京市-海淀区", "comments": [], "to": "河北省-承德市-隆化县", "score": "0", "corp": "兴铁物流", "time": "1398175318.988036" }
时间: 2023-08-26 08:04:39 浏览: 96
链表的C++实现
好的,以下是用C++实现链表,并将rating.json文件数据读入链表中的代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Record {
int comment;
string from;
vector<string> comments;
string to;
string score;
string corp;
string time;
Record* next;
};
Record* createNode(int comment, string from, vector<string> comments, string to, string score, string corp, string time) {
Record* node = new Record;
node->comment = comment;
node->from = from;
node->comments = comments;
node->to = to;
node->score = score;
node->corp = corp;
node->time = time;
node->next = NULL;
return node;
}
void insertNode(Record*& head, int comment, string from, vector<string> comments, string to, string score, string corp, string time) {
Record* node = createNode(comment, from, comments, to, score, corp, time);
if (head == NULL) {
head = node;
}
else {
Record* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
}
void printList(Record* head) {
Record* temp = head;
while (temp != NULL) {
cout << "comment: " << temp->comment << endl;
cout << "from: " << temp->from << endl;
cout << "comments: ";
for (string comment : temp->comments) {
cout << comment << " ";
}
cout << endl;
cout << "to: " << temp->to << endl;
cout << "score: " << temp->score << endl;
cout << "corp: " << temp->corp << endl;
cout << "time: " << temp->time << endl;
cout << endl;
temp = temp->next;
}
}
int main() {
Record* head = NULL;
ifstream fin("rating.json");
if (fin.is_open()) {
string line;
while (getline(fin, line)) {
int comment;
string from, to, score, corp, time;
vector<string> comments;
size_t pos = 0;
while (pos < line.size()) {
if (line[pos] == '{' || line[pos] == ' ' || line[pos] == ',' || line[pos] == ':') {
pos++;
}
else if (line[pos] == '}') {
break;
}
else {
size_t end_pos = line.find('"', pos + 1);
string field_name = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
if (field_name == "comment") {
end_pos = line.find(',', pos);
comment = stoi(line.substr(pos, end_pos - pos));
pos = end_pos + 1;
}
else if (field_name == "from") {
end_pos = line.find('"', pos);
from = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
}
else if (field_name == "comments") {
end_pos = line.find(']', pos);
string comment_str = line.substr(pos, end_pos - pos);
pos = end_pos + 1;
size_t comment_pos = 0;
while (comment_pos < comment_str.size()) {
end_pos = comment_str.find('"', comment_pos + 1);
string comment = comment_str.substr(comment_pos + 1, end_pos - comment_pos - 1);
comments.push_back(comment);
comment_pos = end_pos + 2;
}
}
else if (field_name == "to") {
end_pos = line.find('"', pos);
to = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
}
else if (field_name == "score") {
end_pos = line.find('"', pos);
score = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
}
else if (field_name == "corp") {
end_pos = line.find('"', pos);
corp = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
}
else if (field_name == "time") {
end_pos = line.find('"', pos);
time = line.substr(pos, end_pos - pos);
pos = end_pos + 2;
}
}
}
insertNode(head, comment, from, comments, to, score, corp, time);
}
fin.close();
}
else {
cout << "Cannot open file" << endl;
}
printList(head);
return 0;
}
```
以上代码中,我们定义了一个名为Record的结构体,用来存储每条记录的各个字段信息。通过createNode函数创建一个Record节点,insertNode函数将该节点插入到链表中。printList函数用来遍历整个链表并打印每个节点的信息。
在main函数中,我们首先将链表头指针head初始化为NULL。然后使用ifstream打开rating.json文件,读取每行数据并将其插入到链表中。数据的读取过程中,我们使用字符串的find和substr函数,根据每个字段的名字和位置,从字符串中提取出对应的字段信息。最后调用printList函数遍历链表并打印每个节点的信息。
注意:这里我们没有进行json格式的严格解析,而是通过字符串的查找和截取实现了一个简单的解析。如果需要处理更复杂的json数据,建议使用现成的json解析库。
阅读全文