急求一个C++的同质链表,面向对象,用来存储和管理大学的学生或教师信息。

2025-01-01 11:55:22
推荐回答(3个)
回答1:

学里的人员主要包括学生,行政人员,教师和在职研究生;实现一个单向无循环的异质链表
用C++代码来完成! 谢谢!
另外,团IDC网上有许多产品团购,便宜有口碑

回答2:

同求代码,757140297@qq.com

回答3:

#include
#include
using namespace std;

class Node {
public:
Node() {

}

Node(char *num, char *name, int nAge, char *department, bool bStu)
: m_sNum(num), m_sName(name), m_nAge(nAge), m_sDepartment(department), m_bStudent(bStu), m_pNext(0) {

}

void SetName(string &name) {
m_sName = name;
}
void SetNum(string &num) {
m_sNum = num;
}
void SetAge(int age) {
m_nAge = age;
}
void SetDepartment(string &department) {
m_sDepartment = department;
}

string GetName() const {
return m_sName;
}
string GetNum() const {
return m_sNum;
}
string GetDepartment() const {
return m_sDepartment;
}
int GetAge() const {
return m_nAge;
}

void SetNext(Node *pNode) {
m_pNext = pNode;
}

Node* GetNext() const {
return m_pNext;
}

bool IsStudent() const {
return m_bStudent;
}

private:
string m_sNum; //学号或教师编号
string m_sName;
int m_nAge;
string m_sDepartment;
bool m_bStudent; //是否是学生,true(学生) false(老师)
Node * m_pNext;
};

class StuNode : public Node {
public:
StuNode(char *num, char *name, int nAge, char *department, bool bStu, int nGrade, int nAveScore)
: Node(num, name, nAge, department, bStu), m_nGrade(nGrade), m_nAveScore(nAveScore) {
}

StuNode() {

}

void SetGrade(int nGrade) {
m_nGrade = nGrade;
}

void SetAveScore(int nAveScore) {
m_nAveScore = nAveScore;
}

int GetGrade() const {
return m_nGrade;
}

int GetAveScore() const {
return m_nAveScore;
}

private:
int m_nGrade;
int m_nAveScore;
};

class TeacherNode : public Node {
public:
TeacherNode() {

}

TeacherNode(char *num, char *name, int nAge, char *department, bool bStu, char *idCardNo, char *jobTitle, int salary)
: Node(num, name, nAge, department, bStu), m_sIdCardNo(idCardNo), m_sJobTitle(jobTitle), m_nSalary(salary) {

}

void SetIdCardNo(string &idCardNo) {
m_sIdCardNo = idCardNo;
}

void SetJobTitle(string &jobTitle) {
m_sJobTitle = jobTitle;
}

void SetSalary(int salary) {
m_nSalary = salary;
}

string GetIdCardNo() const {
return m_sIdCardNo;
}

string GetJobTitle() const {
return m_sJobTitle;
}

int GetSalary() const {
return m_nSalary;
}

bool IsSenior() const {
//这儿根据情况判断一下,如果是高级职称就反加true
return true;
}

private:
string m_sIdCardNo;
string m_sJobTitle;
int m_nSalary;
};

class Manager {
public:
Manager() : m_pHead(0) {

}

void Insert(Node *pNode);

void DeleteByNum(char *num);

void clear();

//统计某个年级分数段人数
int CountStudent(int nGrade, int lScore, int rScore);

int CountTeacher(char *department);

private:
Node *m_pHead;
};

void Manager::Insert(Node *pNode) {
if (0 == m_pHead) {
m_pHead = pNode;
return ;
}
pNode->SetNext(m_pHead->GetNext());
m_pHead->SetNext(pNode);
}

void Manager::DeleteByNum(char *num) {
Node *p = m_pHead, *pPre = 0;
while(p) {
if (p->GetNum() == num) {
if (p == m_pHead) {
m_pHead = m_pHead->GetNext();
delete p;
}
else {
pPre->SetNext(p->GetNext());
delete p;
}
break;
}
pPre = p;
p = p->GetNext();
}
}

void Manager::clear() {
Node *p = m_pHead;
while(m_pHead) {
m_pHead = p->GetNext();
delete p;
}
}

int Manager::CountStudent(int nGrade, int lScore, int rScore) {
int count = 0;
Node *p = m_pHead;
while(p) {
if (!p->IsStudent()) {
p = p->GetNext();
continue;
}
StuNode *pStu = static_cast(p);
if (pStu->GetGrade() == nGrade && pStu->GetAveScore() >= lScore && pStu->GetAveScore() < rScore)
++count;
p = p->GetNext();
}
return count;
}

int Manager::CountTeacher(char *department) {
int count = 0;
Node *p = m_pHead;
while(p) {
if (p->IsStudent()) {
p = p->GetNext();
continue;
}
TeacherNode *pTeacher = static_cast(p);
if (pTeacher->GetDepartment() == department && pTeacher->IsSenior())
++count;
p = p->GetNext();
}
return count;
}

int main() {
Manager *pMgr = new Manager();
Node *pNode = 0;
pNode = new StuNode("10001", "xiaoming", 20, "computer department", true, 2, 98);
pMgr->Insert(pNode);

pNode = new TeacherNode("20001", "lilaoshi", 32, "computer department", false, "320882************", "某个职称", 5800);
pMgr->Insert(pNode);

cout << pMgr->CountStudent(2, 90, 100) << endl;
cout << pMgr->CountStudent(1, 90, 100) << endl;
cout << pMgr->CountStudent(2, 80, 90) << endl;
cout << pMgr->CountTeacher("computer department") << endl;
cout << pMgr->CountTeacher("computer departmen") << endl;
return 0;
}