Exercise 5.1 What is a null statement? When empty statements?
Empty statement is no action statements when the syntax requires a statement but does not require logically, they can use an empty statement.
Exercise 5.2 What is a block? What will be used when the block?
Block is a sequence of statements and declarations enclosed in braces, when a statement syntax required but requires a set of logical statements, you can use the block.
Exercise 5.3 with the comma operator section 1.4.1 rewrite while
cycle, it is no longer needed blocks observed after rewriting the code to improve readability or decreased.
while (val <= 10)
sum += val, ++val;
I think the code is really become simple, but readability decreased.
Exercise 5.4 The following examples illustrate the meaning, if there is a problem, try to modify it.
(a) while (string::iterator iter != s.end()) { /* . . . */ }
(b) while (bool status = find(word)) { /* . . . */ }
if (!status) { /* . . . */ }
(a) statement seems to want to use an iterator iter
to traverse s
, but if the iterator is iter
defined in the written while
judgment condition statement, will redefine each cycle iter
, which is obviously wrong. The correct wording should be iter
defined while
outside of the loop.
(b) while
the statement and the if
statement is independent two code blocks, the definition while
statement of the variable status
is not in the if
accessed, correct wording should be status
defined while
before the loop, or the if
statement to get the while
cycle to judge.
Exercise 5.5 Write a program of their own, using if else statements to convert a number to achieve the requirements of letter grades.
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int grade;
vector<string> souce = { "F", "D", "C", "B", "A", "A++" };
while(cin >> grade){
string lettergrade;
if (grade < 60) {
lettergrade = souce[0];
}
else {
lettergrade = souce[(grade - 50) / 10];
}
if (grade == 100 || grade < 60) {
}
else {
if (grade % 10 > 7) {
lettergrade += "+";
}
else if (grade % 10 < 3) {
lettergrade += "-";
}
}
cout << lettergrade << endl;
}
return 0;
}
Exercise 5.6 rewritten on a question of procedure, the use of the conditional operator instead of the if else
statement.
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int grade;
vector<string> souce = { "F", "D", "C", "B", "A", "A++" };
while(cin >> grade){
string lettergrade = grade < 60 ? souce[0] : souce[(grade - 50) / 10];
lettergrade += (grade == 100 || grade < 60) ? "" : (grade % 10 > 7) ? "+" : (grade % 10 < 3) ? "-" : "";
cout << lettergrade << endl;
}
return 0;
}
Exercise 5.7 rewrite the following code snippet error.
(a) if (ival1 != ival2)
ival1 = ival2
else
ival1 = ival2 = 0;
(b) if (ival < minval)
minval = ival;
occurs = 1;
(c) if (int ival = get_value())
cout << "ival = " << ival << endl;
if (!ival)
cout << "ival = 0\n";
(d) if (ival = 0)
ival = get_value();
(a) After ival1 = ival2 statement less ';'
.
(b) should minval = ival;
and occurs = 1;
around braces.
© should be if(!ival)
changed else
.
(d) if (ival = 0)
should be replaced if (ival == 0)
.
Exercise 5.8 What is "hanging else
"? How to deal with C ++ language else
clause?
C ++ provisions else the distance if it matches the closest match yet.
Exercise 5.9 write a program that uses a series of if
statements from the statistics cin
how many vowels read text.
#include<iostream>
using namespace std;
int main()
{
char ch;
int sum_a = 0, sum_e = 0, sum_i = 0, sum_o = 0, sum_u = 0;
int vowelcnt = 0;
while(cin >> ch){
if (ch == 'a') {
++sum_a;
++vowelcnt;
}
else if(ch == 'e'){
++sum_e;
++vowelcnt;
}
else if (ch == 'i') {
++sum_i;
++vowelcnt;
}
else if (ch == 'o') {
++sum_o;
++vowelcnt;
}
else if (ch == 'u') {
++sum_u;
++vowelcnt;
}
}
cout << "acnt is: " << sum_a << endl;
cout << "ecnt is: " << sum_e << endl;
cout << "icnt is: " << sum_i << endl;
cout << "ocnt is: " << sum_o << endl;
cout << "ucnt is: " << sum_u << endl;
cout << "vowelcnt is: " << vowelcnt << endl;
return 0;
}
Exercise 5.10 vowels statistics exist to achieve our program before a problem: If the vowels in upper case, will not be counted. Write a program, both statistical lowercase vowel, and also statistical uppercase vowels, that is, new encounters 'a' and 'A' should be incremented aCnt
value, and so on.
#include<iostream>
using namespace std;
int main()
{
char ch;
int sum_a = 0, sum_e = 0, sum_i = 0, sum_o = 0, sum_u = 0;
int vowelcnt = 0;
while(cin >> ch){
switch (ch)
{
case 'a':
case 'A':
++sum_a;
++vowelcnt;
break;
case 'e':
case 'E':
++sum_e;
++vowelcnt;
break;
case 'i':
case 'I':
++sum_i;
++vowelcnt;
break;
case 'o':
case 'O':
++sum_o;
++vowelcnt;
break;
case 'u':
case 'U':
++sum_u;
++vowelcnt;
break;
default:
break;
}
}
cout << "acnt is: " << sum_a << endl;
cout << "ecnt is: " << sum_e << endl;
cout << "icnt is: " << sum_i << endl;
cout << "ocnt is: " << sum_o << endl;
cout << "ucnt is: " << sum_u << endl;
cout << "vowelcnt is: " << vowelcnt << endl;
return 0;
}