移居阿里 2014-10-17 02:43 采纳率: 0%
浏览 1714

输入一串字符,统计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条回答

  • Eleven 2015-01-04 09: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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算