//
#include<iostream>
#include<fstream>
#include<string>
#include<io.h>
#include<vector>
#include"User.h"
#include"Gui.h"
#include"Rooms.h"
using namespace std;
void Show_Login();
User *t=new User;
int main()
{
Show_Login();
while (1)
{
if (t->Permissions == 1)
{
int a = 0;
Show_Admin_GUI();
cin >> a;
switch (a)
{
case 1:
{
Add_Apatment();
}break;
case 2:
{
Add_Dormitory();
}break;
case 3:
{
Modify_Apartment();
}break;
case 4:
{
Modify_Dormitory();
}break;
case 5:
{
Delete_Apartment();
}break;
case 6:
{
Delete_Dormitory();
}break;
case 7:
{
t->Admin_Lookup_Dormitory();
}break;
case 8:
{
exit(0);
}break;
default:
{
cout << "您的输入有误!" << endl << endl;
cout << "滑稽信息管理系统发生错误 错误原因:管理员界面选择错误" << endl << "请重新启动程序!";
abort();;
}
break;
}
}
else
{
Show_User_GUI();
int a = 0;
cin >> a;
switch (a)
{
case 1:
{
t->Show_My_Info();
}break;
case 2:
{
t->Lookup_My_Dormitory();
}break;
case 3:
{
t->Lookup_Dormitory();
}break;
case 4:
{
exit(0);
}break;
default:
{
cout << "您的输入有误!" << endl<<endl;
cout << "滑稽信息管理系统发生错误 错误原因:用户界面选择错误" << endl << "请重新启动程序!";
abort();;
}
break;
}
}
}
}
//登录注册选择界面
void Show_Login()
{
cout << "欢迎使用滑稽公寓信息管理系统" << endl;
cout << "1.注册账户" << endl;
cout << "2.登录账户" << endl<<endl;
cout << "当前系统已注册 人数:"<< GetUserNum() <<" 公寓:"<< GetApartmentNum() <<" 当前信息完善率:" << GetRatio() << endl;
int a = 0;
cin >> a;
if (a != 1 && a != 2)
{
cout << "输入错误!" << endl;
}
switch (a)
{
case 1:
t->Register(*t);
break;
case 2:
t->Login(*t);
break;
default:
{
cout << "您的输入有误!" << endl << endl;
cout << "滑稽信息管理系统发生错误 错误原因:注册登录界面选择错误" << endl << "请重新启动程序!";
abort();;
}
break;
}
}
//需要的头文件
Rooms.h
#pragma once
#include<string>
#include<iostream>
#include<cstdio>
#include"User.h"
using std::cout;
using std::cin;
using std::endl;
//公寓 寝室类
class Room
{
protected:
std::string id_No = "";
std::string Admin_Name;
};
class Apartment :public Room
{
private:
int Student_Total = 0;
std::string use = "";
public:
void Setting(std::string id, std::string admin, std::string s)
{
id_No = id;
Admin_Name = admin;
use = s;
}
void Show()
{
std::cout << id_No << "号公寓楼:" << endl << "公寓性质:" << use << endl << "公寓管理员:" << Admin_Name<<endl<<endl;
}
};
class Dormitory :public Room
{
friend void Add_Dormitory();
friend void Modify_Dormitory();
friend class User;
private:
int Student_Total = 6;
std::string Name[6];
public:
void Setting(int id, std::string admin)
{
id_No = id;
Admin_Name = admin;
}
void Show()
{
std::cout << "寝室号:" << id_No << " 寝室管理员:" << Admin_Name << endl<<endl;;
}
};
void Add_Apatment()
{
std::string name = "",use="";
std::string id = "";
cout << "请输入新增的公寓楼号:" << endl;
cin >> id;
std::string real_id = "Data\\" + id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
std::string temp = "mkdir " + real_id;
std::system(temp.c_str());
}
else
{
cout << "此公寓已存在!" << endl;
cout << "滑稽信息管理系统发生错误 错误原因:用户试图创建已存在的公寓!" << endl << "请重新启动程序!";
abort();
}
cout << "请输入公寓管理员姓名:" << endl;
cin >> name;
cout << "请输入公寓用途:" << endl;
cin >> use;
std::string real_filename = real_id + "info.dat";
std::ofstream out(real_filename, std::ios::binary);
Apartment* ctemp = new Apartment;
ctemp->Setting(id, name, use);
out.write((char*)ctemp, sizeof(Apartment));
out.close();
}
void Show_Apartment_Info()
{
std::string id = "";
cout << "请输入您要查看的公寓号:" << endl;
cin >> id;
std::string real_id = "Data\\" + id + "\\info.dat";
std::ifstream in(real_id, std::ios::binary);
Apartment* ctemp = new Apartment;
in.read((char*)ctemp, sizeof(Apartment));
ctemp->Show();
}
void Modify_Apartment()
{
std::string name = "", use = "";
std::string id = "";
cout << "请输入修改的公寓楼号:" << endl;
cin >> id;
std::string real_id = "Data\\" + id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
cout << "不存在此公寓信息,请您选择新增公寓信息!" << endl;
}
else
{
cout << "请输入新公寓管理员姓名:" << endl;
cin >> name;
cout << "请输入新公寓用途:" << endl;
cin >> use;
std::string real_filename = real_id + "info.dat";
std::ofstream out(real_filename, std::ios::binary | std::ios::trunc);
Apartment* ctemp = new Apartment;
ctemp->Setting(id, name, use);
out.write((char*)ctemp, sizeof(Apartment));
out.close();
}
}
void Add_Dormitory()
{
Dormitory* temp = new Dormitory;
std::string Apartment_Id, Dormitory_Id,Admin_Name;
int Number = 0;
cout << "请输入寝室所在的公寓号:" << endl;
cin >> Apartment_Id;
std::string real_id = "Data\\" + Apartment_Id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
cout << "不存在此公寓信息,请您选择新增公寓信息!" << endl;
}
else
{
cout << "请输入寝室号:" << endl;
cin >> Dormitory_Id;
std::string real_fid = "Data\\" + Apartment_Id + "\\" + Dormitory_Id + ".dat";
State = _access(real_fid.c_str(), 0);
if (State != -1)
{
cout << "已存在此寝室信息!请选择修改寝室信息!" << endl;
cout << "滑稽信息管理系统发生错误 错误原因:用户试图创建已存在的寝室!" << endl << "请重新启动程序!";
abort();
}
cout << "请输入寝室管理员姓名:";
cin >> temp->Admin_Name;
cout << "请输入寝室人数:";
cin >> Number;
if (Number > 6 || Number < 1)
{
cout << "数据输入异常!人员数只能是1到6!";
cout << "滑稽信息管理系统发生错误 错误原因:异常的人员数!" << endl << "请重新启动程序!";
abort();
}
for (int i = 0; i < Number; i++)
{
cout << "请输入第" << i + 1 << "名同学的姓名";
cin >> temp->Name[i];
}
temp->Student_Total = Number;
temp->id_No = Dormitory_Id;
std::ofstream out(real_fid.c_str(), std::ios::binary);
out.write((char*)temp, sizeof(Dormitory));
out.close();
}
}
void Modify_Dormitory()
{
Dormitory* temp = new Dormitory;
std::string Apartment_Id, Dormitory_Id, Admin_Name;
int Number = 0;
cout << "请输入寝室所在的公寓号:" << endl;
cin >> Apartment_Id;
std::string real_id = "Data\\" + Apartment_Id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
cout << "不存在此公寓信息,请您选择新增公寓信息!" << endl;
}
else
{
cout << "请输入寝室号:" << endl;
cin >> Dormitory_Id;
std::string real_fid = "Data\\" + Apartment_Id + "\\" + Dormitory_Id + ".dat";
State = _access(real_fid.c_str(), 0);
if (State == -1)
{
cout << "此寝室信息不存在!请选择新增寝室信息!" << endl;
}
cout << "请输入新寝室管理员姓名:";
cin >> temp->Admin_Name;
cout << "请输入新寝室人数:";
cin >> Number;
if (Number > 6 || Number < 1)
{
cout << "数据输入异常!人员数只能是1到6!";
}
else
{
for (int i = 0; i < Number; i++)
{
cout << "请输入第" << i + 1 << "名同学的姓名";
cin >> temp->Name[i];
}
temp->Student_Total = Number;
std::ofstream out(real_fid.c_str(), std::ios::binary | std::ios::trunc);
out.write((char*)temp, sizeof(Dormitory));
out.close();
}
}
}
void Delete_Apartment()
{
std::string name = "", use = "";
std::string id = "";
cout << "请输入要删除的公寓楼号:" << endl;
cin >> id;
std::string real_id = "Data\\" + id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State!= -1)
{
std::string t = "rmdir /s /q " + real_id;
system(t.c_str());
cout << id << "公寓已被删除!";
}
else
{
cout << "此公寓不存在 无法删除!" << endl;
}
}
void Delete_Dormitory()
{
Dormitory* temp = new Dormitory;
std::string Apartment_Id, Dormitory_Id;
int Number = 0;
cout << "请输入寝室所在的公寓号:" << endl;
cin >> Apartment_Id;
std::string real_id = "Data\\" + Apartment_Id + "\\";
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
cout << "不存在此公寓信息,请您选择输入公寓信息!" << endl;
}
else
{
cout << "请输入寝室号:" << endl;
cin >> Dormitory_Id;
std::string real_fid = "Data\\" + Apartment_Id + "\\" + Dormitory_Id + ".dat";
State = _access(real_fid.c_str(), 0);
if (State != -1)
{
std::remove(real_fid.c_str());
cout << Dormitory_Id << "公寓已被删除!";
}
else
{
cout << "无法删除!因为" << Dormitory_Id << "寝室不存在!" << endl;
}
}
}
Gui.h
#pragma once
#include<iostream>
#include<string>
#include<io.h>
#include<queue>
using std::cout;
using std::cin;
using std::endl;
//Admin界面
void Show_Admin_GUI()
{
cout << endl << "超级管理员功能:" << endl<<endl;
cout << "1.新增公寓信息" << endl;
cout << "2.新增寝室信息" << endl;
cout << "3.修改公寓信息" << endl;
cout << "4.修改寝室信息" << endl;
cout << "5.删除公寓信息" << endl;
cout << "6.删除寝室信息" << endl;
cout << "7.全系统信息查询" << endl;
cout << "8.退出系统" << endl<< endl;
}
void Show_User_GUI()
{
cout << endl << "普通用户功能:" << endl<<endl;
cout << "1.查询我的信息" << endl;
cout << "2.查询同寝室同学信息" << endl;
cout << "3.查询同公寓同学信息" << endl;
cout << "4.退出系统" << endl << endl;
}
int GetUserNum()
{
int fileNum = 0;
std::string inPath = "Userinfo\\";
std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
q.push(item + "\\" + fileinfo.name);
}
else
{
fileNum++;
pathVec.push_back(item + "\\" + fileinfo.name);
}
}
_findclose(handle);
}
return fileNum;
}
int GetApartmentNum()
{
int fileNum = 0;
std::string inPath = "Data\\";
std::vector<std::string> pathVec;
std::queue<std::string> q;
q.push(inPath);
while (!q.empty())
{
std::string item = q.front(); q.pop();
std::string path = item + "\\*";
struct _finddata_t fileinfo;
auto handle = _findfirst(path.c_str(), &fileinfo);
if (handle == -1) continue;
while (!_findnext(handle, &fileinfo))
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)continue;
q.push(item + "\\" + fileinfo.name);
}
else
{
fileNum++;
pathVec.push_back(item + "\\" + fileinfo.name);
}
}
_findclose(handle);
}
return fileNum;
}
std::string GetRatio()
{
if (GetApartmentNum() == 0)
{
return "当前系统尚未录入公寓信息 无法显示";
}
else
{
double Num = double(GetUserNum()) / (double(GetApartmentNum()) * 1800.00);
std::string s = std::to_string(Num * 100) + "%";
return s;
}
}
User.h
#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<io.h>
#include<typeinfo>
#include"Rooms.h"
using std::cout;
using std::cin;
using std::endl;
//用户类
class User
{
friend class Dormitory;
friend std::string Lookup_User(std::string Username);
friend int main();
private:
std::string Password, Username = "";
long long ID_Number=0L;
int Apartment_id=0, Dormitory_id = 0;
int Permissions = 0;
public:
void Login(User& u);
void Register(User& u);
void Show_My_Info();
void Lookup_My_Dormitory();
void Lookup_Dormitory();
void Admin_Lookup_Dormitory();
};
void Check_Catalog()
{
int State = 0;
State = _access("Userinfo\\", 0);
if (State == -1)
{
system("mkdir Userinfo\\");
}
}
//检查此用户是否已经注册
void Check_is_Reg(std::string name)
{
int State = 0;
std::string real_address = "Userinfo\\" + name + ".dat";
State = _access(real_address.c_str(), 0);
if (State != -1)
{
cout<<"此用户已在系统内注册! 如需修改请联系系统管理员!"<<endl<<endl;
cout << "滑稽信息管理系统发生错误 错误原因:已注册过的用户名" << endl << "请重新启动程序!";
abort();
}
}
void User::Register(User& u)
{
std::string name, fname, password = "";
int permissions = 0;
long long id_number = 0;
int a_id, d_id = 0;
std::cout << "请输入姓名:" << std::endl;
std::cin >> name;
Check_is_Reg(name);
std::cout << "请输入密码(当前版本无法修改密码):" << std::endl;
std::cin >> password;
std::cout << "请输入学/工号:" << std::endl;
std::cin >> id_number;
std::cout << "请输入公寓楼号:" << std::endl;
std::cin >> a_id;
std::cout << "请输入寝室号:" << std::endl;
std::cin >> d_id;
std::cout << "请输入用户类型对应的数字:" << std::endl << "1.管理员 2.普通用户" << std::endl;
std::cin >> permissions;
if (permissions == 0)
{
cout << "滑稽信息管理系统发生错误 错误原因:错误的数据输入"<<endl<<"请重新启动程序!";
abort();
}
if (permissions != 1 && permissions != 2)
{
std::cout << "类型输入错误!" << std::endl;
std::abort();
}
Check_Catalog();
fname = "./Userinfo/" + name + ".dat";
std::ofstream username;
username.open(fname, std::ios::binary);
if (!username)
{
std::cout << "注册失败!文件无法写入!" << std::endl;
abort();
}
else
{
Password = password;
Username = name;
Permissions = permissions;
Apartment_id = a_id;
Dormitory_id = d_id;
ID_Number = id_number;
username.write((char*)&u, sizeof(User));
username.close();
std::cout << "注册成功!" << std::endl << "你的用户名是:" << name << std::endl;
std::cout << "学/工号:" << id_number << std::endl << "你的寝室是:" << a_id << "#" << d_id << std::endl;
}
}
void User::Login(User& u)
{
std::string name = "";
std::string fname = "";
std::string password="";
std::cout << "请输入用户名:" << std::endl;
std::cin >> name;
fname = "./Userinfo/" + name + ".dat";
std::ifstream username;
username.open(fname, std::ios::binary || std::ios::_Nocreate);
if (!username)
{
std::cout << "不存在此账户!" << std::endl;
cout << "滑稽信息管理系统发生错误 错误原因:错误的用户名登录!" << endl << "请重新启动程序!";
abort();
}
std::cout << "请输入密码:" << std::endl;
std::cin >> password;
User *temp=new User;
username.read((char*)temp, sizeof(User));
username.close();
if (password == temp->Password)
{
std::cout << "登录成功!" << std::endl;
}
else
{
std::cout << "密码错误!" << std::endl;
cout << "滑稽信息管理系统发生错误 错误原因:错误的密码登录!" << endl << "请重新启动程序!";
abort();
}
u = *temp;
}
void User::Show_My_Info()
{
system("cls");
std::cout <<endl<< "用户名:" << Username << std::endl;
std::cout << "学/工号:" << ID_Number << std::endl;
std::cout << "权限组:" << Permissions << std::endl;
std::cout << "('1'为管理员 '2'为普通用户)" << std::endl;
std::cout << "寝室号" <<Apartment_id <<"#"<<Dormitory_id << std::endl<<std::endl;
std::cout << "寝室信息:"<< std::endl;
Lookup_My_Dormitory();
}
//提供给Room的账户信息查询
std::string Lookup_User(std::string Username)
{
std::string fname = "./Userinfo/" + Username + ".dat";
std::ifstream in(fname.c_str(), std::ios::binary);
if(!in)
{
std::string r = "用户:" + Username + "( 此用户尚未在系统中注册 无信息)";
return r;
}
else
{
User* temp = new User;
in.read((char*)temp, sizeof(User));
std::string rr = "用户:" + Username + " 学/工号:" + std::to_string(temp->ID_Number);
return rr;
}
in.close();
}
//查询本人寝室信息
void User::Lookup_My_Dormitory()
{
std::string real_fid = "Data\\" + std::to_string(Apartment_id) + "\\" + std::to_string(Dormitory_id) + ".dat";
std::ifstream in(real_fid.c_str(), std::ios::binary|std::ios::_Nocreate);
if (!in)
{
cout<<endl << "此寝室信息尚未创建!"<<endl;
}
else
{
Dormitory* temp = new Dormitory;
in.read((char*)temp, sizeof(Dormitory));
for (int i = 0; i < temp->Student_Total; i++)
{
cout << Lookup_User(temp->Name[i]) << endl;
}
}
}
//查询同公寓寝室信息
void User::Lookup_Dormitory()
{
std::string id;
cout <<endl<< Username << " 你好 您可查询的范围是" << Apartment_id << "公寓所有寝室" << endl;
cout << "请输入需要查询的寝室号:" << endl;
cin >> id;
std::string real_fid = "Data\\" + std::to_string(Apartment_id) + "\\" + id + ".dat";
std::ifstream in(real_fid.c_str(), std::ios::binary | std::ios::_Nocreate);
if (!in)
{
cout << "此寝室信息尚未创建!";
in.close();
}
else
{
system("cls");
Dormitory* temp = new Dormitory;
in.read((char*)temp, sizeof(Dormitory));
temp->Show();
in.close();
}
}
//管理员查询全系统信息
void User::Admin_Lookup_Dormitory()
{
std::string a_id, d_id;
cout << Username << "管理员你好 您可查询的范围是系统内所有信息" << endl;
cout << "请输入需要查询的公寓号:" << endl;
cin >> a_id;
cout << "请输入需要查询的寝室号:" << endl;
cin >> d_id;
std::string real_fid = "Data\\" + a_id + "\\" + d_id + ".dat";
std::string real_id = "Data\\" + a_id + "\\";
std::ifstream in(real_fid.c_str(), std::ios::binary | std::ios::_Nocreate);
int State = 0;
State = _access(real_id.c_str(), 0);
if (State == -1)
{
cout << "此公寓信息尚未创建!";
}
else if (!in)
{
cout << "此寝室信息尚未创建!";
}
else
{
in.close();
real_fid = "Data\\" + a_id + "\\" + "info.dat";
in.open(real_fid.c_str(), std::ios::binary | std::ios::_Nocreate);
Apartment* atemp = new Apartment;
in.read((char*)atemp, sizeof(Apartment));
atemp->Show();
in.close();
real_fid = "Data\\" + a_id + "\\" + d_id + ".dat";
in.open(real_fid.c_str(), std::ios::binary | std::ios::_Nocreate);
Dormitory* temp = new Dormitory;
in.read((char*)temp, sizeof(Dormitory));
temp->Show();
for (int i = 0; i < temp->Student_Total; i++)
{
cout << Lookup_User(temp->Name[i]) << endl<<endl<<endl;
}
}
in.close();
}
仅为刚入门C++的练手程序










