2014-04-23 17:45

题目:假设有个呼叫中心,有接线员、经理、主管三种角色。如果接线员无法处理呼叫,就上传给经理;如果仍无法处理,则上传给主管。请用代码描述这一过程。

解法:第一眼觉得这题肯定是在考察设计模式,很像exception的throw过程。对于我这种对设计模式一窍不通的人,这题还无法很好解答。待稍后专门学习设计模式之后,再回来好好琢磨一遍。

代码:

 // 8.2 Design a call center system to handle calls, the order will be respondent->manager->director, find the first employee to handle a call.
#include <iostream>
#include <vector>
using namespace std; class People {
public:
People() {};
virtual void handle() = ;
virtual ~People() {};
}; class Respondent: public People {
public:
Respondent() {};
void handle() {
cout << "Respondent is handling the call." << endl;
};
~Respondent() {};
}; class Manager: public People {
public:
Manager() {};
void handle() {
cout << "Manager is handling the call." << endl;
};
~Manager() {};
}; class Director: public People {
public:
Director() {};
void handle() {
cout << "Director is handling the call." << endl;
};
~Director() {};
}; class CallCenter {
public:
CallCenter(int num_respondent = , int num_manager = , int num_director = ) {
respondents.resize(num_respondent);
managers.resize(num_manager);
directors.resize(num_director);
respondents_available.resize(num_respondent);
managers_available.resize(num_manager);
directors_available.resize(num_director);
fill(respondents_available.begin(), respondents_available.end(), true);
fill(managers_available.begin(), managers_available.end(), true);
fill(directors_available.begin(), directors_available.end(), true);
} void handle() {
size_t i; for (i = ; i < respondents.size(); ++i) {
if (respondents_available[i]) {
break;
}
}
if (i < respondents.size()) {
respondents_available[i] = false;
respondents[i].handle();
respondents_available[i] = true;
return;
} for (i = ; i < managers.size(); ++i) {
if (managers_available[i]) {
break;
}
}
if (i < managers.size()) {
managers_available[i] = false;
managers[i].handle();
managers_available[i] = true;
return;
} for (i = ; i < directors.size(); ++i) {
if (directors_available[i]) {
break;
}
}
if (i < directors.size()) {
directors_available[i] = false;
directors[i].handle();
directors_available[i] = true;
return;
}
}
private:
vector<Respondent> respondents;
vector<Manager> managers;
vector<Director> directors;
vector<bool> respondents_available;
vector<bool> managers_available;
vector<bool> directors_available;
}; int main()
{
CallCenter *p_call_center = nullptr;
int r, m, d; while (cin >> r >> m >> d) {
p_call_center = new CallCenter(r, m, d);
p_call_center->handle();
delete p_call_center;
p_call_center = nullptr;
} return ;
}

《Cracking the Coding Interview》——第8章:面向对象设计——题目2的更多相关文章

  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. TP5.1:将外部资源引入到框架中(css/js/font文件)

    为了让我们的框架形式变得更加好看,我们需要加入Bootstrap和Jq文件到框架中 1.通过Bootstrap和jq官网进行相关文件的下载 (1)Bootstrap下载地址:https://v3.bo ...

  2. percona-toolkit 工具集安装

    下载地址: www.percona.com/downloads/percona-toolkit     安装方法一,源码安装: perl Makefile.PL make:make install   ...

  3. DP之背包问题详解及案例

    0-1背包 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 #include <stdio.h> #include <stri ...

  4. jade在命令行实时编译

    jade文件: doctype html html head title jade study body h1 imoock jade study 在jade文件夹下,终端输入 jade index. ...

  5. python_1_变量的使用

    print("hello word") name="Qi Zhiguang" print("My name is",name) name2= ...

  6. Python测量时间,用time.time还是time.clock

    在计算机领域有多种时间.第一种称作CPU时间或执行时间,用于测量在执行一个程序时CPU所花费的时间.第二种称作挂钟时间,测量执行一个程序时的总时间.挂钟时间也被称作流逝时间或运行时间.与CPU时间相比 ...

  7. mac系统的几种u盘启动制作方式

    先拿一个U盘,格式化好(guid分区表之类的选项弄好) 1.通过终端制作: 1>下载好自己要安装的系统镜像,最新的在App Store上下,以前的可以去pc 苹果等地方下载 2>在终端输入 ...

  8. link链接外部样式表

    一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...

  9. struts2入门第一天----------配置环境

    放假之后有空就开始走上了三大框架的学习.第一个选择的框架是struts2.首先第一步当然是环境的配置.去apache官网把struts2下载下来.然后在自己的开发工具下创建一个web项目.在lib文件 ...

  10. Centos 7 下安装mysql

    // 卸载原有maridb-lib库 [root@localhost ~]# rpm -qa | grep mariadb mariadb-libs-5.5.41-2.el7_0.x86_64 [ro ...