ComputerLab实例2.0(继承)

要求:

Write a computer program that could be used to track users' activities.

Lab NumberComputer Station Numbers
11-3
21-4
31-5
41-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<iostream>
#include<string>
#include<vector>
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<User>& v)const;void show_date(const User& u)const;vector<vector<User>> Rooms;pair<int, int> 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<User>(3, User());this->Rooms[1] = vector<User>(4, User());this->Rooms[2] = vector<User>(5, User());this->Rooms[3] = vector<User>(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<<u.year;return;
}
void ComputerLab::show(const vector<User>& 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<int, int> 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<u2.year;i++){d+=365;if(is_leap_year(i))d+=1;}d+=365;if(is_leap_year(u1.year)){d+=1;M[2]=29;}for(int i=1;i<u1.month;i++){d-=M[i];}d-=u1.day;if(!is_leap_year(u2.year))M[2]=28;else M[2]=29;for(int i=1;i<u2.month;i++){d+=M[i];}d+=u2.day;if(u1.year==u2.year){d-=365;if(M[2]==29)d-=1;}return d;
}
void ComputerLab::operator+(StaInReq& r)
{pair<int, int> 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<int, int> 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<int, int> 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<int, int> 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 <iostream>
#include <string>
#include <vector>
#include <cstdio>
#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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/13591.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LabVIEW和ZigBee无线温湿度监测

LabVIEW和ZigBee无线温湿度监测 随着物联网技术的迅速发展&#xff0c;温湿度数据的远程无线监测在农业大棚、仓库和其他需环境控制的场所变得日益重要。开发了一种基于LabVIEW和ZigBee技术的多区域无线温湿度监测系统。系统通过DHT11传感器收集温湿度数据&#xff0c;利用Zig…

uniapp-自定义navigationBar

封装导航栏自定义组件 创建 nav-bar.vue <script setup>import {onReady} from dcloudio/uni-appimport {ref} from vue;const propsdefineProps([navBackgroundColor])const statusBarHeight ref()const navHeight ref()onReady(() > {uni.getSystemInfo({success…

图生代码,从Hello Onion 代码开始

从Hello Onion 代码开始 1&#xff0c;从代码开始 原生语言采用java 作为载体。通过注解方式实现“UI可视化元素"与代码bean之间的映射. 转换示例 2&#xff0c;运行解析原理 在执行JAVA代码期间&#xff0c;通过读取注解信息&#xff0c;转换为前端的JSON交由前端JS框…

NB49 牛群的秘密通信

描述 在一个远离人类的世界中&#xff0c;有一群牛正在进行秘密通信。它们使用一种特殊的括号组合作为加密通信的形式。每一组加密信息均包括以下字符&#xff1a;(,{,[,),},]。 加密信息需要满足以下有效性规则&#xff1a; 每个左括号必须使用相同类型的右括号闭合。左括号…

c++设计模式-->访问者模式

#include <iostream> #include <string> #include <memory> using namespace std;class AbstractMember; // 前向声明// 行为基类 class AbstractAction { public:virtual void maleDoing(AbstractMember* member) 0;virtual void femaleDoing(AbstractMemb…

OrangePiKunPengPro | 开发板学习与使用

OrangePi KunPengPro | 开发板学习与使用 时间:2024年5月23日20:51:12 文章目录 `OrangePi KunPengPro` | 开发板学习与使用1.参考2.资料2.使用2-1.通过串口登录系统2-2.通过SSH登录系统2-3.安装交叉编译工具链2-4.复制文件到设备1.参考 1.OrangePi Kunpeng Pro Orange Pi官网…

c语言指针入门(二)

今天学习了指针的两个常用场景&#xff0c;在此记录&#xff0c;以便后续查看。 场景1&#xff1a;传数组 在c语言中&#xff0c;我们在定义函数的时候是没有办法直接传一个数组进去的&#xff0c;为了解决这个问题&#xff0c;我们一般将数组的名称当作一个指针参数传入到函数…

mysql主从复制的步骤和使用到的操作命令有哪些?

步骤&#xff1a; 配置主服务器&#xff08;Master&#xff09;&#xff1a; 启用二进制日志记录&#xff08;binary logging&#xff09;。配置主服务器的唯一标识&#xff08;server-id&#xff09;。创建用于复制的专用复制账户。 配置从服务器&#xff08;Slave&#xff0…

安装Pnetcdf顺便升级autoconf与automake

Netcdf NetCDF&#xff08;Network Common Data Form&#xff09;是一种用于存储科学数据的文件格式和软件库。它是一种自描述、可移植且可扩展的数据格式&#xff0c;广泛应用于气象学、海洋学、地球科学和其他领域的科学研究。 NetCDF文件以二进制形式存储&#xff0c;结构…

Qt | QGridLayout 类(网格布局)

01、上节回顾 Qt | QBoxLayout 及其子类(盒式布局)02、QGridLayout 简介 1、网格布局原理(见下图): 基本原理是把窗口划分为若干个单元格,每个子部件被放置于一个或多个单元格之中,各 单元格的大小可由拉伸因子和一行或列中单元格的数量来确定,若子部件的大小(由 sizeH…

Vue从入门到实战 Day08~Day10

智慧商城项目 1. 项目演示 目标&#xff1a;查看项目效果&#xff0c;明确功能模块 -> 完整的电商购物流程 2. 项目收获 目标&#xff1a;明确做完本项目&#xff0c;能够收获哪些内容 3. 创建项目 目标&#xff1a;基于VueCli自定义创建项目架子 4. 调整初始化目录 目…

网络安全之BGP详解

BGP&#xff1b;边界网关协议 使用范围&#xff1b;BGP范围&#xff0c;在AS之间使用的协议。 协议的特点&#xff08;算法&#xff09;&#xff1a;路径矢量型&#xff0c;没有算法。 协议是否传递网络掩码&#xff1a;传递网络掩码&#xff0c;支持VLSM&#xff0c;CIDR …

【15】编写shell-安装mysql

说明: 1、请注意mysql版本的压缩包格式 2、请注意挂载data盘 3、请注意部署包和shell脚本放在同一个文件夹 4、实现shell脚本自动化部署mysql5.7.40版本 # !/bin/bash#****************************************************** # Author : 秋天枫叶35 # Last modified …

Spring Boot 中 对话 Redis

Redis 是一款开源的&#xff0c;使用 C 开发的高性能内存 Key/Value 数据库&#xff0c;支持 String、Set、Hash、List、Stream 等等数据类型。它被广泛用于缓存、消息队列、实时分析、计数器和排行榜等场景。基本上是当代应用中必不可少的软件&#xff01; Spring Boot 对 Re…

oracle正则的使用

1、建表 create table person (first_name varchar2(20),last_name varchar2(20),email varchar2(40),zip varchar2(20)); insert into PERSON (first_name, last_name, email, zip) values (Steven, Chen, stevenhp.com, 123456); insert into PERSON (first_name, last_name…

ASP+ACCESS基于B2C电子商务网站设计

摘 要 运用ASP技术结合了Access数据库原理&#xff0c;基于B/S模式我们开发了一个网上购物系统。在我们的系统中&#xff0c;顾客可以很方便的注册成为会员&#xff0c;对商品进行浏览检索&#xff0c;查看商品的详细资料&#xff0c;然后根据各人的喜好购买心仪的商品。系统…

CCF20220901——如此编码

CCF20220901——如此编码 代码如下&#xff1a; #include<bits/stdc.h> using namespace std; int main() {int n,m,cnt1,a[1000],c[1000]{1};cin>>n>>m;for(int i1;i<n;i){cin>>a[i];cnt*a[i];c[i]cnt;}int b[1000]{0};for(int i1;i<n;i)b[i](…

JPHS-JMIR Public Health and Surveillance

文章目录 一、期刊简介二、征稿信息三、期刊表现四、投稿须知五、投稿咨询 一、期刊简介 JMIR Public Health and Surveillance是一本多学科期刊&#xff0c;专注于公共卫生创新与技术的交叉领域&#xff0c;包括公共卫生信息学、监测&#xff08;监测系统和快速报告&#xff…

CCF20220601——归一化处理

CCF20220601——归一化处理 代码如下&#xff1a; #include<bits/stdc.h> using namespace std; int main() {int n,a[1000],sum0;scanf("%d",&n);for(int i1;i<n;i){scanf("%d",&a[i]);suma[i];}double aver1.0,b0.0,d1.0;aversum/(n*1…

Java基础(三)- 多线程、网络通信、单元测试、反射、注解、动态代理

多线程基础 线程&#xff1a;一个程序内部的一条执行流程&#xff0c;只有一条执行流程就是单线程 java.lang.Thread代表线程 主线程退出&#xff0c;子线程存在&#xff0c;进程不会退出 可以使用jconsole查看 创建线程 有多个方法可以创建线程 继承Thread类 优点&#x…