/**
* 书本:【ThinkingInC++】
* 功能:实现一个猜谜的游戏
* 时间:2014年10月8日21:54:44
* 作者:cutter_point
*/
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<"自己想出来一个数字,在1到100之间的,我来猜猜你的心思"<<endl
<<"我将不断地猜测,知道猜到为止,但是你每次都要告诉我是高了还是低了"<<endl;
int low=1, high=100;
bool guessed=false;
while(!guessed) //只要没有猜到,那么guessed就是false,那就不断执行
{
//如果使用者玩阴的
if(low > high)
{
cout<<"fuck you! 能不能好好玩游戏,根本不纯在这个数字"<<endl;
return EXIT_FAILURE;
}
int guess=(low+high)/2; //得到中间的值
cout<<"我猜这个数字是:"<<guess<<". ";
cout<<"告诉我是高了(High)还是低了(Low),还是说我猜中了?(Equal)"<<endl;
string response;
cin>>response;
switch(toupper(response[0]))
{
case 'H':
high=guess-1;
break;
case 'L':
low=guess+1;
break;
case 'E':
guessed=true;
break;
default:
cout<<"亲,您输入的数据有误!"<<endl;
continue;
}
}
cout<<"YES I got it!!!"<<endl;
return EXIT_SUCCESS; //返回一个cstdlib里面的一个宏,表示程序的执行失败
}
/**
* 书本:【ThinkingInC++】
* 功能:时间类的构建
* 时间:2014年10月8日21:55:09
* 作者:cutter_point
*/
#ifndef DATE1_H_INCLUDED
#define DATE1_H_INCLUDED
#include <string>
class Date
{
public:
struct Duration //表示时间的一个句柄结构
{
int years;
int months;
int days;
//构造函数
Duration(int y, int m, int d) : years(y), months(m), days(d) {}
}*dt;
Date();
Date(int year, int month, int day) { dt=new Duration(year, month, day); }
Date(const std::string&);
int getYear() const { return dt->years; }
int getMonth() const { return dt->months; }
int getDay() const { return dt->days; }
std::string toString() const;
friend bool operator<(const Date&, const Date&);
friend bool operator>(const Date&, const Date&);
friend bool operator<=(const Date&, const Date&);
friend bool operator>=(const Date&, const Date&);
friend bool operator==(const Date&, const Date&);
friend bool operator!=(const Date&, const Date&);
friend Duration duration(const Date&, const Date&);
};
#endif // DATE1_H_INCLUDED
/**
* 书本:【ThinkingInC++】
* 功能:测试时间类
* 时间:2014年10月8日21:55:33
* 作者:cutter_point
*/
#include <iostream>
#include "Date1.h"
using namespace std;
int nPass=0, nFail=0;
void test(bool t) { if(t) nPass++; else nFail++; } //用来测试的函数
int main()
{
Date mybday(1951, 10, 1);
test(mybday.getYear() == 1951);
test(mybday.getMonth() == 10);
test(mybday.getDay() == 1);
cout<<"Passed: "<<nPass<<", Failed: "<<nFail<<endl;
return 0;
}
原文地址:http://blog.csdn.net/cutter_point/article/details/39900557