简体   繁体   English

C ++替换字符串的一部分

[英]C++ replacing part of string

I have this code that replaces the {0} and {1} in the string "The number {0} is greater than {1}" with 0 and 1 being the index of a list of variable arguments, but if inside the { } their is a specifier added, {0:[specifier]}, c for currency, f for fixed, i for int, or e for scientific, the number also gets formatted that way. 我有这段代码将字符串“数字{0}大于{1}”中的{0}和{1}替换为0和1是变量参数列表的索引,但是如果在{}内它们是添加的说明符,{0:[specifier]},c表示货币,f表示固定,i表示int,e表示科学,数字也采用这种格式。 The code is only working if the specifiers are the same, "The number {0:[c]} is greater than {1:[c]}", if they are different, "The number {0:[c]} is greater than {1:[f]}", then the result for some reason is both are formatted in whichever way the second index is, in the example i gave both would be formatted as fixed. 仅当说明符相同时,代码才有效:“数字{0:[c]}大于{1:[c]}”;如果它们不同,则“数字{0:[c]}大于{1:[f]}”,则无论出于何种原因,都以第二个索引采用的任何一种方式格式化结果,在本例中,我给出的两个索引都将格式化为固定格式。 I don't know why the second specifier is affecting both numbers, the values don't change but the second specifier affects both values. 我不知道为什么第二个说明符影响两个数字,值没有改变,但是第二个说明符影响两个值。 any one have any ideas? 有人有想法么?

I don't understand the issue, because when the program reaches the line 我不明白这个问题,因为当程序到达该行时

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

prompt has been modified once already so there should only be one {index:[specifier]} to be found because the first one was already replaced. 提示已经被修改过一次,因此只能找到一个{index:[specifier]},因为第一个已被替换。 Am i off on this line of thinking? 我是不是按照这种思路?

#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;
}

The problem is the consecutive search for your identifiers like [i], [f] and so on. 问题是连续搜索您的标识符,例如[i],[f]等。

If u have at the end a [f] string.find() will ALWAYS find [f] and then leave your if-else-branches. 如果您最后有一个[f] string.find()总是会找到[f],然后离开if-else分支。 So because the [f] branch is before the [i] branch you find in your example always [f] and print the corresponding format. 因此,由于[f]分支位于[i]分支之前,因此在示例中始终为[f]并打印相应的格式。

The solution is to quit seeking the whole [f] expression but to seek only for "[". 解决的办法是放弃寻找整个[f]表达式,而只寻找“ [”。

I actually got it to work with this, its extremely long and tedious but it works. 实际上,我可以使用它,它非常冗长且乏味,但是可以使用。

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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM