// ZhengTest.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <string>using namespace std;/*学生信息类*/class stud_info{private: string m_strName;//学生姓名 string m_strCode;//学生编号 string m_strTel;//联系方式public: stud_info(string strName = "",string strCode = "",string strTel = "")//构造函数 { m_strName=strName; m_strCode=strCode; m_strTel=strTel; pNext=NULL; } stud_info *pNext; string GetName();//获得学生姓名 string GetCode();//获得学生学编号 string GetTel();//获得学生联系方式 };/*信息消息类*/class info_mag{private: int m_iMaxCapacity; //信息管理允许的最大容量 int m_iInfoLength; //目前存储的信息容量 stud_info *Head; //列表头指针 public: /*构造函数*/ info_mag(int Length = 0,int Max = 5) { m_iMaxCapacity = Max; m_iInfoLength = Length; this->Head = new stud_info(); } /*获取当前列表长度*/ int GetCurrentLength() { return m_iInfoLength; } /*获得头指针*/ stud_info *GetPHead() { return this->Head; } /*设定头指针*/ stud_info *SetPHead(stud_info *pNext) { this->Head = pNext; return this->Head; } /*打印列表信息*/ bool writeAll() { stud_info *p = new stud_info(); cout<<"姓名"<<"\t学号"<<"\t电话"<<endl; p = Head; while (p->pNext != NULL) { cout<<p->GetName()<<"\t"<<p->GetCode()<<"\t"<<p->GetTel()<<endl; p = p->pNext; } if(p->pNext == NULL) { cout<<p->GetName()<<"\t"<<p->GetCode()<<"\t"<<p->GetTel()<<endl; } return true; } /*添加列表信息*/ bool Add(stud_info &m_info) { stud_info *p = new stud_info(); stud_info *q = new stud_info(); if(this->m_iInfoLength <= this->m_iMaxCapacity) { if(GetCurrentLength() == 0) { this->Head = &m_info; this->m_iInfoLength++; } else { p = this->Head; q = p; while(p->pNext!=NULL) { q = p; p = p->pNext; } p->pNext = &m_info; this->m_iInfoLength++; } return true; } else { return false; } } /*查询学生信息*/ stud_info *Select(string strName) { stud_info *p = new stud_info(); stud_info *q = new stud_info(); p = Head; while (p->pNext != NULL) { if(p->GetName() == strName) { return p; } q = p; p = p->pNext; if(p == NULL) { return NULL; } } if ((p->GetName()) != strName) { return NULL; } else { return p; } }};/*获得学生姓名*/string stud_info ::GetName(){ return m_strName;}/*获得学生编号*/string stud_info::GetCode(){ return m_strCode;}/*获得学生联系方式*/string stud_info::GetTel(){ return m_strTel;}int _tmain(int argc, _TCHAR* argv[]){ string s = ""; info_mag infm = info_mag();//创建列表对象 //创建5个学生信息对象 stud_info m_info = stud_info("Sam","001","1356688789"); stud_info m_info2 = stud_info("Bob","002","1356688790"); stud_info m_info3 = stud_info("Jack","003","1356688790"); stud_info m_info4 = stud_info("Lucy","004","1356688790"); stud_info m_info5 = stud_info(); //向列表中添加学生信息 infm.Add(m_info); infm.Add(m_info2); infm.Add(m_info3); infm.Add(m_info4); //打印学生信息 infm.writeAll(); cout<<"是否添加新学生信息?(y/n)"<<endl; char c = 'n'; cin>>c; if (c == 'y'||c == 'Y') { string sName; string sCode; string sTel; cout<<"输入姓名:"<<endl; cin>>sName; cout<<"输入学号:"<<endl; cin>>sCode; cout<<"输入联系方式:"<<endl; cin>>sTel; m_info5 = stud_info(sName,sCode,sTel); if (infm.Select(sName) == NULL) { infm.Add(m_info5); } } cout<<"请输入您要查询的姓名:"<<endl; cin>>s; //查询学生信息 stud_info *p= new stud_info(); p = infm.Select(s); if (p != NULL) { cout<<"\n找到查询信息:\n"<<endl; cout<<"姓名:"<<p->GetName()<<"\n学号:"<<p->GetCode()<<"\n电话:"<<p->GetTel()<<endl; } else { cout<<"\n您所查询的姓名不存在!\n"<<endl; } cout<<"按任意键结束!"<<endl; getchar(); return 0;}
添加回答
举报
0/150
提交
取消