AC++第二章答案

2-1 将pad的值改为0即可,其他不做任何变动。

2-2 将pad分解为行空白数与列空白数,因此在对行进行计数和对列进行计数时将分别进行,不会发生冲突。

2-3 源代码:

#include <iostream>

#include <string>

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

using std::cout;
using std::string;

int main() {

	cout << "Please enter your first name: ";

	string name;

	cin >> name;

	const string greeting = "Hello, " + name + "!";

	int pad_h = 0; //定义上下边界的间隔

	int pad_w = 0; //定义左右边界的间隔

	cout << "Please enter the interval number of the height and the width:"
			<< endl;

	cin >> pad_h >> pad_w;

	const int rows = pad_h * 2 + 3;

	const string::size_type cols = greeting.size() + pad_w * 2 + 2;

	cout << endl;

	for (int r = 0; r != rows; ++r)

	{

		string::size_type c = 0;

		while (c != cols) {

			if (r == pad_h + 1 && c == pad_w + 1) {

				cout << greeting;

				c += greeting.size();

			}

			else {

				if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)

					cout << "*";

				else

					cout << " ";

				++c;

			}

		}

		cout << endl;

	}

	return 0;

}


2-4 源代码:(即定义一个space字符串专门存储空白行的空白字符串,需要注意的是在问候语一行同样需要加上单个空格字符的输入)

#include <iostream>

#include <string>

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

using std::cout;
using std::string;

int main() {

	cout << "Please enter your first name: ";

	string name;

	cin >> name;

	const string greeting = "Hello, " + name + "!";

	cout << "Please enter the internal number of the height and the width: ";

	int pad_h = 0;

	int pad_w = 0;

	cin >> pad_h >> pad_w;

	const int rows = pad_h * 2 + 3;

	const string::size_type cols = greeting.size() + pad_w * 2 + 2;

	const string space = string(greeting.size() + pad_w * 2, ' ');

	cout << endl;

	for (int r = 0; r != rows; ++r)

	{

		string::size_type c = 0;

		while (c != cols) {

			if (r == pad_h + 1 && c == pad_w + 1) {

				cout << greeting;

				c += greeting.size();

			}

			else {

				if (r == 0 || r == rows - 1 ||

				c == 0 || c == cols - 1)

				{

					cout << "*";

					++c;

				}

				else if (r == pad_h + 1)

				{

					cout << " ";

					++c;

				}

				else

				{

					cout << space;

					c += space.size();

				}

			}

		}

		cout << endl;

	}

	return 0;

}


2-5 源代码:(此处只要第一个正方形正确写出,后面直接修改即可,均为修改列数)

#include<iostream>

#include<string>

using namespace std;

int main()

{

	int n = 10;

//下面首先输出正方形,因为在运行中行距大于字符间距,所以此处将字符间距加宽一倍。

//否则直接将space定义为cols-2个,并且还原“* ”为“*”即可。

	int rows = n;

	int cols = n;

	string space = string((cols - 2) * 2, ' ');

	for (int r = 0; r != rows; r++)

	{

		string::size_type c = 0;

		if (r == 0 || r == rows - 1)

		{

			while (c != cols)

			{

				cout << "* ";

				++c;

			}

		}

		else

		{

			cout << "* " << space << "*";

		}

		cout << endl;

	}

//下面输出长方形,只需要改变每行的space宽度以及首末行的宽度即可

	rows = n;

	cols = 2 * n;

	space = string(cols - 2, ' ');

	for (int r = 0; r != rows; r++)

	{

		string::size_type c = 0;

		if (r == 0 || r == rows - 1)

		{

			while (c != cols)

			{

				cout << "*";

				++c;

			}

		}

		else

		{

			cout << "*" << space << "*";

		}

		cout << endl;

	}

//下面输出三角形

	rows = n;

	cols = 1;

	for (int r = 0, cols = 1; r != rows; r++, cols++)

	{

		if (cols < 2)

			cout << "*" << endl;

		else {

			space = string(cols - 2, ' ');

			string::size_type c = 0;

			if (r == rows - 1)

			{

				while (c != cols)

				{

					cout << "*";

					++c;

				}

			}

			else

			{

				cout << "*" << space << "*";

			}

			cout << endl;

		}

	}

	return 0;

}


2-6 输出1到10十个整数,不解释。。。
2-7 源代码:

#include<iostream>

#include<string>

using namespace std;

int main()

{

	int i = 10;

	while (i >= -5) {

		cout << i << endl;

		i -= 1;

	}

	return 0;

}

2-8 源代码:

#include<iostream>

#include<string>

using namespace std;

int main()

{

	int i = 1;

	int product = 1;

	while (i < 10)

	{

		product *= i;

		++i;

	}

	cout << "The product is:" << product;

	return 0;

}

2-9 源代码:(本程序没有考虑到用户可能输入错误的情况,比如用户可能输入字母或者符号等。)

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

int main()
{

	int a = 1;

	int b = 1;

	cout << "Please enter two numbers:" << endl;

	cin >> a >> b;

	if (a > b)

		cout << "Number " << a << " is bigger" << endl;

	else if (b > a)

		cout << "Number " << b << " is bigger" << endl;

	else

		cout << "The two numbers is equal." << endl;

	return 0;

}

2-10 第一个std是调用std名称空间中的cout输出流,第二个std同上,由于第一个std只作用在while函数域内,故此处需重新调用。第三个std是调用std名称空间中的endl函数输出换行符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值