书籍版本:2019年9月第一版;王刚 杨巨峰译;电子工业出版社
编译器 : win10 && VS2015
5.1
空语句是只含一个分号的语句,表示当前什么也不做。
在程序的某个地方,语法上需要一条语句但是逻辑上不需要,此时应该使用空语句。
5.2
块就是用花括号括起来的(可能为空的)语句和声明的序列,一个块就是一个作用域。
在程序的某些地方,语法上需要一条语句,但是逻辑上需要多条语句,则应该使用复合语句。
5.3
while(val <= 10)
{
val++, sum+=val;
}
5.4
a. iter变量未初始化
b. status定义在while中,离开while的作用域之后无效
5.5
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int i;
cin >> i;
if (i > 90)
{
cout << "成绩为:A" << endl;
}
else if(i > 60)
{
cout << "成绩为:B" << endl;
}
else
{
cout << "成绩为:C" << endl;
}
system("pause");
}
5.6
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int i;
cin >> i;
char s = 'A';
s = i < 60 ? 'C' : (i < 90 ? 'B' : 'A');
cout << "成绩为" << s << endl;
system("pause");
}
5.7
a.
if(ival1 != ival2)
ival1 = ival2;
else
ival1 = ival2 = 0;
b.
if(ival < minval)
{minval = ival; occurs = 1;}
c.
int ival = 0;
if(ival == get_value())
cout << "ival = " << ival << emdl;
if(!ival)
cout << "ival = " << ival << endl;
d.
if (ival == 0)
ival = get_value();
5.8
当一个if语句嵌套在另一个if语句内部时,很可能会if分支多余else分支,这就是悬垂else。
C++规定else与离它最近的尚未匹配的if匹配。
5.9
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
char i = ' ';
int num = 0;
while (cin >> i)
{
if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u'
|| i == 'A' || i == 'E' || i == 'I' || i == 'O' || i == 'U')
{
++num;
}
}
cout << num << endl;
system("pause");
}
5.10
同上
5.11
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
char i ;
int num = 0;
while (cin >> i)
{
if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u'
|| i == 'A' || i == 'E' || i == 'I' || i == 'O' || i == 'U'
|| i == ' ' || i == '/r' || i == '/t' || i == '/n')
{
++num;
}
}
cout << num << endl;
system("pause");
}
5.12
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string sIn = "";
int nNnmff = 0, nNumfl = 0, nNumfi = 0;
string sff = "ff";
string sfl = "fl";
string sfi = "fi";
while (getline(cin, sIn))
{
if (sIn == sff)
{
++nNnmff;
}
else if (sIn == sfl)
{
++nNumfl;
}
else if (sIn == sfl)
{
++nNumfi;
}
cout << nNnmff << " " << nNumfi << " " << nNumfl << endl;
}
system("pause");
}
5.13
a. 每个case和default后面都没有break;
b. ix超出了作用域
c. 每个case只能对应一个值
d. case后面的对应值应为一个常量
5.14
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector <string> srrVec;
string text = "";
while (cin >> text) {
if (text != "END") {
srrVec.push_back(text);
}
else
{
break;
}
}
int nSize = srrVec.size();
string tmpStr;
int a[999] = { 0 };
for (int i = 0;i<nSize; i++)
{
tmpStr = srrVec.at(i);
for (int j = 0; j < nSize; j++)
{
if (srrVec.at(j) == tmpStr)
{
++a[i];
}
}
}
bool bShow = false;
vector <string> strShowVec;
for (int m = 0; m < nSize; m++)
{
int nShowSize = strShowVec.size();
bool bHadShow = false;
for (int n = 0; n < nShowSize; n++)
{
if ( (srrVec.at(m) == strShowVec.at(n)) )
{
bHadShow = true;
}
}
if ( !bHadShow && a[m] > 1)
{
strShowVec.push_back(srrVec.at(m));
cout << "连续出现的字符串是:" << srrVec.at(m) ;
cout << ",它出现的次数为:" << a[m] << endl;
bShow = true;
}
}
if (!bShow)
{
cout << "没有连续出现" << endl;
}
system("pause");
}
5.15
a. for好像没问题,for之后的if是多余的
b. ix没有赋初值
c. 循环永远不可能结束,除非ix与sz一开始就想等
5.16
更倾向于用for,因为可以准确取到某个位置的值
5.17
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
bool bIs = false;
vector <int> vec1 = { 0,1,1,2 };
vector <int> vec2 = { 0,1,1,2,3,5,8 };
int size1 = vec1.size();
int size2 = vec2.size();
int realSize = size1 > size2 ? size2 : size1;
for (int i = 0; i < realSize; i++)
{
if (vec1.at(i) == vec2.at(i))
{
continue;
}
bIs = true;
}
if (bIs)
{
cout << "不存在前缀关系" << endl;
}
else
{
cout << "是前缀关系" << endl;
}
system("pause");
}
5.18
a. 代码是为了将输入的两个数相加输出和。但是对输入的判断在对输入的数的操作之后,会炸
b. while里面的是赋值操作不是判断。
c. ival超出了作用域
5.19
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "请输入两个字符串:";
string str1,str2;
do
{
int size1 = str1.size();
int size2 = str2.size();
if (size1 < size2)
{
cout << str1 << endl;
}
else
{
cout << str2 << endl;
}
} while (cin>>str1>>str2);
system("pause");
}
5.20
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
vector<string> strVec;
int nPos = -1;
while (cin>>str)
{
strVec.push_back(str);
int nSize = strVec.size();
for (int i = 0;i<nSize-1;i++)
{
if (str == strVec.at(i))
{
nPos = i;
}
}
if (nPos > 0)
{
break;
}
}
if (nPos > 0)
{
cout << "重复的是:" << str << endl;
}
system("pause");
}
5.21
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str;
vector<string> strVec;
int nPos = -1;
while (cin>>str)
{
strVec.push_back(str);
int nSize = strVec.size();
for (int i = 0;i<nSize-1;i++)
{
string ss = strVec.at(i);
if (str == ss && ss[0]>='A' && ss[0]<='Z' )
{
nPos = i;
}
}
if (nPos > 0)
{
break;
}
}
if (nPos > 0)
{
cout << "重复的是:" << str << endl;
}
system("pause");
}
5.22
int sz;
do
{
sz = get_size();
}
while(sz<=0)
5.23
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int num1, num2;
cin >> num1 >> num2;
cout << num1 / num2 << endl;
system("pause");
}
5.24.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int num1, num2;
cin >> num1 >> num2;
if (num2 == 0)
{
throw runtime_error("Data Error!");
}
cout << num1 / num2 << endl;
system("pause");
}
抛出异常之后系统会中断
5.25
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int num1, num2;
try
{
cin >> num1 >> num2;
if (num2 == 0)
{
throw runtime_error("Data Error!");
}
cout << num1 / num2 << endl;
}
catch (runtime_error err)
{
cout << "输入错误!";
}
system("pause");
}