(链表)学生管理系统

#include<iostream>
#include<string>

using namespace std;

struct Student{
    string name; //姓名
    string id; //学号
    double score;  //成绩
    struct Student *next; 
};

//按照姓名判断学生是否存在
int isExist1(struct Student * head,string name){
    struct Student * t=head;
    int i=0;
    while(t->next){
        t=t->next;
        if(t->name==name){
            return i;
        }
        i++;
    }
    return -1;
}

//按照学号判断学生是否存在
int isExist2(struct Student * head,string id){
    struct Student * t=head;
    int i=0;
    while(t->next){
        t=t->next;
        if(t->id==id){
            return i;
        }
        i++;
    }
    return -1;
}


void Menu(){
    cout<<"************************************"<<endl;
    cout<<"************************************"<<endl;
    cout<<"     1———添加学生信息"<<endl;
    cout<<"     2———删除学生信息"<<endl;
    cout<<"     3———查找学生信息"<<endl;
    cout<<"     4———修改学生信息"<<endl;
    cout<<"     5———显示所有学生信息"<<endl;
    cout<<"     6———退出系统"<<endl;
    cout<<"************************************"<<endl;
    cout<<"************************************"<<endl;
}

//1.添加学生信息
void addStudent(struct Student * head){
    struct Student * p = new struct Student;
    string name;
    string id;
    double score;
    //姓名
    cout<<"请输入名字:"<<endl;
    cin>>name;
    p->name=name;
    //学号
    cout<<"请输入学号:"<<endl;
    cin>>id;
    p->id=id;
    //成绩
    while(1){
        cout<<"请输入分数:"<<endl;
        cin>>score;
        if(score<0||score>100){
            cout<<"输入有误,请重新输入"<<endl;
            system("pause");
        }
        else{
            p->score=score;
            break;
        }
    }
    p->next=head->next;
    head->next=p;
    //更新数据库信息
    cout<<"添加成功!"<<endl;
}

//2.删除学生信息
void deleteStudent(struct Student * head){
    struct Student * temp = head;
    if(head->next==nullptr){
        cout<<"数据库中无数据,请添加数据"<<endl;
        return;
    }
    string name;
    cout<<"请输入要删除的学生的姓名:"<<endl;
    cin>>name;
    int ret=isExist1(head,name);
    if(ret!=-1){
        for(int i=0;i<ret;i++){
            temp=temp->next;
        }
        struct Student * del = temp->next;
        temp->next=temp->next->next;
        free(del); //手动释放该节点
        cout<<"删除成功"<<endl;      
}
    else{
        cout<<"未查询到该学生"<<endl;
    } }


//3.1 按学生姓名查找学生
void findStudent1(struct Student * head){
    struct Student * temp = head;
    if(head->next==nullptr){
        cout<<"数据库中无数据,请添加数据"<<endl;
        return;
    }
    string name;
    cout<<"请输入要查找学生姓名:"<<endl;
    cin>>name;
    int ret=isExist1(head,name);
    if(ret!=-1){
        int i=ret;
        for(int j=0;j<=ret;j++){
            temp=temp->next;
        }
        cout<<"序号"<<"\t姓名"<<"\t学号"<<"\t\t成绩"<<endl;
        cout<<i+1<<"."<<"\t"<<temp->name<<"\t"<<temp->id<<"\t"<<temp->score<<endl;
    }
    else{
        cout<<"未查询到该学生"<<endl;
    } }


//3.2 按学生学号查找学生
void findStudent2(struct Student * head){
    struct Student * temp = head;
    if(head->next==nullptr){
        cout<<"数据库中无数据,请添加数据"<<endl;
        return;
    }
    string id;
    cout<<"请输入要查找学生学号:"<<endl;
    cin>>id;
    int ret=isExist2(head,id);
    if(ret!=-1){
        int i=ret;
        for(int j=0;j<=ret;j++){
            temp=temp->next;
        }
        cout<<"序号"<<"\t姓名"<<"\t学号"<<"\t\t成绩"<<endl;
        cout<<i+1<<"."<<"\t"<<temp->name<<"\t"<<temp->id<<"\t"<<temp->score<<endl;
    }
    else{
        cout<<"未查询到该学生"<<endl;
    } }


