C++课设案例 学生信息管理系统

#include <iostream>
#include <string>
#include <windows.h>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <vector>
#include <cstdio>


#define FILENAME "stuInfo.bat"

using namespace std;

/* 账号类
class Account
{
    private:
        string account;  // 账号
        string pwd; // 密码
    public:
        Account()
        {
            account = "yc";
            pwd = "123";
        }

        ~Account(); // 析构函数
        Account(string newAccount, string newPwd);
        void show();
        int login(string account, string pwd);  // 函数的声明
};

Account::Account(string newAccount, string newPwd){
    account = newAccount;
    pwd = newPwd;
}

void inline Account::show() // 显式声明的内联函数
{
    cout << account << "\t" << pwd << endl;
}
int Account::login(string account, string pwd)  // 成员函数的实现
{
}
*/

// 学生类
class StuInfo
{
    private:
        string sno;  // 学号
        string sname; // 姓名
        string cname; // 班级
        string sex; // 性别
        int age; // 年龄
        string tel; // 电话号码

    public:
        /*StuInfo(string sno, string sname, string cname, string sex , int age, string tel)
        {
            this -> sno = sno;
            this -> sname = sname;
            this -> cname = cname;
            this -> sex = sex;
            this -> age = age;
            this -> tel = tel;
        }*/

        StuInfo(string sno, string sname, string cname, string sex , int age, string tel):sno(sno),sname(sname),cname(cname),sex(sex),age(age),tel(tel)
        {

        }

        StuInfo()
        {

        }

        void setSno(string sno) {
            this -> sno = sno;
        }

        void setSname(string sname) {
            this -> sname = sname;
        }

        void setCname(string cname) {
            this -> cname = cname;
        }

        void setSex(string sex) {
            this -> sex =  sex;
        }

        void setAge(int age) {
            this -> age =  age;
        }

        void setTel(string tel) {
            this -> tel = tel;
        }

        string getSno() {
            return sno;
        }

        string getSname() {
            return sname;
        }

        string getCname() {
            return cname;
        }

        string getSex() {
            return sex;
        }

        int getAge() {
            return age;
        }

        string getTel() {
           return tel;
        }

        void show()
        {
            cout << "\n\t" << sno << "\t" << sname << "\t" << cname << "\t" << sex << "\t" << age << "\t" << tel << endl;
        }

        bool save()  // 保存到数据文件
        {
            ofstream out(FILENAME, ios::out | ios::app | ios::binary);
            if (out.is_open())
            {
                out << sno << "\t" << sname << "\t" << cname << "\t" << sex << "\t" << age << "\t" << tel << "\r\n";
                out.close();
                return true;
            }
            return false;
        }
};

class Controller
{
    public:
        void showCopy(); // 显示版权
        bool login(); // 用户登录
        void menu(); // 主菜单
        void initData(); // 初始化数据
        void view(); // 显示学生信息
        void addStuInfo(); // 添加学生信息
        void search(); // 查询学生信息
        void update(); // 修改学生信息
        bool rewrite(); // 重写数据的方法
        void remove(); // 删除学生信息
        void showStuInfo(StuInfo stu); // 显示单个学生信息
        void showStuInfos(StuInfo stus[]); // 显示多个学生信息
};

//显示版权信息
void Controller::showCopy()
{
    cout << "\n\n\t**************************欢迎使用XXXX系统**************************";
    cout << "\n\n\t**************************源辰信息版权所有**************************";
}

bool Controller::login()  // 成员函数的实现
{
    system("cls");
    int index = 0, count = 0;
    string account;
    char pwd[20];
    char ch;
    showCopy();

    do{
        index = 0;
        count ++;
        cout << "\n\n\t请输入您的账号:";
        //获取用户输入的账号
        cin >> account;

        cout << "\n\t请输入您的密码:";

        //获取用户输入的密码
        while( (ch = getch()) != 13 ){ //回车键的键值      //密码只能是数字和字母????
            if( ch == 8){ //Backspace  回退
                if(index<=0){
                    index=0;
                }else{
                    cout << "\b \b";
                    index--;
                }
            }else{
                pwd[index++] = ch;
                cout << '*';
            }
        }

        pwd[index]='\0'; //数组中数据结束的标志

        //判断用户输入的账号和密码是否正确
        if( account == "yc" && strcmp(pwd,"123") == 0){
           return true;
        }
        if(count==3){
            cout << "\n\n\t对不起,您暂无权限,系统将自动退出....\n\t";
            exit(0);
        }
        cout << "\n\n\t账号或密码错误,请确认后重新输入<您还有 " << (3-count) << " 输入机会,回车后继续>!!!" << endl;
    }while(count > 0);

    return false;

}

