基于单链表实现学生信息管理系统

#include
#include
#include
#include
#include
using namespace std;

struct node
{
    string name;
    string sex;
    int age;
    string address;
    string phone;
    node* next;
    node()//无参初始化
    {
        this->name="null";
        this->sex="null";
        this->age=0;
        this->address="null";
        this->phone="null";
        this->next=NULL;
    }
    node(string name,string sex,int age,string address,string phone,node* next)//带参初始化
    {
        this->name=name;
        this->sex=sex;
        this->age=age;
        this->address=address;
        this->phone=phone;
        this->next=next;
    }
};
node* head=NULL;//学生信息的头结点
node* read();//读取数据
void save(node* head);//保存数据
node* createLink();//创建头节点
void addNode(node* head,node student);
void addStudent(node *head);//增加学生信息
bool delNode(node* head,node student);
void delStudent(node* head);//删除学生信息
bool modifyNode(node* head,node student);
void modifyStudent(node* head);//修改学生信息
void display(node* head);
void displayStudent(node* head);//显示学生信息
void statisticStudent(node* head);//统计学生信息
void menu();//主菜单
bool isExist(node* head,node student);//判断学生是否存在

//读取文件
node* read()
{
    ifstream in;
    in.open("student.txt");
    if(!in) cout<<"fail to open this file!\n";
    node* head=createLink();//每次读取文件重新创建头节点,防止多次读入数据
    node* endNode=head;
    string s;
    getline(in,s);//跳过第一行
    while(!in.eof())
    {
        node* newNode=new node();
        in>>newNode->name>>newNode->sex>>newNode->age>>newNode->address>>newNode->phone;
        endNode->next=newNode;
        endNode=newNode;//指针后移
        in.get();
        if(in.peek()=='\n') break;//跳出最后一行回车
    }
    in.close();
    return head;
}

//保存文件
void save(node* head)
{
    ofstream out;
    out.open("student.txt");//追加到文件末端
    if(!out) cout<<"fail to open this file!\n";
    out<<"姓名\t"<<"性别\t"<<"年龄\t"<<"地址\t\t"<<"电话"<<endl;
    node* endNode=head->next;
    while(endNode!=NULL)
    {
        out<<endNode->name<<"\t"<<endNode->sex<<"\t"<<endNode->age<<"\t"<<endNode->address<<"\t"<<endNode->phone<<endl;
        endNode=endNode->next;
    }
    out.close();
}

bool isExist(node* head,node student)
{
    node* endNode=head->next;
    while(endNode!=NULL)
    {
        if(endNode->name==student.name)
        {
            return true;
        }
        endNode=endNode->next;
    }
    return false;
}

//创建链表头结点
node* createLink()
{
    node* head = new node();//初始化一个头结点是为了方便添加、删除操作
    return head;
}

//添加链表节点
void addNode(node* head,node student)
{
    node* endNow=head;
    node* newNode=new node(student.name,student.sex,student.age,student.address,student.phone,NULL);
    while(endNow->next!=NULL)
    {
        endNow=endNow->next;
    }
    newNode->next=endNow->next;
    endNow->next=newNode;
}

void addStudent(node *head)
{
    head=read();//读取数据
    node student;
    cout<<"输入学生姓名:";
    cin>>student.name;
    if(isExist(head,student))
    {
        cout<<"学生已存在!请重新输入!\n";
    }
    else
    {
        cout<<"输入学生性别:";
        cin>>student.sex;
        cout<<"输入学生年龄:";
        cin>>student.age;
        cout<<"输入学生地址:";
        cin>>student.address;
        cout<<"输入学生电话:";
        cin>>student.phone;
        cout<<"是否添加?y/n\n";
        char ch;
        cin>>ch;
        if(ch=='y'||ch=='Y')
        {
            addNode(head,student);
            cout<<"添加成功!\n";
        }
        else
            cout<<"添加失败!\n";
    }
    save(head);
}

//删除链表节点
bool delNode(node* head,node student)
{
    node* endNode=head->next;
    node* oldNode=head;
    while(endNode!=NULL)
    {
        if(endNode->name==student.name)
        {
            oldNode->next=endNode->next;
            free(endNode);
            return true;
        }
        endNode=endNode->next;
        oldNode=oldNode->next;
    }
    return false;
}


