[C++ Primer Plus] 第5章、循环和关系表达式(一)程序清单——指针自加减优先级
程序5.4
1 #include<iostream> 2 using namespace std; 3 4 const int Size = 16; 5 6 void main() { 7 long long factorials[Size]; 8 factorials[1] = factorials[0] = 1LL; 9 for (int i = 2; i < Size; i++) 10 { 11 factorials[i] = i*factorials[i - 1]; 12 } 13 for (int i = 0; i < Size; i++) 14 { 15 cout << i << "!=" << factorials[i] << endl; 16 } 17 system("pause"); 18 }
factorials阶乘函数
递增/递减运算符和指针
前缀递增递减和*优先级相同,从右到左;
后缀递增递减比前缀优先级高,从左到右。
比如
int arr[5] = { 1,3,5,7,9 }; int *p = arr;
*++p:p先自+,然后*p,最终为3
++*p:先*,然后再++,最终为2
*p++:值为arr[0],即1,该语句执行完毕后,p指向arr[1](因为后缀优先级高,所以等价于*(p++),自增p并返回自增之前指向的值)
程序5.8
1 #include<iostream> 2 using namespace std; 3 4 void main() { 5 double sum = 0.0, number; 6 cout << "The Amazing Accounto will sum and average five numbers for you.." << endl; 7 cout << "Please enter five values: " << endl; 8 for (int i = 1; i < 6; i++) 9 { 10 cout << "Value " << i << ":"; 11 cin >> number; 12 sum += number; 13 } 14 cout << "Sum=" << sum << endl; 15 cout << "Average=" << sum/5 << endl; 16 17 system("pause"); 18 }
程序5.9(反转字符串)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 void main() { 6 cout << "Enter a word:"; 7 string word; 8 cin >> word; 9 char tmp;//tmp也可以在循环体内部定义,这样的话将在每次循环中都分配和释放,效率稍微慢一点 10 for (int j = 0,i=word.size()-1; j < i; ++j,--i)//word.size()不包括‘\0’,前缀递增递减在某些情况下比后缀递增递减效率更高 11 { 12 tmp = word[i]; 13 word[i] = word[j]; 14 word[j] = tmp; 15 } 16 cout << word << endl; 17 18 system("pause"); 19 }
程序5.14(延时等待)
1 #include<iostream> 2 #include<ctime> 3 using namespace std; 4 5 void main() { 6 cout << "Enter the delay time,in seconds:"; 7 float sec; 8 cin >> sec; 9 clock_t delay = sec*CLOCKS_PER_SEC;//CLOCKS_PER_SEC每秒钟包含的系统时间单位数。 秒数*CLOCKS_PER_SEC=系统时间 10 cout <<"starting\a"<< endl; 11 clock_t start = clock(); 12 while (clock() - start < delay)//当不满足条件时跳出循环 13 ; 14 cout << "done\a\n"; 15 system("pause"); 16 }
当starting显示的时候会响一声,done出现的时候再响一声,中间的时间间隔取决于输入的数字。
类型别名
程序5.16(cin)
#include<iostream> using namespace std; void main() { cout << "Enter characters(enter # to quit):" << endl; char ch; int count = 0; cin >> ch; while (ch != '#') { cout << ch; ++count; cin >> ch; } cout << endl << count << " characters read\n"; system("pause"); }
可以注意到:1.没有空格和换行符;2.输入#之后还能继续输入
这是因为cin在读取char值时,将忽略空格和换行符;另外,发送给cin的输入被缓冲,这意味着只有在按下回车后,输入的内容才会发送给程序。
程序5.17+5.18+5.19(cin.get和EOF)
程序5.20
1 #include<iostream> 2 using namespace std; 3 4 const int City = 5; 5 const int Year = 4; 6 7 void main() { 8 const char *city[City] = { 9 "Gribble City ", 10 "Gribbletown", 11 "New Gribble ", 12 "San Gribble ", 13 "Gribble Vista" 14 }; 15 int maxtemps[Year][City] = 16 { 17 {96,100,87,101,105}, 18 {96,98,91,107,104 }, 19 {97,101,93,108,107}, 20 {98,103,95,109,108} 21 }; 22 cout << "Maxinum temperatures for 2008~2011" << endl; 23 for (int i = 0; i < City; i++) 24 { 25 cout << city[i] << ":\t"; 26 for (int year = 0; year < Year; year++) 27 cout << maxtemps[year][i] << "\t"; 28 cout << endl; 29 } 30 system("pause"); 31 }