码迷,mamicode.com
首页 > 编程语言 > 详细

19天学完C++Primer【5】

时间:2016-01-05 18:36:13      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

5.1:什么是空语句?什么时候会用到空语句?
解答:
【引用】空语句就是含有一个单独分号的语句。
【引用】如果在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。

5.2:什么是块?什么时候会用到块?
解答:
【引用】复合语句是指用花括号括起来的语句和声明的序列,复合语句也被称作块。
【引用】如果在程序的某个地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句。

5.3:使用都好运算符重写1.4.1节的while循环,是他不需要快,观察改写之后的代码的可读性是提高了还是降低了。
解答:
while (val++ < 10)
sum += val;//根据王垠的说法,可读性是降低了。


5.4:说明下列例子的含义,如果存在问题,试着修改它
(a) while(string::iterator iter != s.end()){/*...*/}
(b) while(bool status = find(word)) {/* .. */}
if(!status){/* ... */}
解答:
(a) std::string::iterator iter = s.begin();
while (iter != s.end()) { /* . . . */ }
(b) bool status;
while ((status = find(word))) {/* ... */} if (!status) {/* ... */}

5.5:写一段自己的程序,使用if else语句实现把数字成绩转换为字母成绩的要求。
解答:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 

string res[3] = { "fail", "pass", "high pass" }; 

int main(){ 
int grade; 
    while (cin >> grade){ 
        if (grade > 90){ 
            cout << res[2] << endl; 
        } 
            else{ 
        cout << res[grade / 60] << endl; 
        } 
    } 
}

 

5.6:改写上一题的程序,使用条件运算符,代替if else语句。
解答:

#include <iostream> 
#include <vector> 
#include <string> 

using namespace std; 

string res[3] = { "fail", "pass", "high pass" }; 

int main(){ 
int grade; 
    while (cin >> grade){ 
        grade > 90 ? (cout << res[2] << endl) : (cout << res[grade /         60] << endl); 
    } 
}

 

5.7:改正下列代码段中的错误
(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) 第二行缺少分号
(b) 第二行和第三行应该用大括号括起来
(c) ival不能再第二个if中使用
(d) 这段没什么错误。不过if语句中的代码永远不会被执行

5.8:什么是“悬垂else”?C++语言是如何处理else子句的?
解答:
【引用】确定else是和哪个if进行匹配的问题,通常称为“悬垂else”
【引用】C++规定else与离它最近的尚未匹配的if匹配,从而消除了程序的二义性。
5.9:编写一段程序,使用一些列if语句统计从cin读入的文本中有多少元音字母。
解答:

#include <iostream> 
#include <string> 
#include <cctype> 

using namespace std; 

int main(){ 
string str; 
int count[5] = { 0 }; 
while (cin >> str){ 
for (auto i : str){ 
if (isalpha(i)){ 
if (i == a) 
++count[0]; 
else if (i == e) 
++count[1]; 
else if (i == i) 
++count[2]; 
else if (i == o) 
++count[3]; 
else if (i == u) 
++count[4]; 
} 
} 
cout << "a: " << count[0] << endl; 
cout << "e: " << count[1] << endl; 
cout << "i: " << count[2] << endl; 
cout << "o: " << count[3] << endl; 
cout << "u: " << count[4] << endl; 
} 
}

 

5.10:我们之前实现的统计元音字母的程序存在一个问题:如果元音字母以大写形式出现,不会被统计在内。编写一段程序,及统计元音字符的小写形式,也统计大写形式,也就是说,新程序遇到‘a’和‘A‘都应该递增aCnt的值,以此类推。
解答:

#include <iostream> 
#include <string> 
#include <cctype> 

using namespace std; 

int main(){ 
string str; 
int count[5] = { 0 }; 
while (cin >> str){ 
for (auto i : str){ 
if (isalpha(i)){ 
switch (i){ 
casea: 
case A: 
++count[0]; 
break; 
case e: 
case E: 
++count[1]; 
break; 
case i: 
case I: 
++count[2]; 
break; 
case o: 
case O: 
++count[3]; 
break; 
case u: 
case U: 
++count[4]; 
break; 
} 
} 
} 
cout << "a: " << count[0] << endl; 
cout
<< "e: " << count[1] << endl; cout << "i: " << count[2] << endl; cout << "o: " << count[3] << endl; cout << "u: " << count[4] << endl; } }

 

5.11:修改统计元音字母的程序,使其也能统计空格、制表符和换行符的数量。
解答:

#include <iostream> 
#include <string> 

using namespace std; 

int main(){ 
string str; 
int count[5 + 3] = { 0 }; 
while (getline(cin, str)){ 
for (auto i : str){ 
switch (i){ 
casea: 
case A: 
++count[0]; 
break; 
case e: 
case E: 
++count[1]; 
break; 
case i: 
case I: 
++count[2]; 
break; 
case o: 
case O: 
++count[3]; 
break; 
case u: 
case U: 
++count[4]; 
break; 
case  : 
++count[5]; 
break; 
case \t: 
case \v: 
++count[6]; 
break; 
case \n: 
++count[7]; 
break; 
} 
} 
} 
cout << "a: " << count[0] << endl; 
cout << "e: " << count[1] << endl; 
cout << "i: " << count[2] << endl; 
cout << "o: " << count[3] << endl; 
cout << "u: " << count[4] << endl; 
cout << "space character: " << count[5] << endl; 
cout << "table character: " << count[6] << endl; 
cout << "new line character: " << count[7] << endl; 
}

 

