书164页,统计含有两个字符的字符序列的数量:
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
<span style="white-space:pre"> </span>unsigned ffcnt = 0,ficnt =0, flcnt = 0;
<span style="white-space:pre"> </span>char ch,c='\0';
<span style="white-space:pre"> </span>cout << "请依次输入数: " << endl;
<span style="white-space:pre"> </span>while (cin >> ch)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if (c == 'f')
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>switch (ch)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>case'f':
<span style="white-space:pre"> </span>++ffcnt;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case'i':
<span style="white-space:pre"> </span>++ficnt;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case'l':
<span style="white-space:pre"> </span>++flcnt;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>c = ch;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout << "ff的数量为:" << ffcnt << endl;
<span style="white-space:pre"> </span>cout << "fi的数量为:" << ficnt << endl;
<span style="white-space:pre"> </span>cout << "fl的数量为:" << flcnt << endl;
<span style="white-space:pre"> </span>
}