⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teacher.cpp

📁 2003 - 2004 学年度(下)C++ 课程设计题目 实现简单的教师信息管理系统
💻 CPP
字号:
// 简单的教师信息管理系统
// 作者: 沈阳航空工业学院, 计算机科学与技术专业 2401110 赵君
// 电子邮件: junjun0403@163.com

// 2003 - 2004 学年度(下)C++ 课程设计题目
// 实现简单的教师信息管理系统,使用到了链表,定义了一个 TeacherList 类,
// 可以实现简单的插入,查找,删除等功能,涉及到二进制文件的读写操作。

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <conio.h>

enum bool{false, true};

#define ID_LEN           6
#define NAME_LEN         12
#define SEX_LEN          8
#define BIRTHDAY_LEN     12
#define EDUCATION_LEN    12
#define POST_LEN         12
#define PHONE_LEN        10

#define DATAFILE         "teacher.dat"

void showsearchmenu();
void showmainmenu();
void waitpresskey();

struct teacher
{
    char id[ID_LEN];
    char name[NAME_LEN];
    char sex[SEX_LEN];
    char birthday[BIRTHDAY_LEN];
    char education[EDUCATION_LEN];
    char post[POST_LEN];
    char phone[PHONE_LEN];
    teacher * next;
};

class TeacherList
{
private:
    teacher * head;
    int size;
public:
    TeacherList()
    {
	head = 0;
	size = 0;
    }
    ~TeacherList();
    bool insert();
    bool deletes();
    bool edit();
    bool search();
    bool SearchbyID();
    bool SearchbyName();
    bool SearchbySex();
    bool SearchbyBirthday();
    bool SearchbyEducation();
    bool SearchbyPost();
    bool SearchbyPhone();
    int openfile();
    bool closefile();
    bool savefile();
    void setid(teacher *);
    void setname(teacher *);
    void setsex(teacher *);
    void setbirthday(teacher *);
    void seteducation(teacher *);
    void setpost(teacher *);
    void setphone(teacher *);
    void showteacher(teacher *);
    void showtitle();
    void display();
};

TeacherList::~TeacherList()
{
    teacher * temp;
    for(teacher * p = head; p ; )
    {
	temp = p;
	p = p->next;
	delete temp;
    }
}
bool TeacherList::savefile()
{
    ofstream out;
    teacher * temp;
    out.open(DATAFILE);
    if(!out)
    {
	cout<<"Cannot open file to write!"<<endl;
	waitpresskey();
	return false;
    }
    out.write((char *)&size, sizeof(int));
    for(temp = head; temp; temp=temp->next)
    {
	out.write((char *)temp, sizeof(teacher));
    }
    out.close();
    cout<<"Save operate successful!"<<endl;
    waitpresskey();
    return true;
}

int TeacherList::openfile()
{
    ifstream in;
    teacher temp, * p;
    int i, count;
    in.open(DATAFILE);
    if(!in)
	return 0;
    in.read((char *)&count, sizeof(count));
    size = 0;
    for(i = 0; i<count; i++)
    {
	in.read((char *)&temp, sizeof(teacher));
	p = new teacher;
	if(!p)
	    return -1;
	strcpy(p->id, temp.id);
	strcpy(p->name, temp.name);
	strcpy(p->sex, temp.sex);
	strcpy(p->birthday, temp.birthday);
	strcpy(p->education, temp.education);
	strcpy(p->post, temp.post);
	strcpy(p->phone, temp.phone);
	p->next = head;
	head = p;
	size++;
    }
    in.close();
    return 1;
}

void TeacherList::setid(teacher * vteacher)
{
    char vid[ID_LEN];
    cout<<"Enter ID: ";
    cin.getline(vid, ID_LEN);
    strcpy(vteacher->id, vid);
}

void TeacherList::setname(teacher * vteacher)
{
    char vname[NAME_LEN];
    cout<<"Enter name: ";
    cin.getline(vname, NAME_LEN);
    strcpy(vteacher->name, vname);
}

void TeacherList::setsex(teacher * vteacher)
{
    char vsex[SEX_LEN];
    cout<<"Enter sex: ";
    cin.getline(vsex, SEX_LEN);
    strcpy(vteacher->sex, vsex);
}

void TeacherList::setbirthday(teacher * vteacher)
{
    char vbirthday[BIRTHDAY_LEN];
    cout<<"Enter birthday: ";
    cin.getline(vbirthday, BIRTHDAY_LEN);
    strcpy(vteacher->birthday, vbirthday);
}