//4.修改学生信息
void modifyStudent(struct Student * head){
    struct Student * temp = head;
    if(head->next==nullptr){
        cout<<"数据库中无数据,请添加数据"<<endl;
        return;
    }
    string name;
    cout<<"请输入要修改的学生的姓名:"<<endl;
    cin>>name;
    int ret=isExist1(head,name);
    if(ret!=-1){
        int i=ret;
        for(int j=0;j<=ret;j++){
            temp=temp->next;
        }
        int change; //用于后续选择
        cout<<"序号"<<"\t姓名"<<"\t学号"<<"\t\t成绩"<<endl;
        cout<<i+1<<"."<<"\t"<<temp->name<<"\t"<<temp->id<<"\t"<<temp->score<<endl;
        cout<<"请输入要修改的信息:"<<endl;
        cout<<"1.姓名"<<endl;
        cout<<"2.学号"<<endl;
        cout<<"3.成绩"<<endl;
        while(1){
            cin>>change;
            if(change==1){
                string name1;
                cout<<"请输入姓名:"<<endl;
                cin>>name1;
                temp->name=name1;
                cout<<"修改成功!"<<endl;
                break;
            }
            else if(change==2){
                string id1;
                cout<<"请输入学号:"<<endl;
                cin>>id1;
                temp->id=id1;
                cout<<"修改成功!"<<endl;
                break;
            }
            else if(change==3){
                double score1;
                cout<<"请输入成绩:"<<endl;
                cin>>score1;
                temp->score=score1;
                cout<<"修改成功!"<<endl;
                break;
            }
            else{
                cout<<"输入有误,请重新输入"<<endl;
                system("pause");
            }
        }
    }
    else{
        cout<<"未查询到该学生"<<endl;
    } }


//5.显示学生信息
void shouStudent(struct Student * head){
    int i=0;
    struct Student * temp = head;
    if(head->next==nullptr){
        cout<<"数据库中无数据,请添加数据"<<endl;
        return;
    }
    cout<<"序号"<<"\t姓名"<<"\t学号"<<"\t\t成绩"<<endl;
    while(temp->next){
        temp=temp->next;
        cout<<i+1<<"."<<"\t"<<temp->name<<"\t"<<temp->id<<"\t"<<temp->score<<endl;
        i++;
    } }


int main() {

    struct Student * head = new struct Student;
    head->name="";
    head->id="";
    head->score=(double)NULL;
    head->next=nullptr;
         
int select;
         
while(1){
        Menu();
        cin>>select;
        switch (select) {
            case 1: //添加学生信息
            addStudent(head);
            system("pause");
            system("cls");
            break;
            case 2: //删除学生信息
            deleteStudent(head);
            system("pause");
            system("cls");
            break;
            case 3: //查找学生信息
            int select1; //判断查找方式
            while(1){
                cout<<"请选择如何查找"<<endl;
                cout<<"1.按学生姓名查找"<<endl;
                cout<<"2.按学生学号查找"<<endl;
                cout<<"3.返回菜单"<<endl;
                cin>>select1;
                if(select1==1){
                    findStudent1(head); //按学生姓名查找学生
                    system("pause");
                    system("cls");
                    break;
                }
                else if(select1==2){
                    findStudent2(head); //按学生学号查找学生
                    system("pause");
                    system("cls");
                    break;
                }
                else if(select1==3){
                    system("pause");
                    system("cls");
                    break;
                }
                else{
                    cout<<"输入有误,请重新输入"<<endl;
                    system("pause");
                }
            }
            break;
            case 4: //修改学生信息
            modifyStudent(head);
            system("pause");
            system("cls");
            break;
            case 5: //显示所有学生信息
            shouStudent(head);
            system("pause");
            system("cls");
            break;
            case 6: //退出系统
            int select2; //仅用于判断是否真的退出
            cout<<"请确认是否退出系统"<<endl;
            cout<<"1.是"<<endl;
            cout<<"2.否"<<endl;
            while(1){
                cin>>select2;
                if(select2==1){
                    cout<<"感谢您的本次使用,欢迎下次使用"<<endl;
                    system("pause");
                    return 0;
                }
                else if(select2==2){
                    system("cls");
                    break;
                }
                else{
                    cout<<"输入有误,请重新输入"<<endl;
                    system("pause");
                }
            }
            break;
        default:
            cout<<"输入有误,请重新输入"<<endl;
            system("pause");
            system("cls");
        }
    }
    system("pause");
    return 0; }

posted @   LuMinarY-  阅读(142)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示