所有源码及练习题请参考github: https://github.com/fa-hui/cpp-primer-plus.git
本章内容包括:
- for循环
- 表达式和语句
- 递增运算符和递减运算符:++和–
- 组合赋值运算符
- 复合语句(语句块)
- 逗号运算符
- 关系运算符:>、>=、= =、<=、<和!=
- while循环
- typeof工具
- do while循环
- 字符输入方法get()
- 文件尾条件
- 嵌套循环和二维数组
以上内容仅作为本章的脑图,进行重复学习、查漏补缺使用(哪里不会补哪里)
1.for 循环
先上一个实例(1.cpp):
#include <iostream>
using namespace std;
int main(void)
{
int i;
for(i=0; i<5; i++)
cout << "i = " << i << endl;
cout << "Done, now that i = " << i << endl;
return 0;
}
for 循环为执行重复的操作提供了循序渐进的步骤:
1.设置初始值
2.执行测试,看看循环是否应继续进行
3.执行循环操作
4.更新用于测试的值
2.cpp 代码如下:
include <iostream>
using namespace std;
int main(void)
{
cout << "Enter the starting coundown value: ";
int limit;
cin >> limit;
int i;
for(i=limit; i; i--)
cout <<"i = " << i << endl;
cout << "Done, now that i = " << i << endl;
return 0;
}
以上代码中,for中间控制的是表达式,即for( )中存在3个表达式。
Note
cout.setf(ios_base::boolalpha)函数调用可以设置一个标记,该标记命令cout输出的int值显示为true和false,而不是1和0;
3.cpp 代码如下:
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout << "The expression x = 100 has the value: ";
cout << (x=100) << endl;
cout << "Now x = " << x << endl;
cout << "The expression x < 3 has the value: " << endl;
cout << (x < 3) << endl;
cout << (x > 3) << endl;
cout.setf(ios_base::boolalpha);
cout << "The expression x < 3 has the value: " << endl;
cout << (x < 3) << endl;
cout << "The expression x > 3 has the value: " << endl;
cout << (x > 3) << endl;
return 0;
}
2.计算某一个数的阶乘
4.cpp 代码如下:
#include <iostream>
using namespace std;
const int ArSize = 16;
int main(void)
{
long long factorials[ArSize];
factorials[0] = factorials[1] = 1;
for(int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i-1];
for(int i=0; i < ArSize; i++)
cout << i << "! = " << factorials[i] << endl;
return 0;
}
2.1 修改步长
5.cpp 代码如下:
#include <iostream>
using namespace std;
int main(void)
{
cout << "Enter an integer: ";
int by;
cin >> by;
cout << "counting by " << by << endl;
for(int i = 0; i < 100; i = i + by)
cout << i << endl;
return 0;
}
2.2 使用for循环访问字符串(反向输出)
#include <iostream>
using namespace std;
int main(void)
{
cout << "Enter a word: ";
string word;
cin >> word;
for(int i = word.size(); i>=0; i--)
cout <<word[i];
cout << endl;
return 0;
}
2.3简单了解以下++和–
++:递增运算符;–:递减运算符;但需要注意先加和后加的区别,对同一个变量在一条语句中进行多次++,可能结果会不一样。
3. 递增/递减运算符和指针的关系
*++pt、 ++*pt、 (*pt)++、 *pt++
7.cpp 代码如下:
#include <iostream>
using namespace std;
int main(void)
{
double arr[5] = {21.1, 32.8, 23.4, 45.2, 67.4};
double *pt = arr;
cout << "*++pt = " << *++pt << endl;
cout << "++*pt = " << ++*pt << endl;
cout << "(*pt)++ = " << (*pt)++ << endl;
cout << "*pt = " << *pt << endl;
cout << "*pt++ = " << *pt++ << endl;
cout << "*pt = " << *pt << endl;
return 0;
}
4.改变单词的顺序
8.cpp 代码如下:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
cout << "please enter a word: ";
string word;
cin >> word;
int i, j;
char tmp;
for(j = 0, i = word.size()-1; j<i; j++, i--)
{
tmp = word[i];
word[i] = word[j];
word[j] = tmp;
}
cout << word << endl;
return 0;
}
5. C-风格字符串的比较
如:word为一个数组,word == “mate”; 数组名的本意为地址,而字符串也表示地址,即使word中的值等于mate,那么两者也不会相等;
要想比较字符串,C-风格的则应使用strcmp()函数:
9.cpp 代码如下:
#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
char word[5] = "?ate";
for(char ch = 'a'; strcmp(word, "mate"); ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
return 0;
}
5.1 c-风格检测字符串相等或排列顺序
相等:strcmp(str1, str2) == 0;
若str1在str2前:strcmp(str1, str2) > 0;
若str1在str2后:strcmp(str1, str2) < 0;
5.2 比较string类字符串
10.cpp 代码如下:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string word = "?ate";
for(char ch='a'; word != "mate"; ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
return 0;
}
6.while循环
结构:
while(test-condition) //无初始化部分
body
11.cpp 代码如下:
#include <iostream>
using namespace std;
const int ArSize = 20;
int main(void)
{
char name[ArSize];
cout << "Enter your first name please: ";
cin >> name;
cout << "Here is your name: " << endl;
int i = 0;
while(name[i] != '\0')
{
cout << name[i] << " : " << (int)name[i] << endl;
i++;
}
return 0;
}
Notes:1.while循环体可以改为for循环体:
while(test-condition) -> for(; test-condition; )
body body
2.如果循环体中包括continue语句,情况将稍有不同。continue:结束本次循环,执行下一次循环。
在for循环语句中,结束本次循环也就意味着进入 更新条件语句 ;但在while循环语句中,若在continue后有更新条件语句,则会跳过更新条件语句执行test-condition部分;
3.当确定循环次数时,一般使用for循环;在不确定到底要循环多少次时,一般使用while循环语句;
7.while延时循环
7.1 clock()
clock():返回程序开始执行后所用的系统时间(当前的时钟节拍)
clock_t:长整型(也可以说是别名,让编程人员和读者更容易认识这是时钟类型)
早期延时循环代码:
...
long wait = 0;
while (wait < 10000)
wait++;
这种方法在不同的计算机中执行时间可能不同,也可能被篡改。更好的方法就是让系统时钟来完成这种工作;
12.cpp 代码如下:
#include <iostream>
#include <ctime>
using namespace std;
int main(void)
{
cout << "Enter the delay time, in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC;
clock_t start = clock();
while(clock() - start < delay);
cout << "Done!\n";
return 0;
}
CLOCK_PER_SEC:该常量等于每秒钟包含的系统时间单位数;将秒数×CLOCK_PER_SEC,可以得到以系统时间为单位的时间
7.2 创建变量别名
char byte; //变量(字符型)
typeof char byte; //类型的别名,char类型的别名
如创建一个浮点类型别名的指针
typeof float * FLOAT_POINTER;
FLOAT_POINTER pa, pb; //此时pa, pb 都是指针类型
8.基于范围的for循环
简化了一种常见的循环任务
如:
double price[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for(double x:double)
cout << x << endl;
解:x会遍历prices中的所有元素,但不能修改元素中的内容,要想修改元素的内容则需要知道元素的地址,如下:
for(double &x:prices)
x = x * 0.80;
解:符号&表明x是一个引用变量,能让接下来的代码修改数组中的内容
8.1 循环和文本输入
哨兵字符:可作为停止标记
13.cpp 代码如下:
#include <iostream>
using namespace std;
int main(void)
{
char ch;
int count = 0;
cout << "Enter characters, enter # to quit: " << endl;
cin.get(ch);
while(ch != '#')
{
cout << ch;
++count;
cin.get(ch);
}
cout << endl;
cout << count << " characters read " << endl;
return 0;
}
可以明显的看出代码中存在的一种问题,当输入带有空格字符的也会停止运行;这时则需要通过cin.get(char)进行补救上述代码
文件尾条件(EOF:检查文件尾):在ubuntu中输入ctrl+D结束,否则可一直输入:
14.cpp 代码如下
#include <iostream>
using namespace std;
int main(void)
{
char ch;
int count = 0;
cout << "Enter characters, enter # to quit: " << endl;
cin.get(ch);
while(cin.fail() == false)
{
cout << ch;
++count;
cin.get(ch);
}
cout << endl;
cout << count << " characters read " << endl;
return 0;
}
9. 循环嵌套和二维数组
二维数组更像一个表格,如:
int maxtemps[4][5]; //4行5列
maxtemps[0]; //是由5个int型组成的数组
maxtemps[0][0]; //是数组中的第一个元素
15.cpp代码如下:
#include <iostream>
using namespace std;
const int Cities = 5;
const int Years = 4;
int main(void)
{
const char * cities[Cities] =
{
"Gribble city",
"Gribble town",
"New griible",
"San Gribble",
"Gribble Vista"
};
int maxtemps[Years][Cities] =
{
{96, 100, 87, 101, 105},
{96, 98, 91, 107, 104},
{97, 101, 93, 108, 107},
{98, 103, 95, 109, 108}
};
cout << "City: Maximum temperature for 2008 - 2021 " << endl;
for (int city = 0; city < Cities; city++)
{
cout << cities[city] << ":\t";
for(int year = 0; year < Years; year++)
{
cout << maxtemps[year][city] << "\t";
}
cout << endl;
}
return 0;
}
以上为第五章中的全部内容,所有代码和测试题源码都放在了github中。