第2章习题 Accelerate C++ 学习笔记6

//0
#include<iostream>
#include<string>

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

int main(int argc, char const *argv[])
{
	cout << "Please enter your first name:";
	string name;
	cin >> name;

	const string greeting = "Hello, " + name + "!";//构造将要输出的信息

	const int pad = 1;//空白行的个数
	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;

	cout << endl;
	for (int r = 0; r != rows; ++r)
	{
		string::size_type  c = 0;//目前已经输出的列数
		while(c != cols){
			if(r == pad + 1 && c == pad + 1){
				cout << greeting;
				c += greeting.size();
			}else{
				if(r == 0 || r == rows - 1 ||
					c == 0 || c == cols - 1) //输出边框第0行和最后一行以及第0列和最后一列
					cout << "*";
				else
					cout << " ";
				++c;
			}
		}
		cout << endl;
	}
	return 0;
}
//1
#include<iostream>
#include<string>

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

int main(int argc, char const *argv[])
{
	cout << "Please enter your first name:";
	string name;
	cin >> name;

	const string greeting = "Hello, " + name + "!";//构造将要输出的信息

	const int pad = 1;//空白行的个数
	const int rows = 3;
	const string::size_type cols = greeting.size() + 2;

	cout << endl;
	for (int r = 0; r != rows; ++r)
	{
		string::size_type  c = 0;//目前已经输出的行数
		while(c != cols){
			if(r == pad  && c == pad ){
				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
#include<iostream>
#include<string>

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

int main(int argc, char const *argv[])
{
	cout << "Please enter your first name:";
	string name;
	cin >> name;

	const string greeting = "Hello, " + name + "!";//构造将要输出的信息

	const int pad = 3;//空白行的个数
	const int rows = 3;
	const string::size_type cols = greeting.size() + 2;

	cout << endl;
	for (int r = 0; r != rows; ++r)
	{
		string::size_type  c = 0;//目前已经输出的行数
		while(c != cols){
			if(r == pad  && c == pad ){
				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;
}
//3
#include<iostream>
#include<string>

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

int main(int argc, char const *argv[])
{
	cout << "Please enter your first name:";
	string name;
	cin >> name;

	const string greeting = "Hello, " + name + "!";//构造将要输出的信息

	int pad;//空白行的个数
	cin >> pad;//输入空格数

	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;

	cout << endl;
	for (int r = 0; r != rows; ++r)
	{
		string::size_type  c = 0;//目前已经输出的行数
		while(c != cols){
			if(r == pad + 1 && c == pad + 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;
}
//4
#include<iostream>
#include<string>

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

int main(int argc, char const *argv[])
{
	cout << "Please enter your first name:";
	string name;
	cin >> name;

	const string greeting = "Hello, " + name + "!";//构造将要输出的信息

	const int pad = 1;//空白行的个数
	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;
	const string spaces = string(cols - 2,' ');

	cout << endl;
	for (int r = 0; r != rows; ++r)
	{
		string::size_type  c = 0;//目前已经输出的行数
		while(c != cols){
			if(r == pad + 1 && c == pad + 1){
				cout << greeting;
				c += greeting.size();
			}else{
				if(r == 0 || r == rows - 1 ||
					c == 0 || c == cols - 1) {
					cout << "*";
					++c;
				}
				else if (r == pad + 1){
					cout << " ";
					++c;
				}else{
					cout << spaces;
					c += spaces.size();
				}
			}
		}
		cout << endl;
	}
	return 0;
}

运行结果运行结果

//5
#include<iostream>
using std::cout;
using std::endl;
using std::string;

int main(int argc, char const *argv[])
{
	int rows = 10;//行数,正方形的行数用列数表示
    int cols1 = rows * 2;//长方形的列数
	// for (int i = 0; i != rows; ++i)//正方形
	// {
	// 	string::size_type c = 0;
	// 	while(c != rows){
	// 		if(i ==  0 || i == rows - 1 || 
	// 				c == 0 || c == rows - 1){
	// 			cout << "*";
	// 		}else{
	// 			cout << " ";
	// 		}
	// 		c++;
	// 	}
	// 	cout << endl;
	// }

	// for (int i = 0; i != rows; ++i)//长方形
	// {
	// 	string::size_type c = 0;
	// 	while(c != cols1){
	// 		if(c == 0 || c == cols1 - 1 ||
	// 			i == 0 || i == rows - 1){
	// 			cout << "*";
	// 		}else{
	// 			cout << " ";
	// 		}
	// 		c++;
	// 	}
	// 	cout << endl;
	// }


/*输出三角形 
 *
***/
	for (int i = 0; i != rows; ++i)
	{
		int m = rows - (i + 1);
		while(m != 0){//输出空格,
				cout << " ";
				m--;
			}

		int j = 2 * (i + 1) - 1;
		while(j != 0){//输出*,1,3,5.。。。
			cout << "*";
			j--;
		}
		cout << endl;

	}
	return 0;
}

运行结果
运行结果

//6
#include<iostream>

int main(int argc, char const *argv[])
{
	int i = 0;
	while(i < 10){//按序输出1到10,每输出一个数并换行
		i += 1;
		std::cout << i << std::endl;
	}	
	return 0;
}
//7
#include<iostream>
using std::cout;

int main(int argc, char const *argv[])
{
	for (int i = 10; i != -6; --i)
	{
		cout << i << " ";
	}
	cout << std::endl;
	return 0;
}
//8
#include<iostream>
using std::cout;

int main(int argc, char const *argv[])
{
	int res = 1;;
	for (int i = 1; i != 10; ++i)
	{
		res *= i;
	}
	cout << res << std::endl;
	return 0;
}
// //9
#include<iostream>
using std::cin;
using std::cout;
int main(int argc, char const *argv[])
{

	cout << "Please enter two number:";
	double num1 ,num2;
	cin  >> num1 >> num2;

	if(num1 >= num2) 
		cout << num1;
	else 
		cout << num2; 	
	
	cout << std::endl;

	return 0;
}
//10
#include<iostream>

int main(int argc, char const *argv[])
{
	int k = 0;
	while(k != 10){//连续输出10个*,然后换行
		using std::cout;//将cout定义为std::cout的代替名
		cout << "*";
		++k;
	}
	std::cout << std::endl; //标准库中的名称
	return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页