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. phalcon框架学习之view

    phalcon框架的view分多级:全局-控制器视图-动作视图.视图由上到下,按层级嵌套. 全局视图 默认全局视图为Views/index.html,所有的页面展示时,默认调用此页面,在这个页面中必须 ...

  2. InnoDB 存储引擎—索引

    1.引言         InnoDB 存储引擎支持以下几种觉的索引:             1.1    B+ 树索引 (平衡树索引)             1.2    全文索引       ...

  3. VMware下安装的Mac OS X如何修改显示分辨率

    VMware下安装的Mac OS X如何修改显示分辨率   我在Win7下利用VMware安装了苹果的Mac OS,安装成功启动后,发现分辨率为1920*1080,而宿机的分辨率是1366*768,我 ...

  4. 显示或隐藏一个Grid

    The Rowset class contains two methods that can be used to show and hide all rows: ShowAllRows() Hide ...

  5. mutex 简单介绍

    “mutex”是术语“互相排斥(mutually exclusive)”的简写形式,也就是互斥量. 当两个或更多线程需要同时访问一个共享资源时,系统需要使用同步机制来确保一次只有一个线程使用该资源.M ...

  6. 前端构建工具grunt

    简单配置grunt 配置gulp还是grunt都是在node的环境下安装的,所以在这之前保证你的node环境已经安装好了! -------------------------------------- ...

  7. C# MongoDB--时区问题(差了8小时)

    原因:MongoDB中存储的时间是标准时间UTC +0:00C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTimeOptions(Kind = Da ...

  8. php 递归 适合刚刚接解递归的人看

    递归,就是自己调用自己,当满足某条件时层层退出(后进先出). --------------------------------------------------------------------- ...

  9. MySQL: InnoDB 还是 MyISAM?

    MyISAM存储引擎 MyISAM是 默认存储引擎.它基于更老的ISAM代码,但有很多有用的扩展.MyISAM存储引擎的一些特征:·      所有数据值先存储低字节.这使得数据机和操作系统分离.二进 ...

  10. ajax返回值中有回车换行、空格解决方法

    最近在写一个页面,用jquery ajax来实现判断,刚写好测试完全没有问题,过了两天发现出现问题,判断不成了.后来发现所有alert出来的返回值前面都会加若干换行和空格.(至今不明白,同一台电脑,同 ...