5.12:修改统计元音字母的程序,使其能统计一下含有两个字符的字符序列的数量:ff、fl和fi。
解答:

#include <iostream> 
#include <string> 
using namespace std; 
int main(){ 
string str; 
int count[5 + 3] = { 0 }; 
char ch; 
while (getline(cin, str)){ 
for (auto i : str){ 
switch (i){ 
casea: 
case A: 
++count[0]; 
break; 
case e: 
case E: 
++count[1]; 
break; 
case i: 
if (ch == f) 
++count[7]; 
case I: 
++count[2]; 
break; 
case o: 
case O: 
++count[3]; 
break; 
case u: 
case U: 
++count[4]; 
break; 
case f: 
if (ch == f) 
++count[5]; 
break; 
case l: 
if (ch== f) 
++count[6]; 
break; 
} 
ch = i; 
} 
} 
cout << "a: " << count[0] << endl; 
cout << "e: " << count[1] << endl; 
cout << "i: " << count[2] << endl; 
cout << "o: " << count[3] << endl; 
cout << "u: " << count[4] << endl; 
cout << "ff: " << count[5] << endl; 
cout << "fl: " << count[6] << endl; 
cout << "fi: " << count[7] << endl; 
}

 

5.13:下面显示的每个程序都含有一个常见的编程错误,指出错误在哪里,然后修改他们。

(a)
unsigned aCnt = 0, eCnt = 0, iouCnt =0;
char ch = next_text();
switch(ch){
case a: aCnt++;
case e: eCnt++;
default: iouCnt++;
}
(b)
unsigned index = some_value();
switch(index){
case 1:
int ix = get_value();
ivec[ix] = index;
default:
ix = ivec.size() - 1;
ivec[ix] = index;
}
(c)
unsigned evenCnt = 0, oddCnt = 0;
int digit = get_num() % 10;
switch(digit){
case 1, 3, 5, 7, 9:
oddcnt++;
break;
case 2, 4, 6, 8,10:
eventcnt++;
break;
}
(d)
unsigned ival = 512, jval = 1024, kval = 4096;
unsigned bufsize;
unsigned swt = get_bufCnt();
switch(swt){
case ival:
bufsize = ival * sizeof(int);
break;
case jval:
bufsize = jval * size(int);
break;
case kval:
bufsize = kval * sizeof(int);
break;
}

 


解答:
(a) case语句间没有break,会造成统计错误
(b) 对ix进行了初始化,需要大括号将case1的内容括起来。
(c) case标签不能这样写;使用的变量未定义
(d) case标签必须是整型常量表达式
5.14:编写一段程序,从标准输入中读取若干string对象并查找连续重复出现的单词。所谓连续重复出现的意思是:一个单词后面紧跟着这个单词本身。要求记录连续重复出现的最大次数以及对应单词。如果这样的单词存在,输出重复出现的最大次数;如果不存在,输出一条信息说明任何单词都没有连续出现过。例如,如果输入是
how now now now brown cow cow
那么输出应该表明单词now连续出现了3次。
解答:

#include <iostream> 
#include <sstream> 
#include <map> 
#include <string> 

using namespace std; 

int main(){ 
string line, word_new, word_old; 
map<string, int> res; 
while (getline(cin, line)){ 
istringstream istr(line); 
while (istr >> word_new){ 
if (word_new == word_old){ 
++res[word_old]; 
} 
else{ 
res[word_new] = 1; 
} 
word_old = word_new; 
} 
} 
map<string, int>::iterator max_it = res.begin(); 
for (auto it = res.begin(); it != res.end(); ++it){ 
if (max_it->second < it->second){ 
max_it = it; 
} 
} 
cout << max_it->first << " appearance " << max_it->second << " times." << endl; 
}

 

5.15:说明下列循环的含义并改正其中的错误。
(a)
for(int ix = 0; ix != sz; ++ix){...}
if (ix != sz)....
(b)
int ix;
for (ix != sz; ++ix) ...
(c)
for (int ix = 0; ix != sz; ++ix, ++ sz)...
解答:
(a) ix不能在for循环外使用
(b) for循环语法应有三段
(c) 合法。

5.16:while循环特别适用于那种条件不变、反复执行操作的情况,例如,当未达到文件末尾时不断读取下一个值。for循环则更像是在按步骤迭代,他的索引值在某个范围内依次变化。根据每种循环的习惯用法各自写一段程序,然后分别用另一种循环改写。如果只能使用一种循环,你倾向于使用哪种循环呢?为什么?
解答:for

