Accelerated C++:通过示例进行编程实践——练习解答(第2章)

2-0. Compile and run the program presented in this chapter.
#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<"input your name:";
	string name;
	cin>>name;

	cout<<endl<<endl;
	const string greet="Hello, "+name+", I am sorry!";

	const int pad=1;
	const int row=pad*2+3;
	const string::size_type col=greet.size()+pad*2+2;

	for(int r=0;r!=row;++r)
	{
		string::size_type c=0;
		while(c!=col)
		{
			if(r==pad+1 && c==pad+1)
			{
				cout<<greet;
				c+=greet.size();
			}
			else
			{
				if(r==0||r==row-1||c==0||c==col-1)
				{
					cout<<"*";
				}
				else
				{
					cout<<" ";
				}
				++c;
			}
		}
		cout<<endl;
	}
	return 0;
}
lyj@qt:~/Desktop$ g++ 2-0.cpp -o 2-0
lyj@qt:~/Desktop$ ./2-0
input your name:tianya
******************************
*                                        *
* Hello,tianya, I am sorry! *
*                                        *
******************************
lyj@qt:~/Desktop$ 

2-1. Change the framing program so that it writes its greeting with no separation from the frame.

Ans:要求输出与框架没有间隔的问候语,我们将2-0程序中pad设置为0,即可。


2-2. Change the framing program so that it uses a different amount of space to separate the sides from the greeting than it uses to separate the top and bottom borders from the greeting.

Ans:修改2-0的pad即可。


2-3. Rewrite the framing program to ask the user to supply the amount of spacing to leave between the frame and the greeting.

Ans:同2-0,仅将const int pad=1,修改为int pad; cin>>pad;即可。


2-4. The framing program writes the mostly blank lines that separate the borders from the greeting one character at a time. Change the program so that it writes all the spaces needed in a single output expression.

#include <iostream>
#include <string>
using namespace std;
int main()
{
	cout<<"input your name:";
	string name;
	cin>>name;

	cout<<endl<<endl;
	const int pad=3;
	const string space(pad,' ');
	const string greet=space+"Hello, "+name+", I am sorry!"+space;
	const string ps(greet.size(),' ');
	const int row=pad*2+3;
	const string::size_type col=greet.size()+2;

	for(int r=0;r!=row;++r)
	{
		string::size_type c=0;
		while(c!=col)
		{
			if(r==pad+1 && c==1)
			{
				cout<<greet;
				c+=greet.size();
			}
			else 
			{
				if(r==0||r==row-1||c==0||c==col-1)
				{
					cout<<"*";
					++c;
				}
				else if(c==1)
				{
					cout<<ps;
					c+=ps.size();
				}
			}
		}
		cout<<endl;
	}
	return 0;
}
lyj@qt:~/Desktop$ g++ 2-0.cpp -o 2-0
lyj@qt:~/Desktop$ ./2-0
input your name:tianya
**********************************
*                                             *
*                                             *
*                                             *
*   Hello, tianya, I am sorry!   *
*                                             *
*                                             *
*                                             *
**********************************
lyj@qt:~/Desktop$ 

2-5. Write a set of "*" characters so that they form a square, a rectangle, and a triangle.正方形和长方形较为简单,仅仅给出三角形示例。

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int r,c;
	cout<<"the r*c you want:";
	cin>>r>>c;

	for(int i=0;i!=r;++i)
	{
	if(i<=c)
	{
		const string sp(c-i,' ');
		const string star(i,'*');
		cout<<sp<<star<<endl;
	}
	}
	return 0;
}
Accelerated C++:通过示例进行编程实践——练习解答(第2章)_第1张图片

2-6. What does the following code do?
int i = 0;
while (i < 10) {
    i += 1;
    std::cout << i << std::endl;

}

Ans:输出1,2,3,4,5,6,7,8,9,10

2-7. Write a program to count down from 10 to -5 .

#include <iostream>
using namespace std;
int main()
{
	signed int n=11;
	while(n>-5)
	{
		n-=1;
		cout<<n<<endl;
	}
	return 0;
}

lyj@qt:~/Desktop$ g++ 2.cpp -o 2
lyj@qt:~/Desktop$ ./2
10
9
8
7
6
5
4
3
2
1
0
-1
-2
-3
-4
-5
lyj@qt:~/Desktop$

2-8. Write a program to generate the product of the numbers in the range [1, 10).

#include <iostream>
using namespace std;
int main()
{
	cout<<"9*9 Table"<<endl<<endl;
	for(int i=1;i<=9;++i)
	{
		for(int j=1;j<=i;++j)
		{
			cout<<i<<"*"<<j<<"="<<i*j<<'\t';
		}
		cout<<endl;
	}
	return 0;
}
lyj@qt:~/Desktop$ g++ 3.cpp -o 3
lyj@qt:~/Desktop$ ./3
9*9 Table
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
lyj@qt:~/Desktop$ 

2-9. Write a program that asks the user to enter two numbers and tells the user which number is larger than the other.
#include <iostream>
using namespace std;
int main()
{
	cout<<"input m,n:";
	int m,n;
	cin>>m>>n;
	cout<<endl;
	if(m>n)
		cout<<"the max is: "<<m;
	else
		cout<<"the max is: "<<n;
	return 0;
}
也可以用:cout<<(m>n?m:n)<<endl;代替上述if语句。

2-10. Explain each of the uses of std:: in the following program:
int main() {
int k =  0;
    while (k != n) {             // invariant: we have written k asterisks so far
        using std::cout;
        cout << "*";
        ++k;
    }
    std::cout << std::endl;      // std:: is required here
    return 0;

}

Ans:cout,cin,endl,string等都来自名称空间std,限定名std::cout指明cout来自std名称空间,之后使用cout即表示std::cout,从而使得程序较为简洁;注意作用域,有效范围从{开始,到}结束。

你可能感兴趣的:(C++,Accelerated,Accelerated,C++习题解答,通过示例进行编程实践)