《Cracking the Coding Interview》——第8章:面向对象设计——题目2
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目10
2014-04-24 00:05 题目:用拉链法设计一个哈希表. 解法:一个简单的哈希表,就看成一个数组就好了,每个元素是一个桶,用来放入元素.当有多个元素落入同一个桶的时候,就用链表把它们连起来.由 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目9
2014-04-23 23:57 题目:如何设计一个内存文件系统,如果可以的话,附上一些代码示例. 解法:很遗憾,对我来说不可以.完全没有相关经验,所以实在无从入手.这题目应该和工作经验相关吧? 代码 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目8
2014-04-23 23:49 题目:有个棋牌游戏叫Othello,也叫Reversi.请看游戏规则.中文应该叫黑白棋吧,不常玩儿就是了. 解法:既然这题的规则很清楚,也很清楚,我就写了一个命令行的 ...
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目7
2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...
随机推荐
- 西门子(SIEMENS)软件安装时需要重启的解决方法,regedit restart
打开注册表(regedit) 删除注册表项 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manage\PendingFile ...
- [转载]弹出一个不带地址栏、工具栏的IE非模态窗口
标签:ie /非模态窗口 window.open(url,'_blank','menubar=no,fullscreen=1,toolbar=no,resizable=no,location=no,s ...
- Ubuntu 系统安装
1.首先下载一个Ubuntu系统文件,以.ios后缀结尾的系统镜像文件压缩包. 2,下载一个ultraiso软件,用于制作u盘启动盘 3,将电脑重启,进入BOIS界面,调整系统顺序, 将启动盘系统设置 ...
- Last_Errno: 1396
Last_Errno: 1396 Last_Error: Error 'Operation CREATE USER failed for 'usera63'@'%'' on query. Defa ...
- hdu-3449 Consumer---有依赖性质的背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3449 题目大意: fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒 ...
- 起一个node服务
使用node开发一个应用,非常简单,甚至都不用去配置一堆文件来启动一个webu服务器,直接去官网把这一段示例代码拷过来 https://nodejs.org/en/about/ 中文网没有这个abou ...
- 1.5配置NetBackup数据库备份策略(nbu策略catalog)
1.5配置NetBackup数据库备份策略 建议定期备份NetBackup的索引数据库Catalog,以确保故障时的有效恢复.从Javaconsole可以进入备份NetBackup内部数据库配置窗口, ...
- 问题 D: C++ 习题 输出日期时间--友元函数
题目描述 设计一个日期类和时间类,编写display函数用于显示日期和时间.要求:display函数作为类外的普通函数,分别在Time和Date类中将display声明为友元函数.在主函数中调用dis ...
- 用蒙特卡洛方法计算派-python和R语言
用蒙特卡洛方法算pi-基于python和R语言 最近follow了MOOC上一门python课,开始学Python.同时,买来了概率论与数理统计,准备自学一下统计.(因为被鄙视过不是统计专业却想搞数据 ...
- Apache+Tomcat+jk windows环境下的集群部署
记一次在Windows服务器上搭建apatch+tomcat+jk的集群搭建过程,其中也遇到了很多问题,总结一下. 一.准备工作 1.apache-tomcat-7.0.88 2.Apche http ...