void TeacherList::seteducation(teacher * vteacher)
{
    char veducation[EDUCATION_LEN];
    cout<<"Enter education: ";
    cin.getline(veducation, EDUCATION_LEN);
    strcpy(vteacher->education, veducation);
}

void TeacherList::setpost(teacher * vteacher)
{
    char vpost[POST_LEN];
    cout<<"Enter post: ";
    cin.getline(vpost, POST_LEN);
    strcpy(vteacher->post, vpost);
}

void TeacherList::setphone(teacher * vteacher)
{
    char vphone[PHONE_LEN];
    cout<<"Enter phone: ";
    cin.getline(vphone, PHONE_LEN);
    strcpy(vteacher->phone, vphone);
}

bool TeacherList::insert()
{
    teacher * teachers = new teacher;
    if(teachers)
    {
	setid(teachers);
	setname(teachers);
	setsex(teachers);
	setbirthday(teachers);
	seteducation(teachers);
	setpost(teachers);
	setphone(teachers);
	teachers->next = head;
	head = teachers;
	size++;
	cout<<"Insert operation successful!"<<endl;
	waitpresskey();
	return true;
    }
    cout<<"Not enough memory, insert operate failed!"<<endl;
    waitpresskey();
    return false;
}

bool TeacherList::deletes()
{
    teacher * temp;
    char vid[ID_LEN];
    cout<<"Enter ID to delete: ";
    cin.getline(vid, ID_LEN);
    if(strcmp(head->id, vid) == 0)
    {
	temp = head->next;
	delete head;
	size--;
	head = temp;
	cout<<"Delete operate successful!"<<endl;
	waitpresskey();
	return true;
    }
    for(teacher * p=temp=head; p ;temp=p, p=p->next)
    {
	if((strcmp(p->id, vid) == 0) && (p != head))
	{
	    temp->next = p->next;
	    delete p;
	    size--;
	    cout<<"Delete operate successful!"<<endl;
	    waitpresskey();
	    return true;
	}
    }
    cout<<"Cannot find this ID, delete operation failed!"<<endl;
    waitpresskey();
    return false;
}

bool TeacherList::edit()
{
    char vid[ID_LEN];
    cout<<"Enter ID to edit: ";
    cin.getline(vid, ID_LEN);
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->id, vid) == 0)
	{
	    setid(p);
	    setname(p);
	    setsex(p);
	    setbirthday(p);
	    seteducation(p);
	    setpost(p);
	    setphone(p);
	    cout<<"Edit operate successful!"<<endl;
	    waitpresskey();
	    return true;
	}
    }
    cout<<"Cannot find this ID, edit operation failed!"<<endl;
    waitpresskey();
    return false;
}

