2 u011585252 u011585252 于 2014.10.17 10:43 提问

输入一串字符,统计ff,fl,fi出现的次数,为什么输入奇数个f就会出错啊?

#include
#include
using namespace std;
int main()
{
int a=0,b=0,c=0;
char aa,bb=' ';
vector ss;
while(cin.get(aa))
{
ss.push_back(aa);
}
for(vector::iterator i=ss.begin();i!=ss.end();++i)
{
if(bb=='f')
{
switch(*i)
{
case 'f':
++a;
break;
case 'l':
++b;
break;
case 'i':
++c;
break;
}
++i;
}
bb=*i;
}
cout<<"the number of ff is:"<<a<<endl;
cout<<"the number of fl is:"<<b<<endl;
cout<<"the number of fi is:"<<c<<endl;
system("pause");
return 0;
}

1个回答

VisualEleven
VisualEleven   Ds   Rxr 2015.01.04 17:31
 #include <iostream>
#include <vector>
#include <string>
using namespace std;


int main()
{
    int a=0,b=0,c=0;
    string aa;
    char bb=' ';
    vector<char> ss;

    cin>>aa;

    for(string::size_type i = 0; i != aa.size(); ++i)
    {
        ss.push_back(aa.at(i));
    }
    for(vector<char>::iterator i=ss.begin();i!=ss.end();++i)
    {
        if(bb=='f')
        {
            switch(*i)
            {
            case 'f':
                ++a;
                break;
            case 'l':
                ++b;
                break;
            case 'i':
                ++c;
                break;
            }
            ++i;
        }
        bb=*i;
    }
    cout<<"the number of ff is:"<<a<<endl;
    cout<<"the number of fl is:"<<b<<endl;
    cout<<"the number of fi is:"<<c<<endl;
    system("pause");
    return 0;
}
Csdn user default icon
上传中...
上传图片
插入图片