《Cracking the Coding Interview》——第8章:面向对象设计——题目4
2014-04-23 18:17
题目:设计一个停车位的类。
解法:停车位,就要有停车、取车的功能了。另外我还加了一个工作线程用于计费,每秒给那些有车的车位加1块钱费用。
代码:
// 8.4 Design a class to simulate the parking lot.
#include <iostream>
#include <string>
#include <thread>
#include <vector>
using namespace std; class ParkingLot {
public:
ParkingLot(int _capacity = ):capacity(_capacity) {
slots.resize(capacity);
fees.resize(capacity);
fill(slots.begin(), slots.end(), false);
fill(fees.begin(), fees.end(), );
}; void start() {
work = new thread(workThread, this);
}; void parkIn() {
int i; for (i = ; i < (int)slots.size(); ++i) {
if (!slots[i]) {
cout << "Car is parked at slot " << i << "." << endl;
slots[i] = true;
fees[i] = ;
return;
}
} cout << "Sorry, no more slot is available." << endl;
}; void getOut(int id) {
if (id < || id > (int)slots.size() - ) {
cout << "Invalid slot number." << endl;
return;
} else if (slots[id] == false) {
cout << "The slot is empty." << endl;
return;
} cout << "Car in slot " << id << " is delivered. Total fee is " << fees[id] << " bucks." << endl;
slots[id] = false;
fees[id] = ;
}; friend void workThread(ParkingLot *); ~ParkingLot() {
slots.clear();
fees.clear();
work->detach();
delete work;
};
private:
thread *work;
int capacity;
vector<bool> slots;
vector<int> fees;
}; void workThread(ParkingLot *p)
{
while (true) {
// sleep for one second.
_sleep();
for (int i = ; i < p->capacity; ++i) {
if (p->slots[i]) {
++p->fees[i];
}
}
}
} int main()
{
ParkingLot *p;
string cmd;
int id; p = new ParkingLot();
p->start();
while (cin >> cmd) {
if (cmd == "park") {
p->parkIn();
} else if (cmd == "get") {
cin >> id;
p->getOut(id);
} else if (cmd == "end") {
break;
}
}
delete p; return ;
}
《Cracking the Coding Interview》——第8章:面向对象设计——题目4的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目10
2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...
随机推荐
- hdu-1892 See you~---二维树状数组运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 题目大意: 题目大意:有很多方格,每个方格对应的坐标为(I,J),刚开始时每个格子里有1本书, ...
- bzoj3882 [Wc2015]K小割
Description Input Output Sample Input 3 3 1 3 100 1 2 3 2 3 4 1 3 5 Sample Output 8 9 12 -1 正解:暴搜+ ...
- 【转】在程序中设置android:gravity 和 android:layout_Gravity属性
在进行UI布局的时候,可能经常会用到 android:gravity 和 android:layout_Gravity 这两个属性. 关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一 ...
- 2017.10.27 C语言精品集
第一章 程序设计和C语言 1.1 什么是计算机程序? @ ······ 所谓程序,就是一组计算机能识别和执行的指令.每一条指令使计算机执行特定的操作. 计算机的一切操作都是由程序控制的.所以计算机的本 ...
- 20145238-荆玉茗 《Java程序设计》第四次实验报告
20145238<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...
- css3阴影 box-shadow
语法 box-shadow:X轴偏移量 y轴偏移量 [阴影模糊半径] [阴影扩展半径] [阴影颜色] [投影方式] 参数介绍: 注:inset 可以写在参数的第一个或最后一个,其它位置是无效的. 阴影 ...
- R 语言学习日志 1
1. CSV文件的的读取与写出 2. 数据集筛选 3. 简单随机抽样 sample函数 正文: 1. CSV文件的的读取与写出 文件读取: df2 <- read.table(" ...
- 永久免费开源的卫星地形图地图下载工具更新Somap2.13版本功能更新 更新时间2019年2月22日13:59:05
一.下载地址 最新版本下载地址:SoMap2.13点击此处下载 二.系统自主开发特色功能展示 1.上百种地图随意下载 高德.百度.arcgis.谷歌.bing.海图.腾讯.Openstreet.天地 ...
- github上更新fork项目
转载:https://blog.csdn.net/qq1332479771/article/details/56087333 ps:需要用GitHub所指定的chrome或者firefox浏览器,其它 ...
- Servlet学习笔记02——什么是http协议?
1.http协议 (了解) (1)什么是http协议? 是一种网络应用层协议,规定了浏览器与web服务器之间 如何通信以及相应的数据包的结构. 注: a.tcp/ip: 网络层协议,可以保证数据可靠的 ...