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. [原创][Windows] Win7安装visual c++ 2015 redistributable x64失败

    在win7中安装visual c++ 2015 redistributable x64 时会卡住,原因是visual c++ 2015 redistributable x64 需要KB2999226, ...

  2. 如何将Twitter的内容导入到SAP CRM和C4C

    Twitter的内容导入SAP CRM Interaction Center呼叫中心 具体步骤查看我的博客Twitter(also Facebook) is official integrated i ...

  3. 打表格,字符串处理,POJ(2136)

    题目链接:http://poj.org/problem?id=2136 水题WA了半天,结果是数组开小了. #include <stdio.h> #include <string.h ...

  4. Poj(1125),Floyd,

    题目链接:http://poj.org/problem?id=1125 多源点最短路中的,最长路的,最短路. 看到这里就懵逼了,解释一下,找到一个源点,使得路最短,(遍历源点),路最短怎么求呢? 就是 ...

  5. 问题 C: B 统计程序设计基础课程学生的平均成绩

    题目描述 程序设计基础课程的学生成绩出来了,老师需要统计出学生个数和平均成绩.学生信息的输入如下: 学号(num)                     学生姓名(name)            ...

  6. css3中的 @Keyframes

    一.介绍 keyframes被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以“@keyframes”开头,后面跟着是动画名称加上一对花括号“{…}”,括号中是一些不同时间段样式规则. ...

  7. Python类型转换+序列操作+基本概念辨析速查手册

     第一部分是Python语言中基础中的基础,根据网上资料,合并如下:       1.类型转换     int(x [,base])  将x转换为一个整数     long(x [,base]) 将x ...

  8. 深入理解HDFS的架构和原理

    (一) HDFS主要是用于做什么的? HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处 ...

  9. Firefox 修改User Agent

    Android 版 Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML ...

  10. 成员变量和成员函数前加static的作用?

    成员变量和成员函数前加static的作用?答:它们被称为常成员变量和常成员函数,又称为类成员变量和类成员函数.分别用来反映类的状态.比如类成员变量可以用来统计类实例的数量,类成员函数负责这种统计的动作 ...