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. Python模块与函数

    python的程序由包(package).模块(module)和函数组成.模块是处理某一类问题的集合,模块由函数和类组成,包是由一系列模块组成的集合.包必须至少包含一个__init__.py文件,该文 ...

  2. Python基本数据类型(一)

    一.int的函数说明(部分函数Python2特有,Python3已删除,部分函数Python3新增:) class int(object): """ int(x=0) - ...

  3. mysql语句的相关操作整理

    事实证明,如果不经常跟代码,语句打交道,人家可是会翻脸不认人的,大脑也会觉得一脸懵逼,不知道做错了啥,这次长点记性了,把语句整理出来,不仅加强对sql语句的记忆,还能有个笔记,以后大脑懵逼了还能回来看 ...

  4. OpenGL学习 Our First OpenGL Program

    This shows you how to create the main window with the book’s application framework and how to render ...

  5. MySQL入门很简单: 12 MYSQL 用户管理

    1. 权限表 安装MySQL会自动安装一个名为mysql的数据库,存储权限表: user表, db表,host表,table_priv表,columns_priv表,proc_priv表等. 1)us ...

  6. Facebook interview problem:13. Roman to Integer

    description: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  7. 微信小程序加载本地图片方法

    目录结构如下,只要图片按正确的方式放入小程序的开发工具的项目中,即可在wxml文件中用内联样式或者image标签都可以引用本地的图片. 步骤一:微信开发工具 打开项目 步骤二:新建个文件夹(放项目的一 ...

  8. framework7中一行的字如果过多就省略号显示的CSS写法

    .order-info-title { text-overflow: ellipsis !important; white-space: nowrap !important; overflow: hi ...

  9. 类型构造器--高阶类型(构造器):Kind (type theory)--类型的元

    元类型(0阶类型):nullary type, data types 一元类型(一阶类型):unary  adj. [数] 一元的   二元类型: is the kind of a binary ty ...

  10. eclipse使用maven install 命令,生成war包中没有jsp/js/css的解决方法

    在pom.xml文件中添加如下11行代码就可以了. <build> <plugins> <plugin> <groupId>org.apache.mav ...