使用面向对象的方法编写代码,求解下列问题: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 lab 2 station 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代码需要符合以下要求: ✓ VS 项目包含至少三个文件 ComputerLab.h、ComputerLab.cpp、main.cpp, ComputerLab.h中写类的定义,ComputerLab.cpp中写类的成员函数实现, main.cpp 中写主函数; ✓ 代码中应有两个类ComputerLab和User,且类中所有数据成员都为私有; ✓ ComputerLab 类是 User 类的友元,可访问 User 类中私有数据成员; ✓ 给 ComputerLab 类重载操作符+和-,分别实现用户登录和退出功能实现下列格式的输入输出If user SWE001 is logged into lab 2 station 3 and user DMT001 is logged into lab 1 station 4, use + for logging in, for logging off, and = for end of input: SWE001 2 3 DMT001 1 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.
时间: 2023-12-10 10:37:49 浏览: 166
ComputerLab.h:
```
#pragma once
#include <iostream>
#include <vector>
#include "User.h"
class ComputerLab
{
private:
std::vector<User*> computers[4][10]; // 4 labs, each lab has 10 computers
public:
friend class User;
void login(User* user, int lab, int computer);
void logout(User* user);
friend std::ostream& operator<<(std::ostream& os, const ComputerLab& lab);
};
```
ComputerLab.cpp:
```
#include "ComputerLab.h"
void ComputerLab::login(User* user, int lab, int computer)
{
if (user->loggedIn) {
std::cout << "invalid login\n";
return;
}
if (!computers[lab - 1][computer - 1].empty()) {
std::cout << "invalid login\n";
return;
}
computers[lab - 1][computer - 1].push_back(user);
user->loggedIn = true;
user->lab = lab;
user->computer = computer;
}
void ComputerLab::logout(User* user)
{
if (!user->loggedIn) {
std::cout << "invalid logoff\n";
return;
}
computers[user->lab - 1][user->computer - 1].clear();
user->loggedIn = false;
}
std::ostream& operator<<(std::ostream& os, const ComputerLab& lab)
{
for (int i = 0; i < 4; i++) {
os << "Lab " << i + 1 << ":\n";
for (int j = 0; j < 10; j++) {
os << "Computer " << j + 1 << ": ";
if (lab.computers[i][j].empty()) {
os << "empty\n";
}
else {
os << lab.computers[i][j][0]->id << "\n";
}
}
os << "\n";
}
return os;
}
```
User.h:
```
#pragma once
#include <string>
class ComputerLab;
class User
{
private:
std::string id;
bool loggedIn;
int lab;
int computer;
public:
friend class ComputerLab;
User(std::string id);
void login(ComputerLab& lab, int computer);
void logout(ComputerLab& lab);
};
```
User.cpp:
```
#include "User.h"
#include "ComputerLab.h"
User::User(std::string id) : id(id), loggedIn(false), lab(0), computer(0) {}
void User::login(ComputerLab& lab, int computer)
{
lab.login(this, this->lab, computer);
}
void User::logout(ComputerLab& lab)
{
lab.logout(this);
}
```
main.cpp:
```
#include <iostream>
#include <string>
#include "ComputerLab.h"
#include "User.h"
int main()
{
std::string input;
ComputerLab lab;
while (std::getline(std::cin, input)) {
if (input == "=") {
std::cout << lab;
break;
}
std::string id;
int labNum, compNum;
std::stringstream ss(input);
ss >> id >> labNum >> compNum;
if (id.size() != 6 || id.substr(0,3) != "SWE" && id.substr(0,3) != "DMT" || !isdigit(id[3]) || !isdigit(id[4]) || !isdigit(id[5])) {
std::cout << "invalid login\n";
continue;
}
User* user = new User(id);
if (compNum < 1 || compNum > 10 || labNum < 1 || labNum > 4) {
std::cout << "invalid login\n";
delete user;
continue;
}
if (input[input.size() - 1] == '+') {
user->login(lab, compNum);
}
else if (input[input.size() - 1] == '-') {
user->logout(lab);
delete user;
}
}
return 0;
}
```
阅读全文