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.pswp.cn/web/13591.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/13591.shtml
英文地址,請注明出處:http://en.pswp.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…