宿舍管理查询软件

任务:为宿舍管理人员编写一个宿舍管理查询软件, 程序设计要求:

(1)采用交互工作方式

(2)可以增加、删除、修改信息

(3)建立数据文件 ,数据文件按关键字(姓名、学号、房号)进行排序(选择、快速排序、堆排序等任选一种)

(4)查询  : a.按姓名查询 ;b.按学号查询 ;c按房号查询

(5)打印任一查询结果(可以连续操作)

#include <iostream>
#include<string>
#include<fstream>
#include<ostream>
#include<stdlib.h>
using namespace std;
#define Max 200
typedef struct
{
    int no;
    string name;
    int room;
}student;
student stu[Max];
int n=0;
void creat()
{
    for(int i=0;i<n;i++)
    {
        cout<<"输入第"<<i+1<<"位同学学号:";
        cin>>stu[i].no;
        cout<<"输入第"<<i+1<<"位同学姓名:";
        cin>>stu[i].name;
        cout<<"输入第"<<i+1<<"位同学房号:";
        cin>>stu[i].room;
        cout<<"************************"<<endl;
    }
    cout<<"创建完毕!"<<endl;
}
void read()
{
    ifstream ofile;
    int no1;
    string name1;
    int room1;
    ofile.open("student.txt",ios::in);
    if(ofile.is_open())
    {
        for(int i=0;i<n;i++)
        {
            ofile>>no1>>name1>>room1;
            stu[i].no=no1;
            stu[i].name=name1;
            stu[i].room=room1;
        }
    }
    ofile.close();
}
void write()
{
    ofstream ifile("student.txt");
    for(int i=0;i<n;i++)
    {
         ifile<<stu[i].no<<" ";
         ifile<<stu[i].name<<" ";
         ifile<<stu[i].room<<endl;
    }
}
void add()
{
    n++;
    student stu1;
    cout<<"输入新增同学学号:";
    cin>>stu1.no;
    cout<<"输入新增同学姓名:";
    cin>>stu1.name;
    cout<<"输入新增同学房号:";
    cin>>stu1.room;
    stu[n-1]=stu1;
}
void del()
{
    int no1;
    cout<<"请输入所删除学生编号" ;
    cin>>no1;
    for(int i=0;i<n;i++)
    {
        if(no1==stu[i].no)
        {
            for(int k=i;k<n;k++)
             stu[k]=stu[k+1];
            break;
        }
    }
    n--;
}
void change()
{
    int no1,i;
    cout<<"请输入所修改学生编号" ;
    cin>>no1;
    for( i=0;i<n;i++)
    {
        if(no1==stu[i].no)
        {
            break;
        }
    }
    int cho;
    string name1;
    int room1;
    cout<<"1.修改姓名"<<endl;
    cout<<"2.修改房号"<<endl;
    cout<<"输入你的选择:";
    cin>>cho;
    if(cho==1)
    {
        cout<<"请输入修改姓名:";
        cin>>name1;
        stu[i].name=name1;
    }
    if(cho==2)
    {
        cout<<"请输入修改姓名:";
        cin>>room1;
        stu[i].room=room1;
    }
}
void sort()
{
    student t;
    int i,j,k;
    for( i=0;i<n-1;i++)
    {
       k=i;
       for(j=i+1;j<n;j++)
        if(stu[j].no<stu[k].no)
        k=j;
       if(k!=i)
       {
           t=stu[i];
           stu[i]=stu[k];
           stu[k]=t;
       }
    }
}
void search()
{
    int cho;
    string name1;
    int no1;
    int room1;
    bool flag;
    cout<<"1.按学号查询"<<endl;
    cout<<"2.按姓名查询"<<endl;
    cout<<"3.按房号查询"<<endl;
    cout<<"输入你的选择:";
    cin>>cho;
    if(cho==1)
    {
        flag=false;
        sort();
        cout<<"输入查询学号:";
        cin>>no1;
        int low=0,high=n-1,mid;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(no1==stu[mid].no)
            {
                cout<<"姓名:"<<stu[mid].name<<endl;
                cout<<"学号:"<<stu[mid].no<<endl;
                cout<<"房号:"<<stu[mid].room<<endl;
                flag=true;
                break;
            }
            else if(no1<stu[mid].no)
                high=mid-1;
            else
                low=mid+1;
        }
        if(!flag)
        {
            cout<<"查无此人"<<endl;
        }
    }
    if(cho==2)
    {
        flag=false;
        cout<<"输入查询姓名:";
        cin>>name1;
        for(int i=0;i<n;i++)
        {
            if(name1==stu[i].name)
            {
                cout<<"姓名:"<<stu[i].name<<endl;
                cout<<"学号:"<<stu[i].no<<endl;
                cout<<"房号:"<<stu[i].room<<endl;
                flag=true;
                break;
            }
        }
        if(!flag)
        {
            cout<<"查无此人"<<endl;
        }
    }
    if(cho==3)
    {
        flag=false;
        cout<<"输入查询房号:";
        cin>>room1;
        for(int i=0;i<n;i++)
        {
            if(room1==stu[i].room)
            {
                cout<<"姓名:"<<stu[i].name<<endl;
                cout<<"学号:"<<stu[i].no<<endl;
                cout<<"房号:"<<stu[i].room<<endl;
                flag=true;
                break;
            }
        }
        if(!flag)
        {
            cout<<"查无此人"<<endl;
        }
    }
}
void print()
{
    cout<<"学号***姓名***房号"<<endl;
    for(int i=0;i<n;i++)
    cout<<stu[i].no<<"    "<<stu[i].name<<"    "<<stu[i].room<<endl;
}
void menu()
{
    cout<<"*******欢迎使用宿舍管理系统*******"<<endl;
    cout<<"************1.读取文件************"<<endl;
    cout<<"************2.增加信息************"<<endl;
    cout<<"************3.删除信息************"<<endl;
    cout<<"************4.修改信息************"<<endl;
    cout<<"************5.排序信息************"<<endl;
    cout<<"************6.查询信息************"<<endl;
    cout<<"************7.保存文件************"<<endl;
    cout<<"************0.退出系统************"<<endl;
}
int main()
{
    int len=0;
	string str;
	ifstream file("student.txt");
	while(file)
	{
		getline(file,str);
		if(str.length()>4)
			len++;
	}
    n=len;
    menu();
    int ch;
    do
    {
        cin>>ch;
        switch(ch)
        {
            case 1:read();print();system("pause");system("cls");break;
            case 2:add();print();system("pause");system("cls");break;
            case 3:del();print();system("pause");system("cls");break;
            case 4:change();print();system("pause");system("cls");break;
            case 5:sort();print();system("pause");system("cls");break;
            case 6:search();system("pause");system("cls");break;
            case 7:write();print();system("pause");system("cls");break;
            case 0:cout<<"退出系统,感谢使用!"<<endl;break;
            default:cout<<"输入错误!"<<endl;
        }
        menu();
    }while(ch!=0);

   return 0;
}

 

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
摘 要:本系统是为方便宿舍管理人员实现宿舍管理查询而开发的,具有信息录入、显示、查询、排序、插入和删除功能,能实现信息修改和通过别的途径导入大量数据,但不能实现信息存盘,使用简单方便,点击“宿舍管理查询系统.exe”即可运行。(使用详解见“说明.txt”) 宿舍管理查询软件 1) 任务:为宿舍管理人员编写一个宿舍管理查询软件, 程序设计要求: A. 采用交互工作方式 B. 建立数据文件 ,数据文件按关键字(姓名学号房号进行排序(冒泡、选择、插入排序等任选一种) 2) 查询菜单: (用二分查找实现以下操作) A. 按姓名查询 B. 按学号查询 C. 按房号查询 3) 打印任一查询结果(可以连续操作) 根据上述要求,我们开始考虑系统应具备的功能: (1)要实现交互工作方式,各项操作结束后均应返回主菜单; (2)系统本无任何信息数据,要建立数据文件,需开发一个信息录入功能,即首先创建一个学员线性表,同时我们可以将数据暂时保存在内存中,所以我们未开发信息存盘功能; (3)信息录入后都保存在内存中,用户看不到,需要设计一个信息显示功能,信息的显示应该便于查阅,所以需具备按各种关键字显示的功能; (4)本系统按关键字(姓名学号房号)进行冒泡排序,采用二分查找方式分别实现按关键字(姓名学号房号查询功能; (5)由于有些同学因为不同原因而离校,所以设计了删除功能; (6)由于有新同学入校,所以设计了插入功能; (7)当用户操作完毕需要退出时,我们提供了退出选项,便于使用者退出交互式工作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值