C++ Primer(第五版) 9.5.5--9.6节练习

9.50

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	vector<string> int_val = {"123", "456", "-789"};
		
	int sum1 = 0;

	for (auto v : int_val) {
		sum1 += stoi(v);
	}

	cout << "sum1 = " << sum1 << endl;

	vector<string> double_val = {"12.3", "-4.56", "+7.8e-2"};

	double sum2 = 0;

	for (auto v : double_val) {
		sum2 += stod(v);
	}
	
	cout << "sum2 = " << sum2 << endl;

	return 0;
}

9.51


#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>

using namespace std;

const vector<string> month_name1 = {"January", "February", "March",
							   "April", "May", "June", "July", "August",
							   "September", "October", "November", "December"};
								
const vector<string> month_name2 = {"Jan", "Feb", "Mar",
							  "Apr", "May", "Jun", "Jul", "Aug",
								"Sept", "Oct", "Nov", "Dec"};

class date {
friend ostream& operator<<(ostream&, const date&);
public:
	typedef string::size_type	size_type;
	date() = default;
	date(const string &s);
private:
	unsigned year, month, day;
};

inline void format1(const string &ds, const string::size_type &index, unsigned &y, unsigned &m, unsigned &d)
{
	string::size_type day_p = index;					//day position
	string::size_type spa_p = ds.find_first_of(" ");	//first space position
	string::size_type sep_p;							//seperator(between day and year) position 
	string::size_type year_p;				//year position
	const vector<string> *p = &month_name1;
	
	if (index == 4) {
		sep_p = ds.find(" ", day_p);
		year_p = sep_p + 1;
		p = &month_name2;
	} else if (index > 4) {
		sep_p = ds.find(", ", day_p);
		year_p = sep_p + 2;
	} else {
		throw invalid_argument("month error");
	}
	
	//check the format
	if (spa_p == string::npos || sep_p == string::npos || day_p - spa_p != 1 || (sep_p - day_p != 1 && sep_p - index != 2))
		throw invalid_argument("fromat error");
	auto year_size = ds.size() - year_p;
	string::size_type pos;
	if (year_size <= 0 || (pos = ds.find_first_not_of("0123456789", year_p)) != string::npos)
		throw invalid_argument("year error");

	//get month value
	auto month = ds.substr(0, spa_p);
	unsigned i = 0, j = 0, month_len = 0;
	for (; i < 12; i++) {
		month_len = (*p)[i].size();
		for (j = 0; j < month_len; j++) {
			if (month[j] != (*p)[i][j])
				break;
		}
		if (j == month_len) {
			m = ++i;
			break;
		}		
	}
	if (i == 12)
		throw invalid_argument("month error");
	
	d = stoi(ds.substr(day_p, sep_p - day_p));
	y = stoi(ds.substr(year_p, ds.size() - year_p));
}

inline void format2(const string &ds, unsigned &y, unsigned &m, unsigned &d)
{
	string::size_type sep1 = ds.find_first_not_of("0123456789");
	string::size_type sep2 = ds.find_first_not_of("0123456789", sep1 + 1);
	
	// check the seperator
	if (sep1 == string::npos || sep2 == string::npos || ds[sep1] != '/' || ds[sep2] != '/')
		throw invalid_argument("format error");
	
	// check the year
	string::size_type pos = ds.find_first_not_of("0123456789", sep2 + 1);
	if (pos != string::npos)
		throw invalid_argument("year error");
	
	string day = ds.substr(0, sep1);
	string month = ds.substr(sep1 + 1, sep2 - sep1 - 1);
	string year = ds.substr(sep2 + 1, ds.size() - sep2 - 1);

	d = stoi(day);
	m = stoi(month);
	y = stoi(year);
}

date::date(const string &ds)
{
	string::size_type index;
	
	if ((index = ds.find_first_of("0123456789")) == string::npos)
		throw invalid_argument("illegal date");
	
	if (index > 0) {		// use month name
		format1(ds, index, year, month, day);
	} else {				//use month number
		format2(ds, year, month, day);
	}
}

ostream& operator<<(ostream& out, const date &d)
{
	out << d.year << " " << d.month << " " << d.day << endl;
	return out;
}

int main()
{
	string ds1 = {"February 1, 2014"};
	string ds2 = {"Jan 15 1900"};
	string ds3 = {"28/8/1985"};
	
	try {
		date d1(ds1);
		date d2(ds2);
		date d3(ds3);
		cout << d1;
		cout << d2;
		cout << d3;
	} catch (invalid_argument e) {
		cout << e.what() << endl;
	}
	
	return 0;
}

9.52

#include <iostream>
#include <string>
#include <stack>
#include <stdexcept>

using namespace std;

void handle(stack<char> &s)
{
	string exp;

	if (s.empty())
		throw invalid_argument("format error");

	while (!s.empty() && s.top() != '(') {
		auto c = s.top();
		exp.insert(exp.begin(), c);
		s.pop();
	}
	
	if (!s.empty()) {
		if (s.top() == '(')
			s.pop();
		else 
			throw invalid_argument("format error");
	}
	
	if (exp[0] != '-') 
		exp.insert(0, 1, '+');
	
	double val = 0, result = 0;
	string::size_type pos = 0, prev = 0;
	while ((pos = exp.find_first_of("+-", prev + 1)) != string::npos) {
		val = stod(exp.substr(prev, pos));
		result += val;
		prev = pos;
	}
	
	val = stod(exp.substr(prev, exp.size()-prev));
	result += val;

	if (!s.empty()) {
		if (s.top() == '+' && (result < 0))
			s.pop();
		else if (s.top() == '-' && (result < 0)) {
			s.pop();
			s.push('+');
			result = 0 - result;
		}
	}
	
	string ret = to_string(result);
	for (auto p = 0; p < ret.size(); p++)
		s.push(ret[p]);
}

double calculate(stack<char> &s)
{
	string exp;
	
	if (s.empty())
		throw invalid_argument("format error");
	
	//生成表达式
	while (!s.empty()) {
		auto c = s.top();
		exp.insert(exp.begin(), c);
		s.pop();
	}
	
	//计算结果
	double val = 0, result = 0;
	string::size_type pos = 0, prev = 0;
	while ((pos = exp.find_first_of("+-", prev + 1)) != string::npos) {
		val = stod(exp.substr(prev, pos));
		result += val;
		prev = pos;
	}
	val = stod(exp.substr(prev, exp.size()-prev));
	result += val;
	
	return result;
}

int main()
{
	string exp;
	stack<char> s;
	string::size_type i = 0;
	
	cout << "input expression: " << endl;
	getline(cin, exp);
	
	while (i < exp.size()) {
		
		if (exp[i] == ')') {
			handle(s);
		} else {
			s.push(exp[i]);
		}
		i++;
	}
	
	auto ret = calculate(s);
	
	cout << "result = " << ret << endl;

	return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值