Essential c++ 课后练习4.4

题目:一份“用户概况记录(user profile)”内含以下数据:登录记录、实际姓名、登录次数、猜过次数、猜对次数、等级——包括初级、中级、高级、专家,以及猜对百分比(可实时计算获得,或将其值储存起来备用)。请写出一个名为UserProfile的class,提供以下操作:输入、输出、相等测试、不相等测试。其constructor必须能够处理默认的用户等级、默认的登录名称(“guest”)。对于同样名为guest的多个用户,你如何宝恒每个guest有他独有的登录会话,不会与其他人混淆?

知识点:class定义(写数据cout,读取数据cin),构造函数初始化值,重载操作符operator,重载输入输出流 ostream& operator<< 和istream& operator>>,枚举变量定义enum, 类的private里的static数据成员需要初始化。。

#include<iostream>
#include<string>
#include<map>
#include<iterator>
using namespace std;

class UserProfile{
public:
	//定义枚举类型ulevel,枚举元素作为常量,是有值的,分别为0,1,2,3,4.....
	enum ulevel {Beginner,Intermediate,Advanced,Guru };   
	//"有参无默认值"初始化,用于有名字和等级的客户
	UserProfile(string login, ulevel = Beginner);  
	//默认构造函数初始化,用于默认的guest用户初始化
	UserProfile();

	bool operator==(const UserProfile &);
	bool operator!=(const UserProfile &rhs);

	//以下函数用来读取数据
	string login() const { return _login; }
	string user_name() const{ return _user_name; }
	int login_count() const { return _login_count;}
	int guess_count() const { return _guess_count; }
	int guess_correct() const { return _guess_correct; }
	string level() const;
	double guess_average() const;

	//以下函数用来写入数据
	void reset_login(const string &val) { _login = val; }
	void user_name(const string &val) { _user_name = val; }

	void reset_level(const string &level); 
	//void reset_level(ulevel newlevel) { _user_level = newlevel; }

	void reset_login_count(int val) { _login_count = val; }
	void reset_guess_count(int val) { _guess_count = val; }
	void reset_guess_correct(int val){ _guess_correct = val; }

	void bump_login_count(int cnt = 1){ _login_count += cnt; }
	void bump_guess_count(int cnt = 1){ _guess_count += cnt; }
	void bump_guess_correct(int cnt = 1){ _guess_correct += cnt; }

private:
	string _login;
	string _user_name;
	int _login_count;
	int _guess_count;
	int _guess_correct;
	ulevel _user_level;

	static map<string, ulevel> _level_map;
	static void init_level_map(); 
	//static string guest_login();

};

//开始定义函数

//定义猜对百分比函数
inline double UserProfile::guess_average() const
{
	return _guess_count ? double(_guess_correct) /double(_guess_count) * 100 : 0.0;
}

//"有参无默认值"初始化,用于有名字和等级的客户
inline UserProfile::UserProfile(string  login, ulevel level)
:_login(login), _user_level(level), _login_count(1), _guess_count(0), _guess_correct(0){}

//默认构造函数初始化,用于默认的guest用户初始化
//guest的等级默认设定为Beginner
#include <cstdlib>
inline UserProfile::UserProfile()
: _login("guest"), _user_level(Beginner), _login_count(1), _guess_count(0), _guess_correct(0)
{
	static int id = 0;
	char buffer[16];
	_itoa_s(id++, buffer, 10);//C语言中的函数,将整型值转换为字符串
	_login += buffer;
}

//定义两个重载操作符函数,rhs:right hand side
//本例中没用到
inline bool UserProfile::operator==(const UserProfile &rhs)
{
	if (_login == rhs._login && _user_name == rhs._user_name)
		return true;
	return false;
}

inline bool UserProfile::operator!=(const UserProfile &rhs)
{
	return  !(*this == rhs);
}

//输出流重载
ostream& operator<<(ostream &os, const UserProfile &rhs)
{// 输出格式:qing Beginner 12 100 10 10%
	os << rhs.login() << ' '
		<< rhs.level() << ' '
		<< rhs.login_count() << ' '
		<< rhs.guess_count() << ' '
		<< rhs.guess_correct() << ' '
		<< rhs.guess_average() << '%'<<endl;
	return os;  //不要忘了要返回输出量os
}

//静态static数据成员初始化
map<string, UserProfile::ulevel> UserProfile::_level_map;
void UserProfile::init_level_map()
{
	_level_map["Beginner"] = Beginner;          //等价于dic={"Beginner":0,"Intermediate":1,"Advanced":2,"Guru":3}
	_level_map["Intermediate"] = Intermediate;
	_level_map["Advanced"] = Advanced;
	_level_map["Guru"] = Guru;
}

