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. 隐藏DLL

    先来推广一下QQ群:61618925.欢迎各位爱好编程的加入. 在外挂或者病毒中,经常需要隐藏掉自己注入的DLL,以免被发现.下面就是一个隐藏DLL的通用模块,用的时候只需要加入到相关模块中即可. 详 ...

  2. C#语法基础和面向对象编程

    1.C#语法基础 http://www.cnblogs.com/tonney/archive/2011/03/16/1986456.html 2.C#与面向对象基础 很棒的资源,简明扼要,介绍的非常清 ...

  3. 怎么利用CSS3绘制三角形

    最近三角形挺火,很多地方都能碰到,如网页,微信,或者QQ空间的时间轴等地方都能看到,而且这些并不是图片插入进去的,那就需要用CSS来做了 <p class="bbb"> ...

  4. jquery源码学习--iceDog

    /** * @FileName : iceDog * @Author : PheonixHkbxoic * @Mail : hkbxoic@gmail.com * @DateTime : 2016-0 ...

  5. 四、MongoDB的查询

    一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 db.blogs.insert([ { "author": " ...

  6. Android开发之Source无法覆写public void onClick(View v)

    初学Android开发,在为一个按钮[该按钮继承OnClickListener()]写监听时,发现无法在Source中引入public void onClick(View v),当时非常纳闷,平常情况 ...

  7. js控制div动起来

    代码: <html> <head> <title>让div动的测试</title> <script language="javascri ...

  8. lua进阶(一)

    第一章 概览     1.chunks              chunks是一系列语句, lua执行的每一块语句,比如一个文件或者交互模式下的每一行都是一个chunks.      2.全局变量 ...

  9. SQL 查询优化

    优化方法论: 分析实例级的等待. 联系等待和队列. 确定方案. 细化到数据/文件级. 细化到进程级. 优化索引/查询.

  10. C#之委托初步

    传说中的东西,今天兴趣来了,就研究了研究,把大概什么是委托,如何使用委托稍微梳理了一下. 1.什么是委托 首先,Class(类)是对事物的抽象,例如,哺乳动物都是胎生,那么你可以定义一个哺乳动物的基类 ...