vector<StuInfo> stuInfos;

/*
    初始化数据
*/
void Controller::initData()
{
    fstream file(FILENAME, ios::in | ios::binary);
    if (file.is_open()) { // 打开成功
        string line;
        string arr[6]; // 定义字符串数组,长度为6
        int index = -1, count = 0, num = 0;
        while(getline(file, line)) // 每次读取一行
        {
            num = 0;
            count = 0;
            index = line.find("\t",count);
            while( index > 0) {
                arr[num++] = line.substr(count,index-count);
                count = index + 1;
                index = line.find("\t",count);
            }
            arr[num] = line.substr(count);

            //atoi 将字符串转换成整形。ASCII to Integer的缩写
            stuInfos.push_back(StuInfo(arr[0], arr[1], arr[2], arr[3], atoi(arr[4].c_str()), arr[5]));
        }
        file.close();
    }
}

void Controller::view()
{
    system("cls");
    showCopy();
    cout << "\n\n\t**************************  浏览学生信息  **************************" << endl;

    if (stuInfos.size() <= 0)
    {
        cout << "\n\t暂无学生信息...";
        return;
    }

    cout << "\n\n\t************************** 共有 " << stuInfos.size() << " 个学生 **************************" << endl;
    cout << "\n\n\t学号" << "\t 姓名" << "\t   班级" << "\t        性别" << "\t年龄" << "\t  电话" << endl;
    for(int i=0,len=stuInfos.size(); i<len; i++)
    {
        stuInfos[i].show();
    }

    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

void Controller::showStuInfo(StuInfo stu)
{
    cout << "\n\n\t学号" << "\t 姓名" << "\t   班级" << "\t        性别" << "\t年龄" << "\t  电话" << endl;
    stu.show();
}

// 查询
void Controller::search()
{
    system("cls");

    showCopy();
    cout << "\n\n\t**************************  查询学生信息  **************************";
    if (stuInfos.size() <= 0)
    {
        cout << "\n\t暂无学生信息...";
        return;
    }

    string sname; //存放用户要查找的商品编号
    cout << "\n\n\t请输入您要查询的学生姓名:";
    cin >> sname;
    cout << "\n\n\t学号" << "\t 姓名" << "\t   班级" << "\t        性别" << "\t年龄" << "\t  电话" << endl;

    int count = 0; // 记录满足条件的学生个数
    int temp = 0, j=0;
    char arr[20];

    for(int i=0,len=stuInfos.size(); i<len; i++)  // 循环所有学生信息
    {
        if (sname == stuInfos[i].getSname()) {
            count++;
            stuInfos[i].show();
        }
    }

    if (count>0)
    {
        cout << "\n\n\t************************ 共有 " << count << " 个基本满足条件 ************************" << endl;
    }
    else
    {
        cout << "\n\t没有您要查找的学生信息...";
    }
    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

bool Controller::rewrite()
{
    int len=stuInfos.size();
    if (len <=0) // 说明没有学生信息,则清空文件中的内容
    {
        ofstream file(FILENAME, ios::out | ios::trunc | ios::binary);
        file.close();
    }
    else
    {
        ofstream out(FILENAME, ios::out | ios::binary);
        if (out.is_open())
        {
            for(int i=0,len=stuInfos.size(); i<len; i++)
            {
                out << stuInfos[i].getSno() << "\t" << stuInfos[i].getSname() << "\t" << stuInfos[i].getCname() << "\t" << stuInfos[i].getSex() << "\t" << stuInfos[i].getAge() << "\t" << stuInfos[i].getTel() << "\r\n";
            }
            out.close();
            return true;
        }

    }
    return false;
}


void Controller::remove()
{
    system("cls");

    showCopy();
    cout << "\n\n\t**************************  删除学生信息  **************************";

    if (stuInfos.empty())
    {
        cout << "\n\t暂无学生信息...";
        return;
    }

    string sno; //存放用户要查找的商品编号
    cout << "\n\n\t请输入您要删除的学生学号:";
    cin >> sno;

    int i, len = stuInfos.size();

    for(i=0; i<len; i++)
    {
        if (sno==stuInfos[i].getSno())
        {
            showStuInfo(stuInfos[i]);
            break;
        }
    }

    if (i>=len)
    {
        cout << "\n\t对不起!没有您要删除的学生信息...";
        return;
    }

    int result=MessageBox(NULL,"您确定要删除此学生信息吗?","确认提示",MB_YESNO|MB_ICONWARNING);
    if(result==6) // 说明确定
    {
        stuInfos.erase(stuInfos.begin()+i);
        // 重写数据文件
        if(rewrite())
        {
           cout << "\n\n\t学生信息删除成功..." << endl;
        }
        else
        {
            cout << "\n\n\t学生信息删除失败..." << endl;
        }
    }
    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

void Controller::update()
{
    system("cls");

    showCopy();
    cout << "\n\n\t**************************  修改学生信息  **************************";
    if (stuInfos.empty())
    {
        cout << "\n\t暂无学生信息...";
        return;
    }

    string sno;  // 学号
    cout << "\n\n\t请输入您要修改的学生学号:";
    cin >> sno;

    int i, len = stuInfos.size();

    for(i=0; i<len; i++)
    {
        if (sno==stuInfos[i].getSno())
        {
            cout << "\n\t您要修改的学生信息如下:";
            showStuInfo(stuInfos[i]);
            break;
        }
    }

    if ( i >= len )
    {
        cout << "\n\t对不起!没有您要删除的学生信息...";
        return;
    }

    string sname; // 姓名
    string cname; // 班级
    string sex; // 性别
    int age; // 年龄
    string tel; // 电话号码

    cout << "\n\t请输入学生姓名:";
    cin >> sname;

    cout << "\n\t请输入所在班级:";
    cin >> cname;

    cout << "\n\t请输入学生性别:";
    cin >> sex;

    cout << "\n\t请输入学生年龄:";
    cin >> age;

    cout << "\n\t请输入联系方式:";
    cin >> tel;

    int result=MessageBox(NULL,"您确定要修改此学生信息吗?","确认提示",MB_YESNO|MB_ICONWARNING);
    if(result==6) // 说明确定
    {
        stuInfos[i].setSname(sname);
        stuInfos[i].setCname(cname);
        stuInfos[i].setSex(sex);
        stuInfos[i].setAge(age);
        stuInfos[i].setTel(tel);

        // 重写数据文件
        if(rewrite())
        {
           cout << "\n\n\t学生信息修改成功..." << endl;
        }
        else
        {
            cout << "\n\n\t学生信息修改失败..." << endl;
        }
    }
    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

void Controller::menu()
{
    int option=0, count=0;

again1:system("cls"); //清屏
    count=0;
    showCopy();
    cout << "\n\n\t**************************     主菜单     **************************\n";
    cout << "\n\t************************** 1.浏览学生信息 **************************";
    cout << "\n\t************************** 2.查询学生信息 **************************";
    cout << "\n\t************************** 3.添加学生信息 **************************";
    cout << "\n\t************************** 4.修改学生信息 **************************";
    cout << "\n\t************************** 5.删除学生信息 **************************";
    cout << "\n\t************************** 6.退出系统     **************************";

    do{
        if(count!=0){ //说明用户不是第一次输入,则提醒用户
            cout << "\n\n\t对不起,没有该选项,请重新选择(1-6):";
        }else{
            cout << "\n\n\t请选择您的操作(1-6):";
        }

        //获取用户的选择
        cin >> option;
        count++;
    }while(option<=0 || option>6);

    //如果用户输入了正确的值
    switch(option){
        case 1:view();break;
        case 2:search();break;
        case 3:addStuInfo();break;
        case 4:update();break;
        case 5:remove();break;
        case 6:exit(0);break;
    }
    goto again1;
}

void Controller::addStuInfo()
{
    system("cls");
    int result, age;
    string sno, sname, cname, sex, tel;

    cout << "\n\n\t**************************  添加学生信息  **************************";
    cout << "\n\n\t*************************请输入以下学生信息**************************\n";
    cout << "\n\t请输入学生学号:";
    cin >> sno;

    cout << "\n\t请输入学生姓名:";
    cin >> sname;

    cout << "\n\t请输入所在班级:";
    cin >> cname;

    cout << "\n\t请输入学生性别:";
    cin >> sex;

    cout << "\n\t请输入学生年龄:";
    cin >> age;

    cout << "\n\t请输入联系方式:";
    cin >> tel;

    StuInfo stu = StuInfo(sno, sname, cname, sex, age, tel);
    cout << "\n\t您要添加的学生信息如下,请确认:" << endl;

    showStuInfo(stu);

    result=MessageBox(NULL,"您确定要添加此数据吗?","确认提示",MB_YESNO|MB_ICONWARNING);
    if(result==6)
    {
        if ( stu.save() ) {
             stuInfos.push_back(stu);
            cout << "\n\t学生信息添加成功..." << endl;
        } else {
            cout << "\n\t学生信息添加失败..." << endl;
        }
    }
    cout << "\n\n\t<请按回车返回>" << endl;
    getch();
}

int main()
{

    Controller controll;
    if (controll.login() )
    {
        controll.initData(); // 初始化数据
        controll.menu();
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qfxsxhfy/article/details/81876169