宿舍管理查询系统
想在查询前增加排序功能(排序方式不限)#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
#include<Windows.h>
using namespace std;
int i=0;
class Student
{private:
string number[100];
string name[100];
string address[100];
public:
void add()
{
cout<<"***************新增学生信息****************"<<endl;//添加学生信息
cout<<"输入姓名"<<endl;
cin>>name[i];
cout<<"输入学号"<<endl;
cin>>number[i];
cout<<"输入房号"<<endl;
cin>>address[i];
save();
cout<<"******添加成功!******"<<endl;
i++;
cout<<"是否继续添加(Y/N)?"<<endl;
char c;
cin>>c;
if(toupper(c)=='Y')
{
add();
}
else
{
show();
return;
}
}
void del()//删除
{
cout<<"请输入要删除的姓名"<<endl;
string na;
cin>>na;
for(int j=0; j < i; j++)
{
string *a = &name[j];
if(*a == na)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
break;
}
else
{
cout<<"没有该学生的信息!"<<endl;
show();
return;
}
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout<<"请确认是否删除(Y/N)?"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY
| FOREGROUND_GREEN);
char a;
cin>>a;
if(toupper(a)=='Y')
{
for(int j=0;j<i;j++)
{
string *p=&name[j];
if(na == *p)
{
name[j] = name[i - 1];
number[j] = number [i - 1];
address[j]=address[i-1];
i--;
}
}
cout<<"***********已删除!!!************"<<endl;
cout<<"是否继续删除(Y/N)?"<<endl;
char c;
cin>>c;
if(toupper(c)=='Y')
{
del();
return;
}
else
{
show();
return;
}
}
else
{
show();
return;
}
}
void modif(int j)
{
cout<<"输入修改后姓名"<<endl;
cin>>name[j];
cout<<"输入修改后学号"<<endl;
cin>>number[j];
cout<<"输入修改后房号"<<endl;
cin>>address[j];
cout << "******修改完成!******" << endl;
show();
return;
}
void modify()
{
string na;
cout<<"请输入要修改的姓名"<<endl;
cin >> na;
int j = 0;
for(; j < i; j++)
{
string *c = &name[j];
if(*c == na)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
break;
}
else
{
cout<<"没有该学生的信息!"<<endl;
show();
return;
}
}
modif(j);
return;
}
void find()//查找
{
cout<<" ┏━━━━━━━━━━━━━┓\n";
cout<<" ┃ 查找方式 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃1.按姓名查找 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃2.按学号查找 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃3.按房号查找 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃输入1——3进行选择 ┃\n";
cout<<" ┗━━━━━━━━━━━━━┛\n";
cout<<"请输入:"<<endl;
int n;
do{
cin>>n;
if(n==1||n==2||n==3)
{
switch(n)
{
case 1:find1();break;
case 2:find2();break;
case 3:find3();break;
}
show();
return;
}
else
cout<<"输入错误,请重新输入"<<endl;
}
while(true);
cout<<"是否继续查找(Y/N)?"<<endl;
char c;
cin>>c;
if(toupper(c)=='Y')
{
find();
}
else
{
show();
return;
}
}
void find1()//按姓名查找
{
cout<<"请输入要查找的姓名"<<endl;
string na;
cin>>na;
bool test = false;
for(int j=0;j<i;j++)
{
string* p = &name[j];
if(*p==na)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
cout<<"******查找成功******"<<endl;
test = true;
};
}
if(test == false)
{
cout << "未找到,请重新输入要查找的姓名" << endl;
find1();
}
}
void find2()//按学号查找
{
cout<<"请输入要查找的学号"<<endl;
//output();
string num;
cin>>num;
bool test = false;
for(int j=0;j<i;j++)
{
string *p=&number[j];
if(*p==num)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
cout<<"******查找成功******"<<endl;
test = true;
};
}
if(test == false)
{
cout << "未找到,请重新输入要查找的学号" << endl;
find2();
}
}
void find3()
{
cout<<"请输入要查找的房号"<<endl;
string add;
cin>>add;
bool test = false;
for(int j=0;j<i;j++)
{
string* p = &address[j];
if(*p==add)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
cout<<"******查找成功******"<<endl;
test = true;
};
}
if(test == false)
{
cout << "未找到,请重新输入要查找的房号" << endl;
find3();
}
}
void look()
{
for(int j=0;j<i;j++)
{
cout<<"姓名:"<<name[j]<<endl;
cout<<"学号:"<<number[j]<<endl;
cout<<"房号:"<<address[j]<<endl;
}
cout<<"预览成功!"<<endl;
cout<<" ┏━━━━━━━━━━━━━┓\n";
cout<<" ┃1.添 加 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃2.删 除 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃3.修 改 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃4.清空管理软件 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃5.返回主菜单 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃输入1——5进行选择 ┃\n";
cout<<" ┗━━━━━━━━━━━━━┛\n";
cout<<"请输入:"<<endl;
do
{
int choice;
cin>>choice;
if(choice==1||choice==2||choice==3||choice==4||choice==5)
{
switch(choice)
{
case 1:add();break;
case 2:del();break;
case 3:modify();break;
case 4:close();break;
case 5:show();
return;
}
}
else
cout<<"输入错误,请重新输入!!!"<<endl;
}
while(true);
}
void save()
{
ofstream student("student.txt",ios::out);
student<<"学号:"<<number[i]<<endl;
student<<"姓名:"<<name[i]<<endl;
student<<"房号:"<<address[i]<<endl;
//i++;
}
void close()//清空
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY |
FOREGROUND_RED);
cout<<"请确认是否全部清除(Y/N)?"<<endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY |
FOREGROUND_GREEN);
char c;
cin>>c;
if(toupper(c)=='Y')
{
i=0;
system("cls");//清屏
cout<<"***********已清除管理软件!!!************"<<endl;
show();
return;
}
else
{
show();
return;
}
}
void show()
{
cout<<" ┏━━━━━━━━━━━━━┓\n";
cout<<" ┃ 宿舍管理查询 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃1.新 增 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃2.删 除 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃3.修 改 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃4.查 询 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃5.预 览 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃6.清 空 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃7.退 出 ┃\n";
cout<<" ┃━━━━━━━━━━━━━┃\n";
cout<<" ┃输入1——7进行选择 ┃\n";
cout<<" ┗━━━━━━━━━━━━━┛\n";
cout<<"请输入:"<<endl;
}
};
int main()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY |
FOREGROUND_GREEN);
int choice;
string name;
Student s;
s.show();
do{
cin>>choice;
if(choice==1||choice==2||choice==3||choice==4||choice==5||choice==6||choice==7)
{
switch(choice){
case 1:s.add();break;
case 2:s.del();break;
case 3:s.modify();break;
case 4:s.find();break;
case 5:s.look();break;
case 6:s.close();break;
case 7:cout<<"***退出程序***"<<endl;
return 0;
}}
else
{cout<<"输入错误,请重新输入"<<endl;}
}
while(true);
}