Accelerated C++学习笔记3—<循环和计数>

时间:2014-05-21 17:15:38   收藏:0   阅读:324

第2章 循环和计数


本节主要利用改进输出问候语的程序来改进如何支持循环和条件分支的。
1、使用循环输出一个周围带框架框住的问候语,且用户自己提供在框架与问候语之间的空格的个数。
<span style="font-family:KaiTi_GB2312;">// lesson2_1.cpp : 定义控制台应用程序的入口点。
//功能:使用循环
//时间:2014.5.8

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	//请求用户输入姓名
	cout << "Please enter your first name:: ";

	//读入用户输入的姓名
	string name;
	cin >> name;
	
	//请求用户输入围住问候语的空白个数
	cout << "Please enter the space size:: ";
	int pad;
	cin >> pad;

	//构造我们将要输出的信息
	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;

	//输出rows行
	//不变式:到目前为止,我们已经输出了r行
	for(int r = 0; r != rows; ++r)
	{
		string::size_type c = 0;

		//不变式:到目前为止,在当前行中我们已经输出c个字符
		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;
}
</span>

运行结果:
bubuko.com,布布扣

2、改写程序,让它在单独的一条输出表达式中输出所有的空白行
<span style="font-family:KaiTi_GB2312;">// lesson2_2.cpp : 定义控制台应用程序的入口点。
//功能:改写代码,程序每次一个字符地输出了大部分的空白行;且让用户自己提供在框架和问候语之间的空格个数
//时间:2014.5.8

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	//请求用户输入姓名
	cout << "Please enter your first name:: ";

	//读入用户输入的姓名
	string name;
	cin >> name;
	
	//请求用户输入围住问候语的空白个数
	cout << "Please enter the space size:: ";
	int pad;
	cin >> pad;

	//构造我们将要输出的信息
	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(greeting.size() + pad * 2, ' ');

	//输出一个空白行,把输出与输入分隔开
	cout << endl;

	//输出rows行
	//不变式:到目前为止,我们已经输出了r行
	for(int r = 0; r != rows; ++r)
	{
		string::size_type c = 0;

		//不变式:到目前为止,在当前行中我们已经输出c个字符
		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;
}

</span>

运行结果:
bubuko.com,布布扣

3、编写一个程序,让它输出一系列的"*"字符,程序输出的这些字符将构成一个正方形,一个长方形,一个三角形
<span style="font-family:KaiTi_GB2312;">// lesson2_3.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序,让它输出一系列的"*"字符,程序输出的这些字符将构成一个正方形,一个长方形,一个三角形

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "**" << endl;
	cout << "**" << endl;

	cout << endl;

	cout << "****" << endl;
	cout << "****" << endl;

	cout << endl;

	cout << "*" << endl;
	cout << "**" << endl;
	cout << "***" << endl;
	cout << "****" << endl;
	cout << "*****" << endl;

	return 0;
}
</span>

运行结果:
bubuko.com,布布扣

4、编写一个程序来依次输出从10~-5的整数
<span style="font-family:KaiTi_GB2312;">// lesson2_4.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序来依次输出从10--5的整数

#include "stdafx.h"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int i = 11;
	while (i > -5 )
	{
		--i;//这里也可以用i--
		cout << i << endl;
	}
	return 0;
}</span>

运行结果:
bubuko.com,布布扣

5、编写一个程序来计算区间[1,10]中所有数值的乘积
<span style="font-family:KaiTi_GB2312;">// lesson2_5.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序来计算区间[1,10]中所有数值的乘积

#include "stdafx.h"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int sum = 1;
	for(int i = 1; i < 10; i++)
	sum = sum * i;
	cout << "sum: "<< sum << endl;

	return 0;
}
</span>

运行结果:
bubuko.com,布布扣

6、编写一个程序,让用户输入两个数值并告知用户在这两个数值中哪一个比较大
<span style="font-family:KaiTi_GB2312;">// lesson2_6.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序,让用户输入两个数值并告知用户在这两个数值中哪一个比较大

#include "stdafx.h"
#include "iostream"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Please enter the first number: ";
	int number1;
	cin >> number1;

	cout << "Please enter the second number: ";
	int number2;
	cin >> number2;
	

	if (number1 >= number2)
	cout << "First is greater" << endl;
	else if (number1 < number2)
		cout << "Second id greater" << endl;
	else
		cout << "Equal!"<< endl;

	return 0;
}
</span>

运行结果:
bubuko.com,布布扣

小结:
1)两个类型:bool  代表真值得内部类型;其值可以是true或者false
string::size_type 无符号整数类型,可以保存任何字符串的长度
2)对于循环中的while,if,if……else……,for等常用的语句这里不做详细分析。大家不懂可以去搜寻些资料。

                                                                                                                                                        ——To_捭阖_youth

Accelerated C++学习笔记3—<循环和计数>,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!