晓晓942 2021-06-14 19:26 采纳率: 100%
浏览 197
已采纳

用C++设计一个学生信息管理系统

 

  • 写回答

2条回答 默认 最新

  • CSDN专家-link 2021-06-14 19:38
    关注
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<fstream>
    #include<algorithm>
    #include<iomanip>
    #include<cstdio>
    #include<cstdlib>
    #include<conio.h>
    #include<windows.h>
    using namespace std;
    class student
    {
    private:
        long long  num;
        char name[20];
        char sex[6];
        int age;
        char  phone[20];
    public:
        static int NUM;
        student() {}
        ~student() {}
        void input();
        friend void readin();
        friend   void show();
        friend   void mood();
        friend  void del();
        friend  void soort();
        friend   void save();
        friend  void findyou();
        friend void clear1();
    } zhuo_yue[100];
    int student::NUM=0;
    void readin()
    {
        ifstream in("inf.txt",ios::binary);
        int i=1;
        while(!in.eof())
        {
            in.read((char*) &zhuo_yue[i],sizeof(zhuo_yue[i]));
            i++;
        }
        in.close();
        student::NUM=i-2;
    }
    void show()
    {
        if(student::NUM==0)
            cout<<endl<<endl<<setw(10)<<"暂无学生信息";
        else
        {
            cout<<"学生信息显示"<<endl<<endl;
            cout<<"学号"<<setw(15)<<"姓名"<<setw(15)
                <<"性别"<<setw(15)<<"年龄"<<setw(15)<<"电话"
                <<endl;
            for(int i=1; i<=student::NUM; i++)
            {
                cout<<zhuo_yue[i].num<<setw(15)<<zhuo_yue[i].name<<setw(15)
                    <<zhuo_yue[i].sex<<setw(15)<<zhuo_yue[i].age<<setw(15)<<zhuo_yue[i].phone
                    <<endl;
            }
        }
        cout<<endl<<setw(40)<<"按任意键退出";
        getch();
    }
    void mood()
    {
        long long L;
        cout<<"请输入欲修改的学号:";
        cin>>L;
        cout<<endl<<setw(40)<<"确认修改(y/n)";
        if(getch()=='y')
        {
            system("cls");
            for(int i=1; i<=student::NUM; i++)
                if(L==zhuo_yue[i].num)
                {
                    cout<<"学生信息修改"<<endl<<endl;
                    cout<<"学号:";
                    cin>>zhuo_yue[i].num;
                    cout<<endl<<endl;
                    cout<<"姓名:";
                    cin>>zhuo_yue[i].name;
                    cout<<endl<<endl;
                    cout<<"性别:";
                    cin>>zhuo_yue[i].sex;
                    cout<<endl<<endl;
                    cout<<"年龄:";
                    cin>>zhuo_yue[i].age;
                    cout<<endl<<endl;
                    cout<<"电话:";
                    cin>>zhuo_yue[i].phone;
                    cout<<endl<<endl;
                    break;
                }
            cout<<endl<<setw(40)<<"已修改,按任意键退出";
            getch();
        }
        else
            return;
    }
    void del()
    {
        int sum=0;
        cout<<setw(50)<<"[1]按学号删除                 [2]按姓名删除"<<endl;
        if(getch()=='1')
        {
            cout<<"请输入学号:";
            long long it;
            cin>>it;
            cout<<endl<<setw(30)<<"您确定删除此学生吗?(y/n)";
            if(getch()=='y')
            {
                system("cls");
                for(int i=1; i<=student::NUM; i++)
                {
                    if(zhuo_yue[i].num==it)
                    {
                        for(int j=i+1; j<=student::NUM; j++)
                            zhuo_yue[j-1]=zhuo_yue[j];
                        student::NUM--;
                        sum++;
                        break;
                    }
                }
                cout<<"共删除"<<sum<<"人";
            }
            else
                return;
        }
        else  if(getch()=='2')
        {
            char name1[20];
            cout<<"请输入姓名:";
            cin>>name1;
            cout<<endl<<setw(30)<<"您确定删除此学生吗?(y/n)";
            if(getch()=='y')
            {
                system("cls");
                for(int i=1; i<=student::NUM; i++)
                {
                    if(strcmp(zhuo_yue[i].name,name1)==0)
                    {
                        for(int j=i+1; j<=student::NUM; j++)
                            zhuo_yue[j-1]=zhuo_yue[j];
                        student::NUM--;
                        sum++;
                    }
                }
                cout<<"共删除"<<sum<<"人";
            }
            else
                return;
        }
        cout<<endl<<endl<<setw(40)<<"按任意键结束";
        getch();
    }
    void soort()
    {
        cout<<setw(50)<<"[1]按学号排序                 [2]按年龄排序"<<endl;
        if(getch()=='1')
        {
            for(int i=1; i<student::NUM; i++)
                for(int j=1; j<student::NUM-i+1; j++)
                {
                    if(zhuo_yue[j].num>zhuo_yue[j+1].num)
                    {
                        student it=zhuo_yue[j];
                        zhuo_yue[j]=zhuo_yue[j+1];
                        zhuo_yue[j+1]=it;
                    }
                }
            cout<<endl<<setw(50)<<"已排序,按任意键退出";
            getch();
        }
        else  if(getch()=='2')
        {
            for(int i=1; i<student::NUM; i++)
                for(int j=1; j<student::NUM-i+1; j++)
                {
                    if(zhuo_yue[j].age>zhuo_yue[j+1].age)
                    {
                        student it=zhuo_yue[j];
                        zhuo_yue[j]=zhuo_yue[j+1];
                        zhuo_yue[j+1]=it;
                    }
                }
            cout<<endl<<setw(50)<<"已排序,按任意键退出";
            getch();
        }
    }
    void student::input()
    {
    LI:
        NUM++;
        cout<<"学生信息输入"<<endl<<endl;
        cout<<"学号:";
        cin>>zhuo_yue[NUM].num;
        cout<<endl<<endl;
        cout<<"姓名:";
        cin>>zhuo_yue[NUM].name;
        cout<<endl<<endl;
        cout<<"性别:";
        cin>>zhuo_yue[NUM].sex;
        cout<<endl<<endl;
        cout<<"年龄:";
        cin>>zhuo_yue[NUM].age;
        cout<<endl<<endl;
        cout<<"电话:";
        cin>>zhuo_yue[NUM].phone;
        cout<<endl<<endl;
        cout<<"已输入,是否继续  (y/n)";
        if(getch()=='y')
        {
            system("cls");
            goto LI;
        }
        else return;
    }
    void save()
    {
        ofstream out("inf.txt",ios::binary);
        cout<<endl<<setw(40)<<"已保存,按任意键退出";
        for(int i=1; i<=student::NUM; i++)
        {
            out.write((char*)&zhuo_yue[i],sizeof(zhuo_yue[i]));
        }
        out.close();
        getch();
    }
    void findyou()
    {
        int sum=0;
        cout<<setw(50)<<"[1]按学号查找                 [2]按姓名查找"<<endl;
        if(getch()=='1')
        {
            cout<<"请输入学号:";
            long long it;
            cin>>it;
            cout<<endl<<setw(30)<<"您确定查看此学生吗?(y/n)";
            if(getch()=='y')
            {
                system("cls");
                cout<<"查看学生信息"<<endl<<endl;
                for(int i=1; i<=student::NUM; i++)
                {
                    if(zhuo_yue[i].num==it)
                    {
                        cout<<setw(20)<<"学号:"<<zhuo_yue[i].num<<endl
                            <<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl
                            <<setw(20)<<"性别:"<<zhuo_yue[i].sex<<endl
                            <<setw(20)<<"年龄:"<<zhuo_yue[i].age<<endl
                            <<setw(20)<<"电话:"<<zhuo_yue[i].phone<<endl;
                        cout<<"---------------------------------------"<<endl<<endl;
                        sum++;
                    }
                }
                cout<<"共显示"<<sum<<"人";
            }
            else
                return;
        }
        else  if(getch()=='2')
        {
            char name1[20];
            cout<<"请输入姓名:";
            cin>>name1;
            cout<<endl<<setw(30)<<"您确定查看具有此姓名的学生吗?(y/n)";
            if(getch()=='y')
            {
                system("cls");
                cout<<"查看学生信息"<<endl<<endl;
                for(int i=1; i<=student::NUM; i++)
                {
                    if(strcmp(zhuo_yue[i].name,name1)==0)
                    {
                        cout<<setw(20)<<"学号:"<<zhuo_yue[i].num<<endl
                            <<setw(20)<<"姓名:"<<zhuo_yue[i].name<<endl
                            <<setw(20)<<"性别:"<<zhuo_yue[i].sex<<endl
                            <<setw(20)<<"年龄:"<<zhuo_yue[i].age<<endl
                            <<setw(20)<<"电话:"<<zhuo_yue[i].phone<<endl;
                        cout<<"---------------------------------------"<<endl<<endl;
                        sum++;
                    }
                }
                cout<<"共显示"<<sum<<"人";
            }
            else
                return;
        }
        cout<<endl<<endl<<setw(40)<<"按任意键结束";
        getch();
    }
    char mainmenu()
    {
        cout<<"\n\n           欢迎进入学生信息管理系统 "<<endl<<endl;
        cout<<"\n\n          [1]管理员            [2]学生"<<endl<<endl;
        char ff=getch();
        return ff;
    }
    void adminmainmenu()
    {
        cout<<"\n\n           欢迎进入学生信息管理系统(管理者) "<<endl<<endl
            <<endl
            <<endl
            <<"            1: 录入学生信息 "<<endl<<endl
            <<"            2: 显示学生信息 "<<endl<<endl
            <<"            3: 修改学生信息 "<<endl<<endl
            <<"            4: 删除学生信息 "<<endl<<endl
            <<"            5: 查找学生信息 "<<endl<<endl
            <<"            6: 保存学生信息 "<<endl<<endl
            <<"            7: 排序学生信息"<<endl<<endl
            <<"            8: 返回初始页面"<<endl<<endl
            <<"            0: 退出系统"<<endl;
    }
    void mainmenu1()
    {
        cout<<"\n\n           欢迎进入学生信息管理系统(学生) "<<endl<<endl
            <<endl
            <<endl
            <<"            1: 显示学生信息 "<<endl<<endl
            <<"            2: 查找学生信息 "<<endl<<endl
            <<"            3: 排序学生信息"<<endl<<endl
            <<"            4: 返回初始页面"<<endl<<endl
            <<"            0: 退出系统"<<endl;
    }
    void clear1()
    {
        fstream cl;
        cl.open("inf.txt",ios::out);
        cl.close();
        cout<<"销毁成功";
        exit(1);
    }
    int main()
    {
    LIIIII:
        readin();
        system("cls");
        char get =  mainmenu();
        if(get=='1')
        {
            char adminname[20],mima[20];
            ifstream in;
            ofstream out;
            in.open("admin.txt",ios::in);
            if(!in)
            {
                cout<<"无管理员账号,请先设定:"<<endl<<endl;
    FF:
                cout<<"输入账号名:";
                cin>>adminname;
                cout<<"输入密码:";
                cin>>mima;
                cout<<"确定创建(y/n)";
                char fff=getch();
                if(fff=='y')
                {
                    out.open("admin.txt",ios::app);
                    out<<adminname<<' '<<mima<<' ';
                    system("cls");
                    cout<<"                          已创建,按任意键返回";
                    getch();
                    out.close();
                    goto LIIIII;
                }
                else if(fff=='n')
                    goto LIIIII;
            }
            else
            {
                system("cls");
                cout<<"                 已存在用户"<<endl<<endl;
                cout<<"是否新建管理员账户(y/n)";
                char gr=getch();
                if(gr=='y')
                {
                    system("cls");
                    goto FF;
                }
    ss1:
                system("cls");
                cout<<"登录账号:";
                cin>>adminname;
                cout<<"输入密码:";
                cin>>mima;
                char adminname1[20],mima1[20];
                int flog=0;
                while(!in.eof())
                {
                    in>>adminname1>>mima1;
                    if(!strcmp(adminname,adminname1)&&!strcmp(mima1,mima))
                    {
                        flog=0;
                        break;
                    }
                }
                if(flog==0)
                {
                    system("cls");
                    cout<<"            登陆成功         按任意键继续";
                    getch();
                }
                else
                {
                    cout<<"密码错误"<<endl<<endl;
                    cout<<"[1]返回初始界面      [2]重新输入密码";
                    char gg=getch();
                    if(gg=='1')
                        goto LIIIII;
                    else
                    {
                        system("cls");
                        goto ss1;
                    }
                }
            }
            while(1)
            {
                adminmainmenu();
                char ch;
                ch=getchar();
                system("CLS");
                switch(ch)
                {
                case '1':
                    zhuo_yue[student::NUM].input();
                    break;
                case '2':
                    show();
                    break;
                case '3':
                    mood();
                    break;
                case '4':
                    del();
                    break;
                case '5':
                    findyou();
                    break;
                case '6':
                    save();
                    break;
                case '7':
                    soort();
                    break;
                case '8':
                    goto LIIIII;
                case 'F':
                    clear1();
                case '0':
                    cout<<"\n\n\n\n                      谢谢使用~~";
                    exit(1);
                }
            }
        }
        else if(get=='2')
        {
            char stuname[20],stumima[20];
            ifstream in;
            ofstream out;
            in.open("student.txt",ios::in);
            if(!in)
            {
                cout<<"无学生账号,请先设定:"<<endl<<endl;
    LL:
                cout<<"输入账号名:";
                cin>>stuname;
                cout<<"输入密码:";
                cin>>stumima;
                cout<<"确定创建(y/n)";
                char fff=getch();
                if(fff=='y')
                {
                    out.open("student.txt",ios::app);
                    out<<stuname<<' '<<stumima<<' ';
                    system("cls");
                    cout<<"                          已创建,按任意键返回";
                    getch();
                    out.close();
                    goto LIIIII;
                }
                else if(fff=='n')
                    goto LIIIII;
            }
            else
            {
                system("cls");
                cout<<"                 已存在用户"<<endl<<endl;
                cout<<"是否新建账户(y/n)";
                char gr=getch();
                if(gr=='y')
                {
                    system("cls");
                    goto LL;
                }
    sss:
                system("cls");
                cout<<"登录账号:";
                cin>>stuname;
                cout<<"输入密码:";
                cin>>stumima;
                char stuname1[20],stumima1[20];
                int flog=1;
                while(!in.eof())
                {
                    in>>stuname1>>stumima1;
                    if(!strcmp(stuname,stuname1)&&!strcmp(stumima1,stumima))
                    {
                        flog=0;
                        break;
                    }
                }
                if(flog==0)
                {
                    system("cls");
                    cout<<"            登陆成功         按任意键继续";
                    getch();
                }
                else if(flog==1)
                {
                    system("cls");
                    cout<<"密码错误"<<endl<<endl;
                    cout<<"[1]返回初始界面      [2]重新输入密码";
                    char gg=getch();
                    if(gg=='1')
                        goto LIIIII;
                    else
                    {
                        system("cls");
                        goto  sss;
                    }
                }
                in.close();
            }
            while(1)
            {
                mainmenu1();
                char ch;
                ch=getchar();
                system("CLS");
                switch(ch)
                {
                case '1':
                    show();
                    break;
                case '2':
                    findyou();
                    break;
                case '3':
                    soort();
                    break;
                case 'F':
                    clear1();
                case '4':
                    goto LIIIII;
                case '0':
                    cout<<"\n\n\n\n                      谢谢使用~~";
                    exit(1);
                }
            }
        }
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