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. COGS 146. [USACO Jan08] 贝茜的晨练计划

    ★☆   输入文件:cowrun.in   输出文件:cowrun.out   简单对比时间限制:1 s   内存限制:32 MB 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运 ...

  2. May 2 2017 Week 18 Tuesday

    The beauty of the journey is found in the scenery along the way. 旅行之美在于沿途所见的风景. Several years ago, I ...

  3. UE4的蓝图都能做什么

    创建关卡脚本 蓝图具有和虚幻3中Kismet一样的功能,每个关卡都由自己的蓝图,他可以: 引用和操控actors 控制使用Matinee的过场 管理关卡流,存档点以及其他关卡相关的系统 和关卡中的类蓝 ...

  4. QT学习之QString的arg方法

    在QT的QString中,arg方法类似于C中的printf中使用的格式输出符(只是有点类似). 在QT5的帮助文档中,可以看出以下几点: 使用arg(str1, str2, str3)这种方法进行替 ...

  5. vue中动画的封装

    <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-active,.v-leave-active{ transition:opacit ...

  6. 离线安装vscode vsix插件

    VS代码扩展市场 通过扩展增强Visual Studio代码的强大功能 https://marketplace.visualstudio.com/vscode Visual Studio Code包含 ...

  7. visual attention

    The visual attention mechanism may have at least the following basic components [Tsotsos, et. al. 19 ...

  8. RestKit ,一个用于更好支持RESTful风格服务器接口的iOS库

    简介 RestKit 是一个用于更好支持RESTful风格服务器接口的iOS库,可直接将联网获取的json/xml数据转换为iOS对象. 项目主页: RestKit 最新示例: 点击下载 注意: 如果 ...

  9. Python 文件访问模式

    f = open('xxx文件', '访问模式') r    以只读方式打开文件(read).文件的指针将会放在文件的开头.默认模式. w   打开一个文件只用于写入(write).如果该文件已存在则 ...

  10. 微信小程序清除默认样式

    1.清除button的默认样式 button::after{border:none;}input{outline:none;border:none;list-style: none;}