5.17:假设两个包含整数的vector对象,编写一段程序,检验其中一个vector对象是否是另一个的前缀。为了实现这个目标,对于两个不等长的vector对象,只需跳出长度较短的那个,把它的所有元素和另一个vector对象比较即可。例如,两个vector对象的元素分别是0、1、1、2和0、1、1、2、3、5、8,则程序返回结果应该为真。

解答:

#include <iostream> 
#include <vector> 

using namespace std; 

int main(){ 
vector<int> ivec1{ 0, 1, 1, 2}, ivec2{ 0, 1, 1, 2, 3, 5, 8 }; 
vector<int>::size_type min_len = (ivec1.size() < ivec2.size() ? ivec1.size() : ivec2.size()); 
bool result = true; 
for (size_t i = 0; i != min_len; ++i){ 
if (ivec1.at(i) != ivec2.at(i)){ 
result = false; 
break; 
} 
} 
cout << boolalpha << result << endl; 
} 

 


5.18:说明下列循环的含义并改正其中的错误。

(a)
do
int v1, v2;
cout << "Please enter two numbers to sum:";
if (cin >> v1 >> v2)
cout << "Sum is: " << v1 + v2 << endl;
while(cin);
(b)
do{
//...
}while(int ival = get_response());
(c)
do{
int ival = get_response();
} while(ival);

 


解答:
(a) do while缺少大括号
(b) 如果ival在块中使用到了,那么这段代码就错了
(c) 编译器会提示未定义ival。

5.19:编写一段程序,使用do while循环重复执行下述任务:首先提示用户输入两个string对象,然后填出较短那个并输出它。
解答:

#include <iostream> 
#include <string> 

using namespace std; 

int main(){ 
do{ 
string str1, str2; 
cout << "Please enter two string:\n"; 
cin >> str1 >> str2; 
cout << (str1.size() < str2.size() ? str1 : str2) << endl; 
} while (cin); 
}

 

5.20:编写一段程序,从标准输入中读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完位置。使用while循环一次读取一个单词,当一个单词连续出现两次时使用break语句终止循环。输出连续重复出现的单词,或者输出一个消息说明没有任何单词是连续重复出现的。
解答:

#include <iostream> 
#include <string> 

using namespace std; 

int main(){ 
string word_new, word_old; 
bool flag = true; 
while (cin >> word_new){ 
if (word_new == word_old){ 
cout << word_old << " is duplicated." << endl; 
flag = false; 
break; 
} 
word_old = word_new; 
} 
if (flag){ 
cout << "no words duplicate." << endl; 
} 
}

 

5.21:修改5.51节练习题的程序,使其找到的重复单词必须以大写字母开头。
解答:

#include <iostream> 
#include <string> 
#include <cctype> 

using namespace std; 

int main(){ 
string word_new, word_old; 
bool flag = true; 
while (cin >> word_new){ 
if (word_new == word_old && isupper(word_new.at(0))){ 
cout << word_old << " is duplicated." << endl; 
flag = false; 
continue; 
} 
word_old = word_new; 
} 
if (flag){ 
cout << "no words duplicate." << endl; 
} 
} 

 


这个是找到全部满足条件的单词。

5.22:本节的最后一个例子跳回到begin,其实是用循环能更好地完成该任务。重写这段代码,注意不再是用goto语句。
解答:
while(1){
int sz = get_size();
if(sz <= 0) continue;
}
5.23:编写一段程序,从标准输入读取两个整数,输入第一个数除以第二个数的结果。
解答:

#include <iostream> 

using namespace std; 

int main(){ 
int a, b; 
cin >> a >> b; 
cout << a / b << endl; 
}

 

5.24:修改你的程序,使得当第二个数是0时抛出异常。先不要设定catch子句,运行程序并真的为除数输入0,看看会发生什么?
解答:

#include <iostream> 
#include <stdexcept> 

using namespace std; 

int main(){ 
int a, b; 
cin >> a >> b; 
if (b == 0){ 
throw range_error("The denominator is 0"); 
} 
cout << a / b << endl; 
}

 

5.26:修改上一题的程序,使用try语句块去捕捉异常。catch子句应该为用户输出一条提示信息,询问其是否输入新数并重新执行try语句块的内容。
解答:

#include <iostream> 
#include <stdexcept> 
#include <string> 

using namespace std; 

int main(){ 
int a, b; 
string choose; 
try{ 
cin >> a >> b; 
if (b == 0){ 
throw range_error("The denominator is 0"); 
} 
} 
catch (range_error &e){ 
cout << e.what()<< endl; 
while (1){ 
cout << "Do you want to type other num for denominator?[y/N]" << endl; 
cin >> choose; 
if (choose.at(0) == y || choose.at(0) == Y){ 
cin >> b; 
if (b == 0){ 
cout << "The denominator is 0" << endl; 
continue; 
} 
else{ 
break; 
} 
} 
else{ 
break; 
} 
} 
} 
cout << a / b << endl; 
}

 

19天学完C++Primer【5】

标签:

原文地址:http://www.cnblogs.com/fastcam/p/5103295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!