2014-05-08 22:09

题目链接

原题:

Implement a class to create timer object in OOP

题目:用OOP思想设计一个计时器类。

解法:我根据自己的思路,用clock()函数为工具,提供启动、停止、显示时长、重置四个方法。

代码:

 // http://www.careercup.com/question?id=5692127791022080
#include <ctime>
#include <iostream>
#include <string>
using namespace std; class Timer {
public:
Timer(): begin(), end() {}; clock_t ticks() {
if (begin == ) {
return end;
} else {
end = clock();
return end - begin;
}
}; double seconds() {
return 1.0 * ticks() / CLOCKS_PER_SEC;
}; void start() {
begin = clock();
}; void stop() {
if (begin > ) {
end = clock();
end -= begin;
begin = ;
}
}; void reset() {
begin = end = ;
};
private:
clock_t begin, end;
}; int main()
{
Timer timer;
string cmd; while (cin >> cmd) {
if (cmd == "start") {
timer.start();
} else if (cmd == "stop") {
timer.stop();
} else if (cmd == "time") {
printf("%.2f seconds.\n", timer.seconds());
} else if (cmd == "reset") {
timer.reset();
}
} return ;
}

Careercup - Google面试题 - 5692127791022080的更多相关文章

  1. Careercup - Google面试题 - 5732809947742208

    2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...

  2. Careercup - Google面试题 - 5085331422445568

    2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...

  3. Careercup - Google面试题 - 4847954317803520

    2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...

  4. Careercup - Google面试题 - 6332750214725632

    2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...

  5. Careercup - Google面试题 - 5634470967246848

    2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...

  6. Careercup - Google面试题 - 5680330589601792

    2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...

  7. Careercup - Google面试题 - 5424071030341632

    2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...

  8. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  9. Careercup - Google面试题 - 6331648220069888

    2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...

随机推荐

  1. SQL Server编程(05)游标

    在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...

  2. Silverlight - GPU加速

    1. 在Silverlight plug-in上设置 <param name="enableGPUAcceleration" value="true" / ...

  3. luigi学习5-task详解

    task是代码执行的地方.task通过target互相依赖. 下面是一个典型的task的大纲视图. 一.Task.requires requires方法用来指定本task的依赖的其他task对象,依赖 ...

  4. js各种宽高(2)

    在javascript和jquery中,都有对各种高度的写法,在这里,我们就着重讲一下窗口.文档等高度的理解.(宽度和高度差不多!) jquery的各种高度 首先来说一说$(document)和$(w ...

  5. yii在TbGridView的td里面加入相应的下拉选项(转)

    当你需要在一个GridView渲染某一个复杂元素的时候(特别是在这种情况下,这是一个小部件),这就需要你在控制器中调用一个动作.例如你给一个GridView的定义这样的一列: <?php $th ...

  6. C-链表的一些基本操作【创建-删除-打印-插入】

    #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define LEN sizeof(stru ...

  7. 【转载】学习C#的28条建议

    1. 看得懂的书,请仔细看:看不懂的书,请硬着头皮看:2. 别指望看第一遍书就能记住和掌握什么——请看第二遍.第三遍:3. 学习编程的秘诀是:编程,编程,再编程:4. 请把书上的程序例子亲手输入到电脑 ...

  8. 解决 MVC 用户上线下线状态问题

    以前工作项目中就有一个微博类功能,其中也出现了用户在线和离线的问题. 但是因为初入程序猿 使用的是 Session _end 上个事件. Session _end 这个事件不需要怎么解释吧 就是在se ...

  9. 在Silverlight宿主html页面添加按钮无法显示

    在建silverlight应用程序时宿主html中嵌入的silverlight时出现的问题: 预想效果: 实际效果: silverlight填满整个page的所以无法显示html中其他的控件 解决办法 ...

  10. python基础学习笔记第二天 内建方法(s t r)

    python的字符串内建函数 str.casefold()将字符串转换成小写,Unicode编码中凡是有对应的小写形式的,都会转换str.center()返回一个原字符串居中,并使用空格填充至长度 w ...