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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第8章:面向对象设计——题目10

    2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...

  8. 《Cracking the Coding Interview》——第8章:面向对象设计——题目9

    2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...

  9. 《Cracking the Coding Interview》——第8章:面向对象设计——题目8

    2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...

  10. 《Cracking the Coding Interview》——第8章:面向对象设计——题目7

    2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...

随机推荐

  1. HTML入门2—HTML常用标签

    HTML常用标签练习 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  2. 【转】如何手动添加Android Dependencies包

    在ADT16 之前可以在工程里面做关联,eclipse会在工程上自动添加ReferenceLibrary.新版本的ADT修改了第三方jar的导入方式,只需要在工程目录下新建libs文件夹,注意是lib ...

  3. FastRCNN 训练自己数据集 (1编译配置)

    http://www.cnblogs.com/louyihang-loves-baiyan/p/4885659.html 按照博客的教程配置,但自己在服务器上配置时,USE_CUDNN = 1会报错, ...

  4. Mac安装protobuf 流程

    下载 https://github.com/google/protobuf/releases 找到对应版本下载 编译 cd protobuf./autogen.sh./configuremake 安装 ...

  5. 泉五培训Day1

    T1 树学 题目 [问题描述] 给定一颗 n 个点的树,树边带权,试求一个排列 P,最大化下式 其中,calc(a, b)表示树上由a到b经过的最大边权. [输入格式] 第一行一个整数 n,表示点数下 ...

  6. floyed dij spfa 模板

    /* SPFA模板 */ const int inf=0x3f3f3f3f; inline int SPFA(int s){ memset(dis,inf,sizeof(dis)); queue< ...

  7. 牛客小白月赛2 H 武 【Dijkstra】

    链接:https://www.nowcoder.com/acm/contest/86/H来源:牛客网 题目描述 其次,Sεlιнα(Selina) 要进行体力比武竞赛. 在 Sεlιнα 所在的城市, ...

  8. SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用

    1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...

  9. java 反射bean

    描述:新bean和旧bean拥有相同的字段属性.将旧bean的属性值,赋给新bean. Java代码如下: /** * 反射Bean * @param bean 反射的对象 * @param newB ...

  10. 【版本控制——svn】

    reposity_name   //版本库 { Passwd //验证密码文件 Authz //权限控制 Server.conf //主配置 } Authz //权限控制 //由[groups]标签控 ...