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. wget无法建立SSL连接

    在使用wget工具的过程中,当URL使用HTTPS协议时,经常出现如下错误:“无法建立SSL连接”. 这是因为wget在使用HTTPS协议时,默认会去验证网站的证书,而这个证书验证经常会失败.加上&q ...

  2. 2018.9.8pat秋季甲级考试

    第一次参加pat考试,结果很惨,只做了中间两道题,还有一个测试点错误,所以最终只得了不到50分.题目是甲级练习题的1148-1151. 考试时有些紧张,第一题第二题开始测试样例都运行不正确,但是调试程 ...

  3. C++学习之虚函数继承和虚继承

    虚函数的定义要遵循以下重要规则: 1.如果虚函数在基类与派生类中出现,仅仅是名字相同,而形式参数不同,或者是返回类型不同,那么即使加上了virtual关键字,也是不会进行晚绑定的. 2.只有类的成员函 ...

  4. Android(java)学习笔记73:Intent启动Activity

    1. Intent启动Activity案例 (1)首先是main.xml和other.xml文件如下: main.xml文件: <?xml version="1.0" enc ...

  5. CORS跨域限制以及预请求验证

    之前我们可以通过“Access-Control-Allow-Origin”,实现跨域请求,那是不是所有跨域请求都可以通过设置Access-Control-Allow-Origin实现跨域请求呢?显然不 ...

  6. Spring boot 集成 Swagger

    添加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  7. quartz的持久化任务调度使用应用的dataSource

    Quartz提供两种基本作业存储类型--->第一种类型叫做RAMJobStore:     最佳的性能,因为内存中数据访问最快     不足之处是缺乏数据的持久性,当程序路途停止或系统崩溃时,所 ...

  8. 激活SQLPrompt7.4及以上版本

    SQLPrompt是个很好的sql server提示工具,SQL-SERVER2016只能使用SQLPrompt7.x 以上版本,用激活工具激活后没多久就会检测到激活码不可用.所以感觉是不是有个服务在 ...

  9. rcnn spp_net hcp

    rcnn开创性工作,但是计算时间太长,重复计算太大. spp_net将重复计算避免了. hcp是yan shuicheng那边的,是用bing生成regions,然后用normalized cut将这 ...

  10. 插入数据返回自增id及插入更新二合一

    原文https://blog.csdn.net/dumzp13/article/details/50984413 JDBC: con.setAutoCommit(false); String sql ...