void delStudent(node* head)
{
    head=read();
    displayStudent(head);
    cout<<"请输入要删除的学生姓名:";
    node student;
    cin>>student.name;
    if(isExist(head,student))
    {
        cout<<"是否删除?y/n\n";
        char ch;
        cin>>ch;
        if(ch=='y'||ch=='Y')
        {
            bool flag=delNode(head,student);
            if(flag) cout<<"删除成功!\n";
            else cout<<"删除失败!\n";
        }
        else cout<<"取消成功!\n";
    }
    else
        cout<<"学生不存在!\n";
    save(head);
}

//修改链表节点
bool modifyNode(node* head,node student)
{
    node* endNode=head->next;
    while(endNode!=NULL)
    {
        if(endNode->name==student.name)
        {
            endNode->name=student.name;
            endNode->sex=student.sex;
            endNode->age=student.age;
            endNode->address=student.address;
            endNode->phone=student.phone;
//            endNode=new node(student.name,student.sex,student.age,student.address,student.phone,NULL);//指向一个新的节点,并没有修改链表中需要修改的节点
            return true;
        }
        endNode=endNode->next;
    }
    return false;
}


void modifyStudent(node* head)
{
    head=read();
    displayStudent(head);
    cout<<"请输入要修改的学生姓名:";
    node student;
    cin>>student.name;
    if(isExist(head,student))
    {
        cout<<"请修改该学生信息:\n";
        cout<<"输入学生性别:";
        cin>>student.sex;
        cout<<"输入学生年龄:";
        cin>>student.age;
        cout<<"输入学生地址:";
        cin>>student.address;
        cout<<"输入学生电话:";
        cin>>student.phone;
        cout<<"是否修改?y/n\n";
        char ch;
        cin>>ch;
        if(ch=='y'||ch=='Y')
        {
            bool flag=modifyNode(head,student);
            if(flag) cout<<"修改成功!\n";
            else cout<<"修改失败!\n";
        }
        else cout<<"取消成功!\n";
    }
    else cout<<"学生不存在!\n";
    save(head);
}

//遍历链表节点
void display(node* head)
{
    node* currNode=head->next;
    cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"年龄"<<"\t"<<"地址"<<"\t\t"<<"电话"<<endl;
    while(currNode!=NULL)
    {
        cout<<currNode->name<<"\t"<<currNode->sex<<"\t"<<currNode->age<<"\t"<<currNode->address<<"\t"<<currNode->phone<<endl;
        currNode=currNode->next;
    }
}

void displayStudent(node* head)
{
    head=read();
    display(head);
}

void statisticStudent(node* head)
{
    head=read();
    int tol=0,femaleTol=0,maleTol=0;
    node* currNode=head->next;
    cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"年龄"<<"\t"<<"地址"<<"\t\t"<<"电话"<<endl;
    while(currNode!=NULL)
    {
        cout<<currNode->name<<"\t"<<currNode->sex<<"\t"<<currNode->age<<"\t"<<currNode->address<<"\t"<<currNode->phone<<endl;
        ++tol;
        if(currNode->sex=="男") maleTol++;
        if(currNode->sex=="女") femaleTol++;
        currNode=currNode->next;
    }
    cout<<"总共有"<<tol<<"位同学!\n";
    cout<<"其中男生有:"<<maleTol<<"人,"<<"女生有"<<femaleTol<<"人。\n";
}

//功能菜单
void menu()
{
    cout<<"学生信息管理系统\n";
    cout<<"1.添加学生\n";
    cout<<"2.删除学生\n";
    cout<<"3.修改学生\n";
    cout<<"4.查找学生\n";
    cout<<"0.退出系统\n";
    cout<<"请选择功能:\n";
}


int main()
{
    head=createLink();
    while(true)
    {
        system("cls");
        menu();
        char ch;
        cin>>ch;
        switch(ch)
        {
        case '1':
            system("cls");
            addStudent(head);
            break;
        case '2':
            system("cls");
            delStudent(head);
            break;
        case '3':
            system("cls");
            modifyStudent(head);
            break;
        case '4':
            system("cls");
            statisticStudent(head);
            break;
        case '0':
            cout<<"退出系统成功!\n";
            exit(0);
            break;
        default:
            cout<<"输入错误,请重新输入!\n";
            break;
        }
        system("pause");
    }
    return 0;
}

你可能感兴趣的:(基于单链表实现学生信息管理系统)