"C ++ Primer" chapter 5th statement the exercises from 1 to 10

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 whilecycle, 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 iterto traverse s, but if the iterator is iterdefined in the written whilejudgment condition statement, will redefine each cycle iter, which is obviously wrong. The correct wording should be iterdefined whileoutside of the loop.

(b) whilethe statement and the ifstatement is independent two code blocks, the definition whilestatement of the variable statusis not in the ifaccessed, correct wording should be statusdefined whilebefore the loop, or the ifstatement to get the whilecycle 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 elsestatement.

#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 elseclause?

C ++ provisions else the distance if it matches the closest match yet.

Exercise 5.9 write a program that uses a series of ifstatements from the statistics cinhow 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 aCntvalue, 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;
}
Published 276 original articles · won praise 21 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_40758751/article/details/104099682