C++ 笔记-第五章 循环和关系表达式(Stephen Prata第6版)
1.for循环1.1 规则statement1forint_expr; test_expr; update_exprstatement2statement3int_expr 初始化表达式test_expr 测试表达式update_expr 更新表达式for是C关键字编译器不会将其视为函数还会防止函数命名为for示例使用循环来计算并存储前16个阶乘。#include iostream const int ArcSize 16; int main() { long long fractorials[ArcSize]; fractorials[1] fractorials[0] 1LL; for(int i 2; i ArcSize; i) fractorials[i] i*fractorials[i-1]; for(int i0; i ArcSize; i) std::cout i ! fractorials[i] std::endl; return 0; }输出0! 11! 12! 23! 64! 245! 1206! 7207! 50408! 403209! 36288010! 362880011! 3991680012! 47900160013! 622702080014! 8717829120015! 13076743680001.2 扩展修改步长i i by访问字符串word[i]递增、递减aab--, --b却别在于操作数影响一样但影响的时间不同。前缀代表先将数值变化使用新的值计算表达式后缀代表使用当前值计算表达式然后值再变化允许指针操作组合运算、-、*、/、%复合语句允许statement2多语句逗号运算符允许update_expr多语句关系表达式等于运算符和赋值运算符不同mu 4 表达式结果是true或falsemu 4 表达式值是4字符数组和string类字符串比较测试#include iostream #include cstring int main() { using namespace std; char word[5] ?ate; for(char ch a; strcmp(word, mate); ch) { cout word endl; word[0] ch; } return 0; }#include iostream #include string int main() { using namespace std; string word ?ate; for(char ch a; word ! mate; ch) { cout word endl; word[0] ch; } return 0; }输出?ateaatebatecatedateeatefategatehateiatejatekatelate2.while循环2.1 规则statement1whiletest_exprstatement2statement3test_expr 测试表达式while循环是没有初始化和更新部分的for循环注意while循环内语句使用花括号参考案例例1#include iostream const int ArcSize 20; int main() { using namespace std; char name[ArcSize]; cout Your first name, please: ; cin name; cout Here is your name, vertivalized and ASCIIized:\n; int i 0; while(name[i] ! 0) { cout name[i] : int(name[i]) endl; i; } cout Done\n; return 0; }输出Your first name, please: XiaonanguaHere is your name, vertivalized and ASCIIized:X:88i:105a:97o:111n:110a:97n:110g:103u:117a:97Done例2#include iostream const int ArcSize 20; int main() { using namespace std; char name[ArcSize]; cout Your first name, please: ; cin name; cout Here is your name, vertivalized and ASCIIized:\n; int i 0; while(name[i] ! 0) cout name[i] : int(name[i]) endl; i; cout Done\n; return 0; }输出Your first name, please: XiaonanguaHere is your name, vertivalized and ASCIIized:X:88X:88X:88X:88...2.2 扩展延时使用以系统时间单位为单位#include iostream #include ctime int main() { using namespace std; float secs; cout Enter the delay time, in seconds: ; cin secs; clock_t delay secs * CLOCKS_PER_SEC; cout Starting\a\n; clock_t start clock(); while(clock() - start delay) { ; } cout Done\n; return 0; }输出Enter the delay time, in seconds: 5StartingDone类型别名定义方式#define BYTE char预处理器编译程序时用char替换所有的BYTE从而使BYTE成为char的别名typedef char byte使用关键字typedef创建3.do while循环3.1 规则dostatement1whiletest_exprtest_expr 测试表达式例1发现我最爱的数字#include iostream int main() { using namespace std; int n; cout Enter numbers in the range 1-10 to find: ; cout my favourite nmuber\n; cin n; do { cin n; } while (n ! 3); cout Yes, 3 is my favourite.\n; return 0; }输出Enter numbers in the range 1-10 to find: my favourite nmuber243Yes, 3 is my favourite.3.2 扩展有时候你会看到有人这样写代码int i 0; for(;;) { i; if(i 30) break; }也有人这样写代码int i 0; for(;;i) { if(i 30) break; }也有人这样写代码int i 0 do{ i } while 30 i通常编写清晰、容易理解的代码比使用语言的晦涩特性来展示自己的能力更为有用。4.基于范围的for循环对数组或者容器类如vector和array的每个元素执行相同的操作基于范围range-based的for循环例我有一家超市商品价格要打九九折#include iostream int main() { using namespace std; int n; double prices[5] {1, 100, 1000, 10000, 100000}; for(double x : prices) { x * 0.99; cout x endl; } return 0; }输出0.99999909900990005.循环和文本输入循环的重要应用之一就是逐字符读取来自文件或者键盘的文本。5.1 使用原始的cin进行输入循环读取键盘文本输入必须有办法知道何时停止读取一种方法是定义哨兵字符。例#include iostream int main() { using namespace std; char ch; int count 0; cout Enter characters: enter # to quit :\n; cin ch; while(ch ! #) { cout ch; count; cin ch; } cout endl count characters read\n; return 0; }输出Enter characters: enter # to quit :CSDN is fun!#CSDNisfun!10characters read5.2 使用cin.get(char)进行补救通常逐个字符读取输入的程序需要检查每个字符包括空格、制表符和换行符。cin.get(ch)读取输入中的下一个字符即使它是 空格将其赋给变量ch。例#include iostream int main() { using namespace std; char ch; int count 0; cout Enter characters: enter # to quit :\n; cin.get(ch); while(ch ! #) { cout ch; count; cin.get(ch); } cout endl count characters read\n; return 0; }输出Enter characters: enter # to quit :CSDN is fun!#CSDN is fun!12characters read5.3 使用哪个cin.get()cin.get(ch)cin.get(name, ArcSize) name可以是地址名char*ArcSize可以是int类型的整数C的OOP特性出来了允许接收一个ch的输入也允许接收char*和int的输入是不是开始有点意思了5.4 文件尾条件EOF为什么要讨论这个问题原因来自命令行首先很多操作系统支持重定向允许用文件替换键盘输入比如windows下有一个gofish.exe和一个名为fishtale的文本文件可以在命令提示模式下输入下面的命令gofish fishtale这样程序从fishtale文件中获取输入。是Unix和Windows命令提示符模式的重定向运算符其次很多操作系统允许通过键盘来模拟文件尾条件。Unix是CtrlDWindows是CtrlZ和Enter。最后检测到EOFcin会将eofbit和failbit都设置为1。例#include iostream int main() { using namespace std; char ch; int count 0; cout Enter characters:\n; cin.get(ch); while(cin.fail() false) { cout ch; count; cin.get(ch); } cout endl count characters read\n; return 0; }输出Enter characters:CSDN is fun!CSDN is fun!^Z13characters read对于文件输入EOF标记结束就不再读取。对于键盘输入EOF标记结束后有可能会 继续输入此时使用cin.clear(清除EOF标记。6.嵌套循环和二维数组int maxtemps[4][5]5个城市4年的最高温度maxtemps是4个元素的数组每个元素都是5个int的数组嵌套循环适合处理二维数组。