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". 输入格式 If user SWE001 is logged into station 2 in lab 3 and user DMT001 is logged into station 1 of lab 4, use + for logging in, - for logging off, and = for end of input: + SWE001 2 3 + DMT001 1 4 《面向对象程序设计 C++》 2022-2023 春季学期 2 / 4 - SWE001 = 输出格式 The status of all labs (who is logged into which computer). Otherwise, display invalid login or invalid logoff. You need to display the status of all labs even when the input is invalid.
时间: 2024-02-25 22:57:27 浏览: 35
以下是一个简单的 C++ 程序,用于跟踪用户登录的情况:
```
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Lab {
public:
Lab(int num, int start, int end) : lab_num(num), start_station(start), end_station(end) {}
int lab_num;
int start_station;
int end_station;
map<int, string> stations;
};
class User {
public:
User(string id) : user_id(id) {}
string user_id;
int lab_num;
int station_num;
};
int main() {
vector<Lab> labs = {Lab(1, 1, 5), Lab(2, 1, 6), Lab(3, 1, 4), Lab(4, 1, 3)};
map<string, User> users;
string input;
while (getline(cin, input)) {
if (input == "=") break;
char op = input[0];
string id = input.substr(2, 6);
int lab_num = stoi(input.substr(9, 1));
int station_num = stoi(input.substr(11));
if (op == '+') {
if (users.count(id)) {
cout << "invalid login" << endl;
} else {
bool valid = true;
for (auto& x : users) {
User& u = x.second;
if (u.lab_num == lab_num && u.station_num == station_num) {
valid = false;
cout << "invalid login" << endl;
break;
}
}
if (valid) {
User u(id);
u.lab_num = lab_num;
u.station_num = station_num;
users[id] = u;
labs[lab_num-1].stations[station_num] = id;
}
}
} else if (op == '-') {
if (!users.count(id)) {
cout << "invalid logoff" << endl;
} else {
User u = users[id];
labs[u.lab_num-1].stations.erase(u.station_num);
users.erase(id);
}
}
}
for (auto& lab : labs) {
cout << "Lab " << lab.lab_num << ":" << endl;
for (int i = lab.start_station; i <= lab.end_station; i++) {
cout << "Station " << i << ": ";
if (lab.stations.count(i)) {
cout << lab.stations[i] << endl;
} else {
cout << "empty" << endl;
}
}
}
return 0;
}
```
该程序使用了面向对象的编程思想,定义了 Lab 和 User 两个类,分别表示实验室和用户。Lab 类包含了实验室的编号、起始和结束的电脑编号,以及一个 map,用于存储每个电脑当前登录的用户。User 类包含了用户的 ID,以及所在实验室和电脑的编号。
程序的主要逻辑在 while 循环中,根据输入的操作类型进行相应的处理。如果是登录操作,首先判断该用户是否已经登录,如果已经登录则输出 "invalid login";否则遍历已经登录的用户,判断当前实验室和电脑是否已经被占用,如果被占用则输出 "invalid login",否则将该用户添加到 users 和对应的实验室的 stations 中。如果是登出操作,首先判断该用户是否已经登录,如果没有登录则输出 "invalid logoff",否则将该用户从 users 和对应的实验室的 stations 中删除。
最后,程序遍历所有实验室和电脑,输出当前登录的用户或者 "empty"。如果输入数据不合法,则会输出相应的错误信息,但仍然会输出所有实验室的状态。