#include <iostream> using namespace std; typedef tuple<list<Teacher>, list<Student>, list<Dean>, list<Chancellor>> Totality; Totality input() { Totality per; Teacher t1; cout << "请输入5名教师信息:" << endl; for (int i = 0; i < 1; i++) { t1.inputData(); get<0>(per).push_back(t1); } return per; } int found(Totality tota, string s) { for (auto it = get<0>(tota).begin(); it != get<0>(tota).end(); it++) { if (s == it->getName()) { cout << *it << endl; return 1; } } for (auto it = get<1>(tota).begin(); it != get<1>(tota).end(); it++) { if (s == it->getName()) { cout << *it << endl; return 2; } } for (auto it = get<2>(tota).begin(); it != get<2>(tota).end(); it++) { if (s == it->getName()) { cout << *it << endl; return 3; } } for (auto it = get<3>(tota).begin(); it != get<3>(tota).end(); it++) { if (s == it->getName()) { cout << *it << endl; return 4; } } cout << "没有匹配的信息!查询失败!"; return 0; } int main() { Totality per; Teacher t1; Student s1; Chancellor c1; Dean d1; string sName;int num = found(per, sName); if (num == 1) { for (auto it = get<0>(per).begin(); it != get<0>(per).end(); it++) { if (sName == it->getName()) get<0>(per).remove(*it); } } else if (num == 2) { for (auto it = get<1>(per).begin(); it != get<1>(per).end(); it++) { if (sName == it->getName()) get<1>(per).remove(*it); } } else if (num == 3) { for (auto it = get<2>(per).begin(); it != get<2>(per).end(); it++) { if (sName == it->getName()) get<2>(per).remove(*it); } } else if (num == 4) { for (auto it = get<3>(per).begin(); it != get<3>(per).end(); it++) { if (sName == it->getName()) get<3>(per).remove(*it); } } 哪里有错,代码怎么改
时间: 2024-03-25 15:35:32 浏览: 47
#include <iostream>
这段代码有几个问题:
1. 在 `main()` 函数中,没有从用户获取输入的人员姓名 `sName`。
2. 在 `main()` 函数中,调用 `found()` 函数时 `per` 还没有被赋值,所以 `found()` 函数中的四个列表都是空的,会导致查询失败。
3. 在 `found()` 函数中,如果找到了匹配的信息,应该直接返回,而不是继续查找其他列表。
4. 在 `found()` 函数中,如果找不到匹配的信息,应该在所有列表都查找完之后再提示查询失败。
下面是修改后的代码:
```cpp
#include <iostream>
#include <tuple>
#include <list>
using namespace std;
class Person {
protected:
string name;
int age;
string gender;
public:
Person() {}
Person(string n, int a, string g) : name(n), age(a), gender(g) {}
void setName(string n) { name = n; }
string getName() const { return name; }
void setAge(int a) { age = a; }
int getAge() const { return age; }
void setGender(string g) { gender = g; }
string getGender() const { return gender; }
virtual void inputData() {
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入性别:";
cin >> gender;
}
virtual void showData() const {
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
cout << "性别:" << gender << endl;
}
virtual ~Person() {}
};
class Teacher : public Person {
protected:
string course;
public:
Teacher() {}
Teacher(string n, int a, string g, string c) : Person(n, a, g), course(c) {}
void setCourse(string c) { course = c; }
string getCourse() const { return course; }
void inputData() {
Person::inputData();
cout << "请输入课程:";
cin >> course;
}
void showData() const {
Person::showData();
cout << "课程:" << course << endl;
}
~Teacher() {}
};
class Student : public Person {
protected:
int score;
public:
Student() {}
Student(string n, int a, string g, int s) : Person(n, a, g), score(s) {}
void setScore(int s) { score = s; }
int getScore() const { return score; }
void inputData() {
Person::inputData();
cout << "请输入成绩:";
cin >> score;
}
void showData() const {
Person::showData();
cout << "成绩:" << score << endl;
}
~Student() {}
};
class Dean : public Person {
protected:
string department;
public:
Dean() {}
Dean(string n, int a, string g, string d) : Person(n, a, g), department(d) {}
void setDepartment(string d) { department = d; }
string getDepartment() const { return department; }
void inputData() {
Person::inputData();
cout << "请输入所在系:";
cin >> department;
}
void showData() const {
Person::showData();
cout << "所在系:" << department << endl;
}
~Dean() {}
};
class Chancellor : public Person {
protected:
string university;
public:
Chancellor() {}
Chancellor(string n, int a, string g, string u) : Person(n, a, g), university(u) {}
void setUniversity(string u) { university = u; }
string getUniversity() const { return university; }
void inputData() {
Person::inputData();
cout << "请输入所在大学:";
cin >> university;
}
void showData() const {
Person::showData();
cout << "所在大学:" << university << endl;
}
~Chancellor() {}
};
typedef tuple<list<Teacher>, list<Student>, list<Dean>, list<Chancellor>> Totality;
Totality input() {
Totality per;
Teacher t1;
cout << "请输入5名教师信息:" << endl;
for (int i = 0; i < 5; i++) {
t1.inputData();
get<0>(per).push_back(t1);
}
Student s1;
cout << "请输入5名学生信息:" << endl;
for (int i = 0; i < 5; i++) {
s1.inputData();
get<1>(per).push_back(s1);
}
Dean d1;
cout << "请输入1名系主任信息:" << endl;
d1.inputData();
get<2>(per).push_back(d1);
Chancellor c1;
cout << "请输入1名校长信息:" << endl;
c1.inputData();
get<3>(per).push_back(c1);
return per;
}
int found(Totality tota, string s) {
for (auto it = get<0>(tota).begin(); it != get<0>(tota).end(); it++) {
if (s == it->getName()) {
cout << *it << endl;
return 1;
}
}
for (auto it = get<1>(tota).begin(); it != get<1>(tota).end(); it++) {
if (s == it->getName()) {
cout << *it << endl;
return 2;
}
}
for (auto it = get<2>(tota).begin(); it != get<2>(tota).end(); it++) {
if (s == it->getName()) {
cout << *it << endl;
return 3;
}
}
for (auto it = get<3>(tota).begin(); it != get<3>(tota).end(); it++) {
if (s == it->getName()) {
cout << *it << endl;
return 4;
}
}
cout << "没有匹配的信息!查询失败!" << endl;
return 0;
}
int main() {
Totality per = input();
string sName;
cout << "请输入要查询的人员姓名:" << endl;
cin >> sName;
int num = found(per, sName);
if (num == 1) {
for (auto it = get<0>(per).begin(); it != get<0>(per).end(); it++) {
if (sName == it->getName()) {
get<0>(per).remove(*it);
cout << "已删除教师:" << endl;
it->showData();
break;
}
}
}
else if (num == 2) {
for (auto it = get<1>(per).begin(); it != get<1>(per).end(); it++) {
if (sName == it->getName()) {
get<1>(per).remove(*it);
cout << "已删除学生:" << endl;
it->showData();
break;
}
}
}
else if (num == 3) {
for (auto it = get<2>(per).begin(); it != get<2>(per).end(); it++) {
if (sName == it->getName()) {
get<2>(per).remove(*it);
cout << "已删除系主任:" << endl;
it->showData();
break;
}
}
}
else if (num == 4) {
for (auto it = get<3>(per).begin(); it != get<3>(per).end(); it++) {
if (sName == it->getName()) {
get<3>(per).remove(*it);
cout << "已删除校长:" << endl;
it->showData();
break;
}
}
}
return 0;
}
```
阅读全文