Careercup - Google面试题 - 5692127791022080
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的更多相关文章
- Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10 题目链接 原题: Given a dictionary, and a list of letters ( or consider as a string), find ...
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
随机推荐
- [转载]《C++0x漫谈》系列之:多线程内存模型
<C++0x漫谈>系列之:多线程内存模型 By 刘未鹏(pongba) 刘言|C++的罗浮宫(http://blog.csdn.net/pongba) <C++0x漫谈>系列导 ...
- 【项目相关】MVC中将WebUploader进行封装
昨天初步将webuploader用在了项目中,但紧接着就发现了一个问题,每一个页面都需要写那么多初始化webUploader的代码,这样肯定是我不能接受的,于是花了半天时间对该控件进行了封装. 最后, ...
- CentOS6.X安装vsftpd服务
#-----------------CentOS6.X安装VSFTPD服务 #! /bin/sh #1.关闭selinux setenforce 0 sed -i 's/enforcing/disab ...
- VS2008使用技巧及快捷键大全
VS2008技巧,非常实用,非常提高效率. 1,Visual Studio 2008自带的1000多个 Windows 系统使用的各种图标.光标和动画文件在Visual Studio 2008的安装目 ...
- ADO .NET 链接 增删改查
ADO.NET: 数据访问技术 就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中也可以将数据库中的数据提取到内存中供程序调用 所有数据访问技术的基础 连 ...
- centos6.7下编译安装lamp环境
编译C源代码: 前提:提供开发工具及开发环境 通过“包组”提供开发组件,CentOS 6: "Development Tools", "Server Platform D ...
- 共享内存shared pool (5):详解一条SQL在library cache中解析
前面介绍的 shared pool,library cache结构,都是为了说明一条SQL是如何被解析的.先看下面的图: 图中涉及的各结构简单介绍 父HANDLE,里面有父游标堆0的地址.. 父游标堆 ...
- STL之迭代器
容器支持的迭代器类型 STL Container Type of Iterators Supported vector random access iterators 元素严格有序(类似数组 ...
- 将python2.7+django1.10部署到SAE上
首先我想说的是我为什么选择SAE呢?本人学生一枚,没钱.然后sae好像又有免费的一定限额,所以我就选了它. 期间曲折颇多,实在不是三言两语所能道情的.各种百度,谷歌,最后所幸成功了,幸哉! 主要参考了 ...
- C基础 那些年用过的奇巧淫技
引言 - 为寻一颗明星 为要寻一颗明星 徐志摩 1924年12月1日<晨报六周年纪念增刊> 我骑著一匹拐腿的瞎马, 向著黑夜里加鞭:—— 向著黑夜里加鞭, 我跨著一匹拐腿的瞎马.// 我冲 ...