C++Primer--第六章练习题自做

练习6.4

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int fun(int item) {
	if (item < 0)	return -1;
	int intbegin{ 1 };
	for (int i = 1; i <= item; ++i)
		intbegin *= i;
	return intbegin;
}

int main() {
	int item;
	cout << "Please enter an integer:\n";
	cin >> item;
	cout << "the result is " << fun(item) << endl;

	return 0;
}

练习6.5

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int Abs(int item) {
	return ((item < 0) ? (-item) : item);
}

int main() {
	int item;
	cout << "Please enter a number:\n";
	cin >> item;
	cout << "the result is " << Abs(item) << endl;

	return 0;
}

练习6.7

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int Count_calls() {
	static int Count{ -1 };
	return ++Count;
}

int main() {
	for (int i = 0; i != 10; ++i) {
		cout << Count_calls() << endl;
	}

	return 0;
}

练习6.10

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

void Exchange(int* a, int* b) {
	int item;
	item = *a;
	*a = *b;	*b = item;
	return;
}

int main() {
	int a, b;
	cout << "Please enter two integers a and b:\n";
	cin >> a >> b;
	cout << "Before changing: a = " << a
		<< "  b = " << b << endl;
	Exchange(&a, &b);
	cout << "After changing: a = " << a
		<< "  b = " << b << endl;

	return 0;
}

练习6.17

#include <iostream>
#include <string>
#include <cctype>

using std::cin;
using std::cout;
using std::endl;
using std::string;

bool haxUpp(const string& str);
void chUpp(string& str);

int main() {
	cout << "Please write a string : \n";
	string str;
	getline(cin, str);
	if (haxUpp(str)) {
		cout << "The string you write has uppers" << endl;
		chUpp(str);
		cout << "After changing:\n" << str << endl;
	}
	else {
		cout << "There is no Upper" << endl;
	}

	return 0;
}

bool haxUpp(const string& str) {
	for (int i = 0; i != str.size(); i++) {
		if (isupper(str[i])) {
			return true;
		}
	}
	return false;
}

void chUpp(string& str) {
	for (int i = 0; i != str.size(); i++) {
		if (isupper(str[i])) {
			str[i] += 32;
		}
	}
}

练习6.22

#include <iostream>

using namespace std;

void conCh(int** p0, int** p1) {
	int* item = *p0;
	*p0 = *p1;
	*p1 = item;
}

int main() {
	int p0 = 0;	int* conp0 = &p0;
	int p1 = 1;	int* conp1 = &p1;
	cout << "Before changing *(conp0) = " << *(conp0)
		<< "  *(conp1) = " << *(conp1) << endl;;
	conCh(&conp0, &conp1);
	cout << "After changing *(conp0) = " << *(conp0)
		<< "  *(conp1) = " << *(conp1) << endl;

	return 0;
}

练习6.27

#include <iostream>

using namespace std;

int Sum(initializer_list<int> ad) {
	int sum{ 0 };
	for (auto beg = ad.begin(); beg != ad.end(); beg++) {
		sum += *(beg);
	}
	return sum;
}

int main() {
	int sum = Sum({ 1,2,2,2 });
	cout << sum << endl;

	return 0;
}

练习6.33

编写一个递归函数,输出 vector 对象的内容

#include <iostream>
#include <vector>

using namespace std;

void printVec(vector<int> intVec, unsigned index) {
	if (!intVec.empty() && index < intVec.size()) {
		cout << intVec[index] << endl;
		printVec(intVec, index + 1);
	}
	return;
}

int main() {
	vector<int> item;
	int id, count{ 0 };
	while (count != 8) {
		cin >> id;
		item.push_back(id);
		count++;
	}
	printVec(item, 0);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值