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

第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>

运行结果:

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>

运行结果:

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>

运行结果:

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>

运行结果:

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>

运行结果:

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>

运行结果:

小结:

1)两个类型:bool  代表真值得内部类型;其值可以是true或者false

string::size_type 无符号整数类型,可以保存任何字符串的长度

2)对于循环中的while,if,if……else……,for等常用的语句这里不做详细分析。大家不懂可以去搜寻些资料。

——To_捭阖_youth

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

时间: 2024-08-06 15:43:23

Accelerated C++学习笔记3—<循环和计数>的相关文章

Accelerated C++学习笔记7—&lt;使用顺序容器并分析字符串&gt;

第6章  使用库算法 本章中主要教我们如何使用几个库算法来解决与处理字符串和学生成绩相关的问题. 1.分析字符串 使用一个循环来连接两幅字符图案 for(vector<string>::const_iterator it = bottom.begin(); it != bottom.end(); ++it) ret.push_back(*it);</span> 等价于 ret.insert(ret.end(), bottom.begin(), bottom.end());</

swift学习笔记(七)自动引用计数

与Object-c一样,swift使用自动引用计数来跟踪并管理应用使用的内存.当实例不再被使用时,及retainCount=0时,会自动释放是理所占用的内存空间. 注:引用计数仅适用于类的实例,因为struct和enumeration属于值类型,也就不牵涉引用,所以其存储和管理方式并不是引用计数. 当一个实例被初始化时,系统会自动分配一定的内存空间,用于管理属性和方法.当实例对象不再被使用时,其内存空间被收回. swift中的引用类型分为三种,即Strong强引用,weak弱引用和无主引用unw

Accelerated C++学习笔记1—&lt;开始学习C++&gt;

第0章 开始学习C++ 1.每次学习一个新的语言,大家都是从Hello, world!开始 // lesson0_1.cpp : 定义控制台应用程序的入口点. //功能:编译并运行Hello,world //时间:2014.5.7 #include "stdafx.h" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { std::cout<< &

Accelerated C++学习笔记5—&lt;组织程序和数据&gt;

第4章  组织程序和数据 从前面的学习中,我们可以发现程序并不是我们所想的那么简短,他们都有一个共同的特性,那就是 1)都能解决某些特定类型的问题 2)与其他的大多数工具都互相独立 3)都具有一个自己的名称 C++中提供两种基本的方法来让我们组织大型的程序,函数(子程序)和数据结构. 1.组织计算 1)计算总成绩 子函数grade <span style="font-family:KaiTi_GB2312;">//根据学生的期中考试.期末考试.家庭作业成绩来计算总成绩 do

2015 IOS 学习笔记 for循环 方法练习 ——蓝懿教育

今天学习了for循环以及方法,内容有些复杂,不过万变不离其宗 在以后的学习中容易掌握,因为涉及范围广阔,使用率高.所以多加练习后会掌握. ————————for循环—————————— for循环概念: for循环编程语言中的语句之一,用于循环执行.for循环是开界的,它的一般形式为: for(; <<span se-mark="1">条件表达式>; ) 语句: 初始化总是一个赋值语句, 它用来给循环控制变量赋初值: 条件表达式是一个关系表达式, 它决定什么时候

VBA学习笔记之循环

VBA 中Do while Loop用法如下:VBA中如果不知道重复多少次,使用 Do...Loop 语句.Do...Loop 语句重复执行某段代码直到条件是 true 或条件变成 true.重复执行代码直到条件是 true使用 While 关键字来检查 Do... Loop 语句的条件. 1 2 3 Do While i>10   'some code Loop 如果 i 等于 9,上述循环内的代码将终止执行. 1 2 3 Do   'some code Loop While i>10 这个

Java基础学习笔记 -- 8(循环语句)

1. 循环结构 循环三要素:A.循环变量初值  B.循环条件(boolean值)  C.循环变量增量(自增或者自减) 1) while循环 语法:while ( boolean表达式 ) { 语句块: } 执行:当while后面的表达式成立,则执行语句块内容,直到boolean表达式为false,不再继续执行. while循环是先判断后执行,有可能一次都不会执行. 案例17: 2) do...while循环 语法:do { 语句块: } while ( boolean表达式 ); 执行:先执行一

《python基础教程(第二版)》学习笔记 语句/循环/条件(第5章)

print 'AB', 123 ==> AB 123 # 插入了一个空格print 'AB', 'CD' ==> AB CD # 插入了一个空格print 1,2,3 ==> 1 2 3print (1,2,3) ==> (1, 2, 3)#在脚本中以下ABCD连在一起输出print 'AB',print 'CD' import somemodule #导入模块from somemodule import somefunction #导入函数from somemodule impo

python 学习笔记day02-python循环、文件、函数、模块

循环语句 while 循环 while 循环语法结构 当需要语句不断的重复执行时,可以使用 while 循环 while expression: while_sutie 语句 while_suite 会被连续不断的循环执行,直到表达式的值变成 0 或 False         #!/usr/bin/env python         # -- coding: utf-8 --         sum100 = 0         counter = 1         while count