要求:
Write a computer program that could be used to track users' activities.
Lab Number | Computer Station Numbers |
1 | 1-3 |
2 | 1-4 |
3 | 1-5 |
4 | 1-6 |
➢ You run four computer labs. Each lab contains computer stations that are numbered as the above table.
➢ There are two types of users: student and staff. Each user has a unique ID number. The student ID starts with three characters (for example, SWE or DMT), and is followed by three digits (like, 001). The staff ID only contains digits (for example: 2023007).
➢ Whenever a user logs in, the user’s ID, lab number, the computer station number and login date are transmitted to your system. For example, if user SWE001 logs into station 2 in lab 3 in 01 Dec, 2022, then your system receives (+ SWE001 2 3 1/12/2022) as input data. Similarly, when a user SWE001 logs off in 01 Jan, 2023, then your system receives receives (- SWE001 1/1/ 2023). Please use = for end of input.
➢ When a user logs in or logs off successfully, then display the status of stations in labs. When a user logs off a station successfully, display student id of the user, and the number of days he/she logged into the station.
➢ When a user logs off, we calculate the price for PC use. For student, we charge 0 RMB if the number of days is not greater than 14, and 1 RMB per day for the part over 14 days. For staff, we charge 2 RMB per day if the number of days is not greater than 30, and 4 RMB per day for the part over 30 days.
➢ 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 off, display "invalid logoff".
附加要求:
1. 必须是包含以下内容的面向对象程序: ComputerLab 类、基类User 及其派生类 Student和Staff、main 函数。
2. 把 ComputerLab 类作为类 User、 Student、 Staff 的友元,使它直接访问各类用户的私有成员。
3. 为 ComputerLab 类重载操作符 + 和 - ,分别实现 Staff 和 Student 的登录和退出功能:
//请合理设计登录和退出请求的数据类型
⚫ void operator + (StaInReq &r); //Staff 登录
⚫ void operator + (StuInReq &r); //Student 登录
⚫ void operator - (StaOffReq &r); //Staff 退出
⚫ void operator - (StuOffReq &r); //Student 退出
输入样例:
+ SWE100 1 1 1/1/2016 + DMT200 2 6 02/04/2016 + SWE400 1 1 1/01/2016 + SWE400 4 3 10/1/2016 + SWE400 2 1 1/1/2015 + 2019007 2 3 1/1/2015 - 2019007 1/12/2016 - DMT700 1/12/2016 + SWE800 1 6 10/10/2013 + SWE900 5 1 10/10/2014 - SWE700 1/12/2016 =
代码实现:
头文件c_user.hpp
#ifndef C_USER_HPP_INCLUDED #define C_USER_HPP_INCLUDED #pragma once #include#include #include using namespace std; class ComputerLab; class User { public: User() :id("empty"), year(0), month(0), day(0){} User(const string& id, int y = 0, int m = 0, int d = 0) : id(id), year(y), month(m), day(d) {} const string& getId() const { return id; } private: friend class ComputerLab; int year, month, day; string id; }; class Student : public User { public: Student(const string& id, int y = 0, int m = 0, int d = 0) : User(id, y, m, d) {} }; class Staff : public User { public: Staff(const string& id, int y = 0, int m = 0, int d = 0) : User(id, y, m, d) {} }; struct StaInReq { Staff* pointer; int labNum; int stationNum; }; struct StuInReq { Student* pointer; int labNum; int stationNum; }; struct StaOffReq { Staff* pointer; }; struct StuOffReq { Student* pointer; }; class ComputerLab { public: ComputerLab(); void operator+(StaInReq& r); void operator+(StuInReq& r); void operator-(StaOffReq& r); void operator-(StuOffReq& r); void display()const; private: void show(const vector & v)const; void show_date(const User& u)const; vector > Rooms; pair check(const string& str); int calculate(User& u1, User& u2)const; }; #endif // C_USER_HPP_INCLUDED
源文件computer.cpp实现类内成员函数
#include "c_user.hpp" int M[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; ComputerLab::ComputerLab():Rooms(4) { this->Rooms[0] = vector(3, User()); this->Rooms[1] = vector (4, User()); this->Rooms[2] = vector (5, User()); this->Rooms[3] = vector (6, User()); } void ComputerLab::show_date(const User& u)const { cout<<" "; if (u.month < 10)cout << 0; cout << u.month << "/"; if (u.day < 10)cout << 0; cout << u.day<<"/"; cout< & v)const { for (int i = 0; i < v.size(); i++) { cout << " " << i + 1 << ":" << v[i].id; if (v[i].id != "empty") { show_date(v[i]); } } cout << endl; } void ComputerLab::display()const { for (int i = 0; i < Rooms.size(); i++) { cout << i + 1 << ":"; show(Rooms[i]); } } pair ComputerLab::check(const string& str) { for (int i = 0; i < 4; i++) { for (int j = 0; j < this->Rooms[i].size(); j++) { if (Rooms[i][j].id == str)return(make_pair(i, j)); } } return make_pair(-1, -1); } bool is_leap_year(int m) { if(m%400==0)return 1; if(m%100==0)return 0; if(m%4==0)return 1; return 0; } int ComputerLab::calculate(User& u1, User& u2)const { int d=0; for(int i=u1.year+1;i p = check(r.pointer->id); int x = p.first, y = p.second; if (x != -1) { cout << "invalid login" << endl; return; } x = r.labNum, y = r.stationNum; if(x>=4||y>=Rooms[x].size()||Rooms[x][y].id!="empty") { cout << "invalid login" << endl; return; } Rooms[x][y] = *(r.pointer); } void ComputerLab::operator+(StuInReq& r) { pair p = check(r.pointer->id); int x = p.first, y = p.second; if (x != -1) { cout << "invalid login" << endl; return; } x = r.labNum, y = r.stationNum; if(x>=4||y>=Rooms[x].size()||Rooms[x][y].id!="empty") { cout << "invalid login" << endl; return; } Rooms[x][y] = *(r.pointer); } void ComputerLab::operator-(StaOffReq& r) { pair p = check(r.pointer->id); int x = p.first, y = p.second; if (x == -1) { cout << "invalid logoff" << endl; return; } int d = calculate(Rooms[x][y], *(r.pointer)); cout << (r.pointer)->id << " log off, time: " << d << " days, "; int cost = 0; if (d <= 30) { cost = d * 2; } else { cost = 60; d -= 30; cost += d * 4; } cout << "price: " << cost << " RMB" << endl; Rooms[x][y]=User(); } void ComputerLab::operator-(StuOffReq& r) { pair p = check(r.pointer->id); int x = p.first, y = p.second; if (x == -1) { cout << "invalid logoff" << endl; return; } int d = calculate(Rooms[x][y], *(r.pointer)); cout << (r.pointer)->id << " log off, time: " << d << " days, "; int cost = 0; if (d > 14) { cost = d - 14; } cout << "price: " << cost << " RMB" << endl; Rooms[x][y]=User(); }
主函数调用main.cpp
#include#include #include #include #include "c_user.hpp" using namespace std; int main() { ComputerLab lab; char op = '+'; while (cin >> op && op != '=') { if (op == '+') { string id; int y, m, d, labnum, station; cin >> id >> labnum >> station; labnum-=1,station-=1; scanf("%d/%d/%d",&d,&m,&y); if (id[0] >= '0' && id[0] <= '9') { Student s(id, y, m, d); StuInReq r; r.pointer = &s, r.labNum = labnum, r.stationNum = station; lab + r; } else { Staff s(id, y, m, d); StaInReq r; r.pointer = &s, r.labNum = labnum, r.stationNum = station; lab + r; } lab.display(); } else if (op == '-') { string id; int y, m, d, labnum, station; cin >> id; scanf("%d/%d/%d",&d,&m,&y); if (id[0] >= '0' && id[0] <= '9') { Student s(id, y, m, d); StuOffReq r; r.pointer = &s; lab - r; } else { Staff s(id, y, m, d); StaOffReq r; r.pointer = &s; lab - r; } lab.display(); } else { cout << "Wrong input" << endl; } } return 0; }
还没有评论,来说两句吧...