设计要求:
Write a computer program that could be used to track, by lab, which user is logged into which computer:
Lab Number | Computer Station Numbers |
1 | 1-5 |
2 | 1-6 |
3 | 1-4 |
4 | 1-3 |
➢ You run four computer labs. Each lab contains computer stations that are numbered as the above table.
➢ Each user has a unique ID number. The ID starting with three characters (for example, SWE or DMT), and followed by three digits (like, 001).
➢ Whenever a user logs in, the user’s ID, lab number, and the computer station number are transmitted to your system. For example, if user SWE001 logs into station 2 in lab 3, then your system receives (SWE001, 2, 3) as input data. Similarly, when a user SWE001 logs off a station,then your system receives the user id SWE001.
➢ If a user who is already logged into a computer attempts to log into a second computer, display "invalid login".If a user attempts to log into a computer which is already occupied, display "invalid login".If a user who is not included in the database attempts to log out, display "invalid logoff".
设计思路:
- 用vector<vector<string>> lab容器模拟实验室,编写init函数,对容器进行初始化,即每台电脑都是无人使用的状态。
- 对于每个登录的用户,用check函数检查,检查成功则修改电脑状态,未成功则提醒用户未成功的原因。对于每个退出的用户,用logoff函数操作。对于每一个操作结束后,调用display函数展示实验室的状态。
- 编写logoff函数用于控制用户的退出,并重置该电脑的状态。
- 编写check函数,遍历实验室的每一台电脑,检查某个用户是否已经登录了某一台电脑。
- 编写show函数显示每一间实验室中每一台电脑的状态,编写display函数显示每一间实验室的状态。
代码实现:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class ComputerLab {
public:
ComputerLab():Rooms(4)
{
Rooms[0]=vector<string>(5,"empty");
Rooms[1]=vector<string>(6,"empty");
Rooms[2]=vector<string>(4,"empty");
Rooms[3]=vector<string>(3,"empty");
}
bool login(int room, int computer, const string& userId)
{
if(room<1||room>Rooms.size()||computer<1||computer>Rooms[room-1].size()||check(userId))
{
cout << "invalid login" << endl;
return false;
}
if (Rooms[room - 1][computer - 1] == "empty") {
Rooms[room - 1][computer - 1] = userId;
return true;
} else {
cout << "invalid login" << endl;
return false;
}
}
bool logout(const string& userId)
{
for (auto& room : Rooms)
{
auto it = find(room.begin(), room.end(), userId);
if (it != room.end())
{
*it = "empty";
return true;
}
}
cout << "invalid logoff" << endl;
return false;
}
void display() const {
for (int i = 0; i < Rooms.size(); i++) {
cout << i + 1 << ":";
show(Rooms[i]);
}
}
private:
vector<vector<string> > Rooms;
static void show(const vector<string>& v) {
for (int i = 0; i < v.size(); i++) {
cout << " " << i + 1 << ":" << v[i];
}
cout << endl;
}
bool check(const string& id) const
{
for(const vector<string>& room : Rooms)
{
if(find(room.begin(),room.end(),id)!=room.end())return true;
}
return false;
}
};
class User {
public:
User(const string& id) : id(id) {}
const string& getId() const {
return id;
}
private:
string id;
};
int main() {
ComputerLab lab;
char op='+';
while (cin >> op && op != '=') {
if (op == '+') {
string id;
int room, computer;
cin >> id >> room >> computer;
User user(id);
lab.login(room,computer,user.getId());
lab.display();
} else if (op == '-') {
string id;
cin >> id;
lab.logout(id);
lab.display();
} else {
cout << "Wrong input" << endl;
}
}
return 0;
}
运行结果
代码实现(重载):
重载运算符+(登录),-(登出),在类内重载。
bool operator+(loginReq& r)
{
string userId=(r.userPointer)->id;
if(login(r.labNum,r.stationNum,userId))r.flag=true;
else r.flag=false;
return r.flag;
}
bool operator-(logoffReq& r)
{
string userId=(r.userPointer)->id;
if(logout(userId))r.flag=true;
else r.flag=false;
return r.flag;
}
结构体定义:
class User {
public:
User(const string& id) : id(id) {}
const string& getId() const {
return id;
}
private:
friend class ComputerLab;
string id;
};
struct loginReq
{
User* userPointer; //指向当前请求登录的用户对象
int labNum; //用户请求登录的 lab 的 number
int stationNum; //用户请求登录的 station 的 number
bool flag;
};
struct logoffReq
{
User* userPointer; //指向当前请求注销的用户对象
bool flag;
};