我的代码替换字符串"The number {0}大于{1}“中的{0}和{1},0和1是变量参数列表的索引,但如果{}中添加了说明符,{0:说明符},c表示货币,f表示固定,i表示int,e表示科学,则数字也是这样格式化的。只有在说明符相同、“数字{0:c}大于{1:c}”、“数字{0:c}大于{1:f}”、“数字{0:C}大于{1:F}”的情况下,代码才能工作,因此由于某种原因,结果都是以第二个索引的任何方式格式化的,在我给出的示例中,这两个索引都将被格式化为固定的。我不知道为什么第二个说明符会影响两个数字,值不会改变,但是第二个说明符会影响两个值。有人有什么想法吗?
我不明白这个问题,因为当程序到达行时
if(prompt.find(c2) != std::string::npos)提示符已经修改过一次,所以应该只有一个{index:说明符},因为第一个索引已经被替换了。我是不是开始这么想了?
#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;
}发布于 2015-04-09 04:21:47
问题是连续搜索您的标识符,如i、f等。
如果在结尾处有一个f string.find(),那么总是会找到f,然后留下If -否则分支。因此,因为f分支位于i分支之前,所以在示例中始终是f,并打印相应的格式。
解决办法是放弃寻找整个f表达式,而只寻找"[“。
发布于 2015-04-09 04:33:55
实际上,我用它来处理这个问题,它非常长,也很乏味,但它很有效。
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";https://stackoverflow.com/questions/29528885
复制相似问题