习题5-1:
空语句就是什么都不写,只含有一个单独的分号。
;//空语句
使用场景:语法上需要一条语句但是逻辑上不需要。
习题5-2:
块就是复合语句,要用花括号括起来的。
使用场景:语法上需要一条语句,逻辑上需要多条语句。
习题5-3:
#include <iostream>
using namespace std;
int main()
{
int sum = 0, val = 1;
while (val <= 10)
sum +=val, ++val;
cout << sum << endl;
return 0;
}
习题5-4:
1、iter没有初始化,如何与s.end()比较
2、while的条件是一个定义语句,编译可以通过,find(word)的返回值为条件值,但是if的条件会报错:没有定义,因为status作用域只在while当中。
习题5-5:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
const vector<string> scores = {"F","D","C","B","A","A++"};
int grade;
vector<string> letterScores;
vector<int> numScores;
while (cin >> grade)
{
if (grade == -1)
break;
numScores.push_back(grade);
if (grade<60)
letterScores.push_back(scores[0]);
else
letterScores.push_back(scores[(grade-50)/10]);
}
for (auto letter : letterScores)
cout << letter << endl;
return 0;
}
习题5-6:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
const vector<string> scores = {"F","D","C","B","A","A++"};
int grade;
vector<string> letterScores;
vector<int> numScores;
while (cin >> grade)
{
if (grade == -1)
break;
string letter = (grade < 60) ? scores[0] : scores[(grade-50)/10];
letterScores.push_back(letter);
}
for (auto letterScore : letterScores)
cout << letterScore << endl;
return 0;
}
习题5-9:
需要注意的是,这里我没有像前面那样设计键盘终止标志,通过流重定向进行的输入。
#include <iostream>
using namespace std;
int main()
{
unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;
char c;
while (cin >> c)
{
if (c == 'a')
++aCnt;
if (c == 'e')
++eCnt;
if (c == 'i')
++iCnt;
if (c == 'o')
++oCnt;
if (c == 'u')
++uCnt;
}
cout << aCnt << endl;
cout << eCnt << endl;
cout << iCnt << endl;
cout << oCnt << endl;
cout << uCnt << endl;
return 0;
}
习题5-10:
题目的意思是通过switch case语句来写,我就直接用上面的程序来修改了
#include <iostream>
using namespace std;
int main()
{
unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0;
char c;
while (cin >> c)
{
if (c == 'a' || c == 'A')
++aCnt;
if (c == 'e' || c == 'E')
++eCnt;
if (c == 'i' || c == 'I')
++iCnt;
if (c == 'o' || c == 'O')
++oCnt;
if (c == 'u' || c == 'U')
++uCnt;
}
cout << aCnt << endl;
cout << eCnt << endl;
cout << iCnt << endl;
cout << oCnt << endl;
cout << uCnt << endl;
return 0;
}
习题5-11:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0,spaceCnt=0, tapCnt=0, enterCnt=0;
char c;
while (cin >> noskipws >> c)
{
if (c == 'a' || c == 'A')
++aCnt;
if (c == 'e' || c == 'E')
++eCnt;
if (c == 'i' || c == 'I')
++iCnt;
if (c == 'o' || c == 'O')
++oCnt;
if (c == 'u' || c == 'U')
++uCnt;
if (c == ' ')
++spaceCnt;
if (c == '\t')
++tapCnt;
if (c == '\n')
++enterCnt;
}
cout << aCnt << endl;
cout << eCnt << endl;
cout << iCnt << endl;
cout << oCnt << endl;
cout << spaceCnt << endl;
cout << tapCnt << endl;
cout << enterCnt << endl;
return 0;
}
习题5-12:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
unsigned aCnt=0, eCnt=0, iCnt=0, oCnt=0, uCnt=0,spaceCnt=0, tapCnt=0, enterCnt=0, ffCnt=0, flCnt=0, fiCnt=0;
char c, cNext;
while (cin >> noskipws >> c)
{
if (c == 'f')
{
cin >> noskipws >> cNext;
if ( cNext == 'f')
++ffCnt;
if ( cNext == 'l')
++flCnt;
if ( cNext == 'i')
++fiCnt;
c = cNext;
}
if (c == 'a' || c == 'A')
++aCnt;
if (c == 'e' || c == 'E')
++eCnt;
if (c == 'i' || c == 'I')
++iCnt;
if (c == 'o' || c == 'O')
++oCnt;
if (c == 'u' || c == 'U')
++uCnt;
if (c == ' ')
++spaceCnt;
if (c == '\t')
++tapCnt;
if (c == '\n')
++enterCnt;
}
cout << aCnt << endl;
cout << eCnt << endl;
cout << iCnt << endl;
cout << oCnt << endl;
cout << spaceCnt << endl;
cout << tapCnt << endl;
cout << enterCnt << endl;
cout << "the number of fi" << fiCnt << endl;
cout << "the number of fl" << flCnt << endl;
cout << "the number of ff" << ffCnt << endl;
return 0;
}
习题5-12的switch写法:
#include <iostream>
#include <string>
using namespace std;
int main()
{
char c,cNext;
int aCnt=0, bCnt=0, ffCnt=0, flCnt=0,fiCnt=0;
while (cin >> c)
{
switch (c)
{
case 'f':case 'F' :
cin >> cNext;
switch (cNext)
{
case 'f':ffCnt++;break;
case 'l':flCnt++;break;
case 'i':fiCnt++;break;
default:c=cNext;break;
}
case 'a':case 'A' :aCnt++;break;
case 'b':case 'B' :bCnt++;break;
}
}
cout << aCnt << endl;
cout << bCnt << endl;
cout << ffCnt << endl;
cout << fiCnt << endl;
cout << flCnt << endl;
}
习题5-17:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec1{0,1,1,2}, vec2{0,1,1,2,3,5,8};
int size1=vec1.size(), size2=vec2.size(),minSize;
minSize = size1 < size2? size1 : size2;
bool head = true;
for (int i = 0; i != minSize; ++i)
{
if (vec1[i] != vec2[i])
{
cout << "False" << endl;
head = false;
break;
}
}
if (head)
cout << "True" << endl;
return 0;
}