bool TeacherList::SearchbyID()
{
    int total = 0;
    char vid[ID_LEN];
    cout<<"Enter ID to search: ";
    cin.getline(vid, ID_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->id, vid) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbyName()
{
    int total = 0;
    char vname[NAME_LEN];
    cout<<"Enter name to search: ";
    cin.getline(vname, NAME_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->name, vname) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbySex()
{
    int total = 0;
    char vsex[SEX_LEN];
    cout<<"Enter sex to search: ";
    cin.getline(vsex, SEX_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->sex, vsex) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbyBirthday()
{
    int total = 0;
    char vbirthday[BIRTHDAY_LEN];
    cout<<"Enter birthday(format yyyy.mm.dd) to search: ";
    cin.getline(vbirthday, BIRTHDAY_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->birthday, vbirthday) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbyEducation()
{
    int total = 0;
    char veducation[EDUCATION_LEN];
    cout<<"Enter education to search: ";
    cin.getline(veducation, EDUCATION_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->education, veducation) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbyPost()
{
    int total = 0;
    char vpost[POST_LEN];
    cout<<"Enter post to search: ";
    cin.getline(vpost, POST_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->post, vpost) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::SearchbyPhone()
{
    int total = 0;
    char vphone[PHONE_LEN];
    cout<<"Enter phone to search: ";
    cin.getline(vphone, PHONE_LEN);
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return false;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next)
    {
	if(strcmp(p->phone, vphone) == 0)
	{
	    showteacher(p);
	    total++;
	}
    }
    cout<<endl<<"Found "<<total<<" record(s)!"<<endl;
    waitpresskey();
    if(total > 0)
	return true;
    else
	return false;
}

bool TeacherList::search()
{
    teacher * temp;
    char vchoice;
    while(1)
    {
	showsearchmenu();
	vchoice = getche();
	cout<<endl;
	switch(vchoice)
	{
	    case '1':
		SearchbyID();
		break;
	    case '2':
		SearchbyName();
		break;
	    case '3':
		SearchbySex();
		break;
	    case '4':
		SearchbyBirthday();
		break;
	    case '5':
		SearchbyEducation();
		break;
	    case '6':
		SearchbyPost();
		break;
	    case '7':
		SearchbyPhone();
		break;
	    case '8':
		break;
	    default:
		cout<<"Input error, please enter a correct choice!"<<endl;
		waitpresskey();
		break;
	}
	if(vchoice == '8')
	    break;
    }
    return true;
}

void TeacherList::showtitle()
{
    cout.width(ID_LEN);
    cout<<"ID";
    cout.width(NAME_LEN);
    cout<<"Name";
    cout.width(SEX_LEN);
    cout<<"Sex";
    cout.width(BIRTHDAY_LEN);
    cout<<"Birthday";
    cout.width(EDUCATION_LEN);
    cout<<"Education";
    cout.width(POST_LEN);
    cout<<"Post";
    cout.width(PHONE_LEN);
    cout<<"Phone"<<endl<<endl;
}

void TeacherList::showteacher(teacher * vteacher)
{
    cout.width(ID_LEN);
    cout<<vteacher->id;
    cout.width(NAME_LEN);
    cout<<vteacher->name;
    cout.width(SEX_LEN);
    cout<<vteacher->sex;
    cout.width(BIRTHDAY_LEN);
    cout<<vteacher->birthday;
    cout.width(EDUCATION_LEN);
    cout<<vteacher->education;
    cout.width(POST_LEN);
    cout<<vteacher->post;
    cout.width(PHONE_LEN);
    cout<<vteacher->phone;
    cout<<endl;
}

void TeacherList::display()
{
    int i = 1;
    if(size == 0)
    {
	cout<<"Cannot find any records!"<<endl;
	waitpresskey();
	return;
    }
    showtitle();
    for(teacher * p=head; p ;p=p->next, i++)
    {
	showteacher(p);
	if(i % 15 == 0)
	    waitpresskey();
    }
    cout<<endl<<"Total: "<<size<<" records(s)."<<endl;
    waitpresskey();
}

void showsearchmenu()
{
    clrscr();
    cout<<"******** Welcome to use search sub-system ** v0.9 ********"<<endl;
    cout<<"*                                                        *"<<endl;
    cout<<"*    1 - search by ID         5 - Search by education    *"<<endl;
    cout<<"*    2 - search by name       6 - Search by post         *"<<endl;
    cout<<"*    3 - search by sex        7 - Search by phone        *"<<endl;
    cout<<"*    4 - search by birthday   8 - Return                 *"<<endl;
    cout<<"**********************************************************"<<endl;
    cout<<"Enter your choice[1 - 8]: ";
}

void showmainmenu()
{
    clrscr();
    cout<<"******** Welcome to use teacher system ** v0.9 ********"<<endl;
    cout<<"*                                                     *"<<endl;
    cout<<"*    1 - Insert records        5 - Display records    *"<<endl;
    cout<<"*    2 - Edit records          6 - Save records       *"<<endl;
    cout<<"*    3 - Delete records                               *"<<endl;
    cout<<"*    4 - Search records        7 - Exit               *"<<endl;
    cout<<"*******************************************************"<<endl;
    cout<<"Enter your choice[1 - 7]: ";
}

void waitpresskey()
{
    cout<<"Press any key to continue...";
    getch();
    cout<<endl;
}

int main(void)
{
    TeacherList m_teacher;
    char ch;
    int errorcode;
    errorcode = m_teacher.openfile();
    if(errorcode == 0)
    {
	cout<<"Cannot open file to load data!"<<endl;
	cout<<"WARNING:"<<
	"You should insert records and save them if you use this program at first time!"<<endl;
	waitpresskey();
    }
    if(errorcode == -1)
    {
	cout<<"Not enough memory to load data!"<<endl;
	return 0;
    }
    textbackground(1);
    textcolor(15);
    clrscr();
    while(1)
    {
	showmainmenu();
	ch = getche();
	cout<<endl;
	switch(ch)
	{
	    case '1':
		m_teacher.insert();
		break;
	    case '2':
		m_teacher.edit();
		break;
	    case '3':
		m_teacher.deletes();
		break;
	    case '4':
		m_teacher.search();
		break;
	    case '5':
		m_teacher.display();
		break;
	    case '6':
		m_teacher.savefile();
		break;
	    case '7':
		break;
	    default:
		cout<<"Input error, please enter a correct choice!"<<endl;
		waitpresskey();
		break;

	}
	if(ch == '7')
	    break;
    }
    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -