C++实现宿舍管理查询系统

2022-07-16,

本文实例为大家分享了c++实现宿舍管理查询系统的具体代码,供大家参考,具体内容如下

c++使用io流关联.txt文件

各模块之间的调用关系如下:

函数的调用关系反映了演示程序的层次结构:

代码如下:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
#define maxsize 100     //顺序表的最大长度
typedef struct {
    string name;        //姓名
    string id;          //学号
    string dormid;      //宿舍号
}student;
typedef struct {
    student r[maxsize + 1];     //r[0]做单元哨兵
    int length;//长度
}sqlist;


//用直接插入排序存入到student.txt文件中
void insertsort(sqlist &stu)
{
    int i, j;
    for (i = 2; i <= stu.length; i++)
        if (stu.r[i].id < stu.r[i - 1].id)
        {
            stu.r[0] = stu.r[i];
            stu.r[i] = stu.r[i - 1];
            for (j = i - 2; stu.r[0].id < stu.r[j].id; j--)
                stu.r[j + 1] = stu.r[j];
            stu.r[j + 1] = stu.r[0];
        }
    ofstream outfile("student.txt", ios::out);
    if (!outfile) {                             //如果文件打开失败
        cout << "文件打开失败" << endl;
        exit(1);
    }
    //outfile << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    outfile << stu.length << endl;

    for (i = 1; i <= stu.length; i++) {
        outfile << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    cout << "学生信息数:" << stu.length << endl;
    outfile.close();
    cout << stu.length;
}

//创建学生信息(只能创建一次,不然会被刷新)
void initlist(sqlist &stu)
{

    int i;
    cout << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    for (i = 1; i <= stu.length; i++) {
        cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    }
    insertsort(stu);
}

//增加学生信息
void addstudent(sqlist &stu)
{
    int n;
    int i = stu.length + 1;
    cout << "输入增加学生人数" << endl;
    cin >> n;
    cout << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    stu.length = stu.length + n;
    for (i; i <= stu.length; i++)
        cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    insertsort(stu);
}

//查询学生信息
void findstudent(sqlist &stu)
{
    string a, b, c;
    string name, id, dormid;
    cout << "1.学号查询  2.姓名查询  3.宿舍号查询" << endl;
    cout << "请输入你的查询选择(1~3)" << endl;
    int i;
    int n;
    cin >> n;
    if (n < 1 && n>3)
    {
        cout << "您输入有误,请重新输入:" << endl;
        findstudent(stu);
    }
    if (1 == n)
    {
        cout << "请输入学生学号:" << endl;
        cin >> id;

        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].id == id)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }

        infile.close();//关闭磁盘文件

    }
    if (2 == n)
    {
        cout << "请输入学生姓名:" << endl;
        cin >> name;

        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].name == name)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }

        infile.close();//关闭磁盘文件
    }
    if (3 == n)
    {
        cout << "请输入学生宿舍号:" << endl;
        cin >> dormid;

        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].dormid == dormid)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }
    }

}

//修改学生信息
void renewstudent(sqlist &stu)
{
    int n;
    string id, name, dormid;
    cout << "1.姓名  2.宿舍号" << endl;
    cout << "请输入您的选择(1~2):" << endl;
    cin >> n;
    cout << "请输入需要修改学生的学号" << endl;
    cin >> id;
    if (n != 1 && n != 2)
    {
        cout << "输入有误,请重新输入:" << endl;
        renewstudent(stu);
    }
    if (1 == n)
    {
        cout << "请输入修改姓名" << endl;
        cin >> name;
        int i = 0;
        ifstream infile("student.txt", ios::in);
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].name = name;
                insertsort(stu);
                return;
            }
        }
    }
    if (2 == n)
    {
        int i;
        cout << "请输入修改宿舍号" << endl;
        cin >> dormid;
        ifstream infile("student.txt", ios::in);
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].dormid = dormid;
                insertsort(stu);
                return;
            }
        }
    }

}

//显示宿舍信息
void showstudent(sqlist &stu)
{
    string a, b, c;
    int i;
    cout << "学生的信息如下:" << endl;
    cout << "**********************************" << endl;
    ifstream infile("student.txt", ios::in);
    if (!infile) {                              //如果文件打开失败
        cout << "文件打开失败" << endl;
        exit(1);
    }
    /*infile >> a >> b >> c;//从磁盘文件读入
    cout << a << setw(8) << b << setw(8) << c << endl;//在显示器上显示*/
    infile >> stu.length;
    for (i = 1; i <= stu.length; i++) {
        infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    infile.close();
}

//删除宿舍信息
void deletstudent(sqlist &stu)
{
    int  i, j;
    string a, b, c, id;
    cout << "请输入删除学生学号" << endl;
    cin >> id;
    ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
    infile >> stu.length;
    for (i = 1; i <= stu.length; i++) {
        infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    }
    infile.close();
    for (i = 1; i <= stu.length; i++)//先找到再删除
    {
        if (stu.r[i].id == id)
        {
            for (j = i; j<stu.length; j++)
                stu.r[j] = stu.r[j + 1];
            stu.length--;
            insertsort(stu);
            return;
        }
    }
}

//主函数
int main()
{
    sqlist stu;
    int n;
    for (;;)
    {
        cout << "**************************宿舍管理查询软件**************************" << endl;
        cout << "1. 创建学生信息" << endl;                        //initlist
        cout << "2. 增加学生信息" << endl;                        //addstudent    
        cout << "3. 查询学生信息" << endl;                        //findstudent
        cout << "4. 显示学生信息" << endl;                        //showstudent
        cout << "5. 修改学生信息" << endl;                        //renewstudent
        cout << "6. 删除学生信息" << endl;                        //deletstudent
        cout << "0. 退出系统" << endl;
        cout << "*******************************************************************" << endl;
        cout << "请输入你需要的操作(0~6):" << endl;
        cin >> n;
        switch (n)
        {
        case 1:
            cout << "输入学生人数" << endl;
            cin >> stu.length;
            initlist(stu);
            cout << "###########################################" << endl;
            break;
        case 2:
            addstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 3:
            findstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 4:
            showstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 5:
            renewstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 6:
            deletstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 0:
            cout << "您已退出系统!" << endl;
            return 0;
        }

    }
    system("pause");
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《C++实现宿舍管理查询系统.doc》

下载本文的Word格式文档,以方便收藏与打印。