//定义level函数,返回的是等级的字符串string形式。
inline string UserProfile::level() const
{
	static string _level_table[] = { "Beginner", "Intermediate", "Advanced", "Guru" };
	return _level_table[_user_level]; //枚举元素作为常量,是有值的,分别为0, 1, 2, 3, 4.....
}

//定义reset_level函数,得到_user_level参数
inline void UserProfile::reset_level(const string &level)
{
	map<string, ulevel>::iterator it;
	if (_level_map.empty())
		init_level_map();
	//确保level的确代表一个可识别的用户等级
	_user_level = ((it = _level_map.find(level)) != _level_map.end())
		? it->second : Beginner;
}


//输入流重载
istream& operator>>(istream &is, UserProfile &rhs)
{
	string login, level;
	is >> login >> level;

	int lcount, gcount, gcorrect;
	is >> lcount >> gcount >> gcorrect;
	
	rhs.reset_login(login);
	rhs.reset_level(level);
	rhs.reset_login_count(lcount);
	rhs.reset_guess_count(gcount);
	rhs.reset_guess_correct(gcorrect);

	return is;
}

int main()
{
	UserProfile measure1;
	cout << measure1;

	UserProfile measure2;
	cout << measure2;

	UserProfile measure3("Qing", UserProfile::Advanced);
	cout << measure3;
	measure3.bump_login_count();
	measure3.bump_guess_count(28);
	measure3.bump_guess_correct(20);
	cout << measure3;

	UserProfile measure4;  
	cin >> measure4;
	cout << measure4;

	system("pause");
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<>开始,短小精悍,可以对c++能进一步了解其特性 以<>作字典和课外读物,因为太厚不可能一口气看完 level 2 然后从<>开始转职,这是圣经,请遵守10诫,要经常看,没事就拿来翻翻 接着是<>,个人认为Herb Sutter主席大人的语言表达能力不及Scott Meyers总是在教育第一线的好 顺下来就是<>和<>,请熟读并牢记各条款 当你读到这里,应该会有一股升级的冲动了 level 3 <>看过后如一缕清风扫去一直以来你对语言的疑惑,你终于能明白compiler到底都背着你做了些什么了,这本书要细细回味,比较难啃,最好反复看几遍,加深印象 看完上一本之后,这本<>会重演一次当年C++他爹在设计整个语言过程中的历程 level 4 <>是stl的字典,要什么都可以查得到 学c++不能不学stl,那么首先是<>,它和圣经一样是你日常行为的规范 <>让你从oo向gp转变 光用不行,我们还有必要了解stl的工作原理,那么<>会解决你所有的困惑 level 5 对于c++无非是oo和gp,想进一步提升oo,<>是一本主席这么多年的经验之谈,是很长esp的 一位stl高手是不能不去了解template的,<>是一本百科全书,足够你看完后对于gp游刃有余 <>是太过聪明的人写给明眼人看的 好书有很多,不能一一列举 以上我的读书经历,供各位参考。接下来的无非就是打怪练级,多听多写多看;boost、stl、loki这些都是利器,斩妖除魔,奉劝各位别再土法练钢了。 at last,无他,唯手熟尔。 忘了一本《thinking in C++》 也是经典系列之一 <>这本圣经的作者Scott Meyesr在给<>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所说的任何事情,也许很快就会被认为是陈旧的、肤浅的、甚至是完全错的。 就我所知,template的世界还在变化,速度之快就像我1995年回避写它的时候一样。从发展的速度来看,我可能永远不会写有关template的技术书籍。幸运的是一些人比我勇敢,Andrei就是这样一位先锋。我想你会从此书得到很多收获。我自己就得到了很多——Scott Meyers September2000。 并且,Scott Meyers 在最近的Top5系列文章中,评价C++历史里面最重要5本书中、把Modern C++ Design列入其中,另外四本是它自己的effective c++、以及C++ Programming Language、甚至包括《设计模式》和《C++标准文档》。 显然,Scott Meyers已经作为一个顶尖大师的角度承认了<>的价值。 并且调侃地说,可以把是否使用其中模板方法定义为,现代C++使用者和非现代C++使用者,并且检讨了自己在早期版本Effective对模板的忽视,最后重申在新版本Effective第七章节加入大量对模板程序设计的段落,作为对这次失误的补偿。 并且,在这里要明确的是<>并不是一本泛型编成的书,也不是一本模板手册。其中提出了基于策略的设计方法,有计划和目的的使用了模板、面向对象和设计模式。虽然Andrei本人对模板的研究世界无人能敌,但对其他领域的作为也令人赞叹。 任何做游戏的人都不能忽视OpenAL把,你在开发者的名单里能看到Loki的名字:) 最近很忙,无时间写文章,小奉献一下书籍下载地址。虽然经过验证,但是不感肯定各位一定能下: 中文 http://www.itepub.net/html/ebookcn/2006/0523/40146.html 英文 http://dl.njfiw.gov.cn/books/C/Essential%20C
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值