C ++替换部分字符串

时间:2015-04-09 02:49:04

标签: c++ string

我有这个代码替换字符串中的{0}和{1}“数字{0}大于{1}”,0和1是变量参数列表的索引,但如果在里面{}:他们是一个添加的说明符,{0:[说明符]},c表示货币,f表示固定,i表示int,或e表示科学,数字也按此方式格式化。该代码仅在说明符相同时才起作用,“数字{0:[c]}大于{1:[c]}”,如果它们不同,“数字{0:[c]}是大于{1:[f]}“,那么结果由于某种原因都是以第二个索引的方式格式化,在我给出的两个例子中都将格式化为固定。我不知道为什么第二个说明符影响这两个数字,值不会改变,但第二个说明符会影响这两个值。任何人都有任何想法?

我不明白这个问题,因为当程序到达

if(prompt.find(c2) != std::string::npos)

提示已经被修改过一次因此只能找到一个{index:[specifier]},因为第一个已被替换。我对这种思路感到失望吗?

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdarg>
#include <sstream> 

using namespace std;

void write(string prompt, ...);
int ConvertCharToInt(char c);

void main()
{
    write("The number {0:[i]} is greater than {1:[i]}.\n", 5.9, 1.3, 4.7, 6.8);
}

void write(string prompt, ...)
{
char c1, c2;
int pos1, pos2, num1, num2, temp;
double arg1, arg2;

va_list arguments;
va_start(arguments, prompt);

pos1 = prompt.find_first_of("{");
c1 = prompt.at(pos1+1);
pos2 = prompt.find_last_of("{");
c2 = prompt.at(pos2+1);

num1 = ConvertCharToInt(c1);
num2 = ConvertCharToInt(c2);


for(int i = -1; i < num1; i++)
{
    arg1 = va_arg(arguments, double);
}   
for(int i = num1; i < num2; i++)
{
    arg2 = va_arg(arguments, double);
}

if(prompt.find(c1) != std::string::npos)
{
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream a;
        a << fixed << setprecision(2) << arg1;
        string r1 = "$" + a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream a;
        a << scientific << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {
        ostringstream a;
        a << fixed << setprecision(6) << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream a;
        a << (int)(arg1+.5);
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else
    {
        ostringstream a;
        a << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 3, r1);
    }
}

if(prompt.find(c2) != std::string::npos)
{
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

cout << prompt << "\n";
va_end(arguments);
}

int ConvertCharToInt(char c)
{
int num;

switch(c)
{
    case '0':
        num = 0;
        break;
    case '1':
        num = 1;
        break;
    case '2':
        num = 2;
        break;
    case '3':
        num = 3;
        break;
    case '4':
        num = 4;
        break;
    case '5':
        num = 5;
        break;
    case '6':
        num = 6;
        break;
    case '7':
        num = 7;
        break;
    case '8':
        num = 8;
        break;
    case '9':
        num = 9;
        break;
    default:
        exit(EXIT_FAILURE);
}
return num;
}

2 个答案:

答案 0 :(得分:0)

问题是连续搜索您的标识符,如[i],[f]等。

如果你有最后一个[f] string.find()总是找到[f]然后离开你的if-else-branches。因为[f]分支在[i]分支之前,你在你的例子中总是找到[f]并打印相应的格式。

解决方案是放弃寻求整个[f]表达式,但只寻求&#34; [&#34;。

答案 1 :(得分:0)

我实际上是让它与它一起工作,它非常漫长而乏味但它确实有效。

if(prompt.find("[c]") != std::string::npos)
{
    ostringstream a;
    a << fixed << setprecision(2) << arg1;
    string r1 = "$" + a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[e]") != std::string::npos)
{
    ostringstream a;
    a << scientific << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[f]") != std::string::npos)
{
    ostringstream a;
    a << fixed << setprecision(6) << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[i]") != std::string::npos)
{
    ostringstream a;
    a << (int)(arg1+.5);
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else
{
    ostringstream a;
    a << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 3, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

cout << prompt << "\n";