void countEducation() { Node* p = head->next; int bachelorCount = 0, masterCount = 0, doctoralCount = 0; while (p != nullptr) { if (p->data.getEducation() == "本科") { bachelorCount++; } else if (p->data.getEducation() == "硕士") { masterCount++; } else if (p->data.getEducation() == "博士") { doctoralCount++; } p = p->next; } cout << "本科:" << bachelorCount << endl; cout << "硕士:" << masterCount << endl; cout << "博士:" << doctoralCount << endl; 修改代码,只使用链表,使其可以分别按照“系”来统计各个系中的各种学历的人数 }
时间: 2024-04-29 13:20:39 浏览: 11
void countEducation() {
unordered_map<string, int> bachelorCount, masterCount, doctoralCount;
Node* p = head->next;
while (p != nullptr) {
string department = p->data.getDepartment();
string education = p->data.getEducation();
if (education == "本科") {
bachelorCount[department]++;
} else if (education == "硕士") {
masterCount[department]++;
} else if (education == "博士") {
doctoralCount[department]++;
}
p = p->next;
}
for (auto it : bachelorCount) {
cout << it.first << "本科:" << it.second << endl;
}
for (auto it : masterCount) {
cout << it.first << "硕士:" << it.second << endl;
}
for (auto it : doctoralCount) {
cout << it.first << "博士:" << it.second << endl;
}
}
修改后的代码中,使用了一个 unordered_map 来分别记录各个系中的本科、硕士和博士的人数。在遍历链表时,对于每个节点,根据其所属的系和学历,更新对应的计数器。最后,通过遍历三个 unordered_map,输出各个系中的各种学历的人数。