C++Primer第五版 第九章习题答案(51~52)

时间:2023-02-27 14:46:49

51:知识点:综合利用了之前的string的各项额外操作

#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<vector>
#include<forward_list>
using namespace std;

class Date
{
public://class默认是私有继承,记得要加public
unsigned _year;
unsigned _month;
unsigned _day;
void _show()
{
cout<<_year<<"年"<<_month<<"月"<<_day<<"日"<<endl;
}
//构造函数
Date(string);
};

Date::Date(string s)
{
int flag = 0;
string number = "0123456789/";
string coma = ",";
string month;
unsigned pos,pos1,pos2,pos3;
unsigned _pos,_pos1;

/*利用一个判断,现判定怎样初始化*/
if ((pos = s.find_first_not_of(number)) == string::npos)
{
flag = 1;
}
if ((pos = s.find_first_of(coma)) != string::npos)
{
flag = 2;
}

switch (flag)
{
case 1:/*处理1/1/1991的格式*/
pos1 = 0;
pos1 = s.find_first_of("/",pos1);
_day = stoul(s.substr(0,pos1));//先截取目标字符串,再将字符串转化为unsigned
pos2 = ++pos1;
pos1 = s.find_first_of("/",pos1);
_month = stoul(s.substr(pos2,pos1));
pos3 = ++pos1;
_year = stoul(s.substr(pos3,s.size()-1));
break;
case 2:/*处理January 1,1900的格式*/
_pos;
_pos = s.find_first_of(number);
month = s.substr(0,_pos);
//本来想用switch,表达式的结果的类型可以是 整数类型,枚举类型,或者类类型
//(但该类需要有单一的转换到整数类型或(可以是字符类型,但不能是浮点类型、字符串、指针类型等)
if (month == "January ") _month = 1;
if (month == "February ") _month = 2;
if (month == "March ") _month = 3;
if (month == "April ") _month = 4;
if (month == "May ") _month = 5;
if (month == "June ") _month = 6;
if (month == "July ") _month = 7;
if (month == "August ") _month = 8;
if (month == "September ") _month = 9;
if (month == "October ") _month = 10;
if (month == "November ") _month = 11;
if (month == "December ") _month = 12;

_pos1 = ++_pos;
_pos = s.find_first_of(number,_pos);
_day = stoul(s.substr(_pos1-1,_pos));

_year = stoul(s.substr(_pos,s.size()-1));
break;
case 0:/*处理Jan 1 1995的格式*/
_pos;
_pos = s.find_first_of(number);
month = s.substr(0,_pos);
if (month == "Jan ") _month = 1;
if (month == "Feb ") _month = 2;
if (month == "Mar ") _month = 3;
if (month == "Apr ") _month = 4;
if (month == "May ") _month = 5;
if (month == "Jun ") _month = 6;
if (month == "Jul ") _month = 7;
if (month == "Aug ") _month = 8;
if (month == "Sep ") _month = 9;
if (month == "Oct ") _month = 10;
if (month == "Nov ") _month = 11;
if (month == "Dec ") _month = 12;

_pos1 = ++_pos;
_pos = s.find_first_of(number,_pos);
_day = stoul(s.substr(_pos1-1,_pos));

_year = stoul(s.substr(_pos,s.size()-1));
break;
}
}

int main(int argc, char**argv)
{
Date _today("25/2/2017");
_today._show();

Date _tomorrow("January 1,1995");
_tomorrow._show();

Date _2tomorrow("Jan 1 1995");
_2tomorrow._show();

return 0;
}

52:知识点1:stack栈,queue队列,priority_queue优先队列,是标准库的三个顺序容器适配器,书上说的非常的抽象,其实就是接收一种容器,但是操作行为不像该容器了。会需要数据结构的相关知识,建议进行了解。

知识点2:stack栈适配器有其特有操作,pop()—删除栈顶元素、push()—将元素压栈表示其进出栈操作,top()返回栈顶元素,但不弹出。先进后出。

知识点3:queue和priority_queue操作类似,注意该pop()和push()等操作与stack不同。先进先出。


但是题目的描述看着有些迷惑,将括号中的内容弹出不难,但是怎样转换为表达式算出其值?