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. 在PHP项目中使用Standford Moss代码查重系统

    Standford Moss 系统是斯坦福大学大名鼎鼎的代码查重系统,它可以查出哪些同学提交的代码是抄袭别人的,从而将提交结果拒之门外.它对一切希望使用该系统的人都是开放的,那么在PHP的项目中如何使 ...

  2. Objective-C特点

    1.兼容性 OC是一种面向对象的C语言,在OC的代码中可以有C和C++语句,它可以调用C函数,也可以通过C++对象访问方法. 2.字符串 OC通常不使用C语言风格的字符串.大多数情况下(CoreFou ...

  3. c# 如何获取项目的根目录

    c# 如何获取项目的根目录 编写程序的时候,经常需要用的项目根目录.自己总结如下 1.取得控制台应用程序的根目录方法     方法1.Environment.CurrentDirectory 取得或设 ...

  4. Php output buffering缓存及程序缓存

       在php中有时为了控制程序的输出显示顺序,提供了output buffering缓存(php自身缓存机制). 若Ob缓存开启,需要输出的就先存在ob缓存里,再到程序缓存里.若没有开启,则直接进入 ...

  5. JDK 环境变量的配置

    小编下载的是 jdk1.6 的版本 具体如何如何安装我相信大家肯定都会的了 这里就不做详细的说明了 , 我的jdk 安装的目录是在  D:\javac 这个目录下 下面我们来配置环境变量 在我的电脑- ...

  6. 通过Eclipse创建SQLite数据库

    import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database ...

  7. javascript绑定时间 含(IE)

    script language = "javascript" type = "text/javascript"> function test(){ win ...

  8. How to get the underlying SSRS Report Query, reset query , add your own ranges and execute report [AX2012]

    Below is the small code snippet to get the underlying query of the SSRS report, reset query, prompt ...

  9. Python核心编程--学习笔记--6--序列(下)列表、元组

    11 列表 类似于C语言的数组,但是列表可以包含不同类型的任意对象.列表是可变类型. 创建列表——手动赋值.工厂函数: >>> aList = [12, 'abc'] >> ...

  10. eval和new Function的区别

    eval和new Function都可以动态解析和执行字符串.但是它们对解析内容的运行环境判定不同. var a = 'global scope' function b(){ var a = 'loc ...