打印

程序精简问题

[复制链接]
837|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pkat|  楼主 | 2014-1-31 07:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我编写了一个查找\,,\:,\r,\n的4层嵌套if语句的程序,将字符串中的所有上述4个字符删除,感觉有点绕,能不能帮忙精简一下,程序如下:
#include<string>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//功能: 删除字符串中的"\r" "\n""\,""\:"
string StrClean(const string &strSource)
{
    string strDes = strSource;
    string::size_type index = 0;
    do
    {
        index = strDes.find("\\:");
             if (index != string::npos)
             {
              strDes = strDes.replace(index, strlen("\\:")," ");
             }
             else
             {            
                       index = strDes.find("\\r");
                       if (index != string::npos)
                         {
                           strDes = strDes.replace(index, strlen("\\r")," ");
                         }
                       else
                       {
                           index = strDes.find("\\n");
                           if (index != string::npos)
                           {
                               strDes = strDes.replace(index, strlen("\\n")," ");
                           }
                           else
                           {
                               index = strDes.find("\\,");
                               if (index != string::npos)
                               {
                                   strDes = strDes.replace(index, strlen("\\,")," ");
                               }
                           }
                       }
             }
    } while (string::npos != index);
    return strDes;
}


int main()
{
    string str = "123\:12\,esg\rsf\n if \: will my name\, gs\r\ntf\vs\r\nc",stre;
    cout << "the source string is :" << str << endl;
    //str.remove("\r\n");
    //str.replace("\r\n", "");
    stre=StrClean(str);
    cout << "the destination string is :" << stre << endl;
    return 0;

}

相关帖子

沙发
秋天落叶| | 2014-1-31 23:05 | 只看该作者
优化程序是一个比较难的课题

使用特权

评论回复
板凳
hsbjb| | 2014-1-31 23:15 | 只看该作者
没看太明白

使用特权

评论回复
地板
无冕之王| | 2014-1-31 23:31 | 只看该作者
哪位大侠给精简一下

使用特权

评论回复
5
石头记| | 2014-2-8 22:34 | 只看该作者
本帖最后由 石头记 于 2014-2-8 22:37 编辑

这么多IF,如果用switch可能会少几行吧?
switch
case  1
goto OS1
case 2
goto OS1
case  3
goto OS1
case  4
goto OS1
default:
goto OS2





使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

2726

帖子

0

粉丝