C 标记多项式系数
我试图将 char 数组中的多项式系数放入 int 数组
我有这个:
char string[] = "-4x^0 + x^1 + 4x^3 - 3x^4";
并可以通过空格将其标记为
-4x^0
x^1
4x^3
3x^4
所以我试图将 -4, 1, 4, 3 转换为 int 数组
int *coefficient;
coefficient = new int[counter];
p = strtok(copy, " +");
int a;
while (p)
{
int z = 0;
while (p[z] != 'x')
z++;
char temp[z];
strncpy(temp[z], p, z);
coefficient[a] = atoi(temp);
p = strtok(NULL, " +");
a++;
}
但是,我收到一个错误,我无法将 char* 转换为 char on strncpy(temp[z], p, z);
error: invalid conversion from ‘char’ to ‘char*’
error: initializing argument 1 of ‘char* strncpy(char*, const char*, size_t)’
最好的方法是什么?
I'm trying to put the coefficients of polynomials from a char array into an int array
I have this:
char string[] = "-4x^0 + x^1 + 4x^3 - 3x^4";
and can tokenize it by the space into
-4x^0
x^1
4x^3
3x^4
So I am trying to get: -4, 1, 4, 3 into an int array
int *coefficient;
coefficient = new int[counter];
p = strtok(copy, " +");
int a;
while (p)
{
int z = 0;
while (p[z] != 'x')
z++;
char temp[z];
strncpy(temp[z], p, z);
coefficient[a] = atoi(temp);
p = strtok(NULL, " +");
a++;
}
However, Im getting an error that I cant convert a char* into a char
on strncpy(temp[z], p, z);
error: invalid conversion from ‘char’ to ‘char*’
error: initializing argument 1 of ‘char* strncpy(char*, const char*, size_t)’
What would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这:
需要是:
但请记住,strncpy 并不总是以空字符结尾字符串。
另外,
z
将是系数的长度,但缓冲区中需要一个额外的字节作为空终止符。更新:
检查您的链接,我仍然看到几个严重的问题:
strtok
中使用“-”,因为它会选择“-4x”中的以及您想要的。我认为你应该只分割空格并将 +/- 运算符作为标记处理。strncpy
函数使字符串未终止,这可能会导致atoi
崩溃或随机给出错误的值。一种惯用的形式是手动编写终止符,例如,temp[z] = '\0'
。coefficient[a] =
正在写入某些随机内存,因为a
尚未初始化。This:
Needs to be:
But remember that
strncpy
doesn't always null-terminate the string.Also,
z
will be the length of the coefficient, but you need an extra byte in the buffer for the null terminator.Update:
examining your link, I still see several serious problems:
strtok
because it will pick up the one in "-4x" as well as the ones you want. I think you should split on spaces only and handle the +/- operators as tokens.strncpy
function leaves the string un-terminated, which may causeatoi
to crash or give the wrong value randomly. One idiomatic form is to write the terminator manually, e.g.,temp[z] = '\0'
.coefficient[a] =
is writing to some random memory becausea
is uninitialized.您将
char
传递给strncpy
:第一个参数应该是
char*
指针,而不是单个char
>。您可能想做的是:You're passing a
char
tostrncpy
:The first argument should be a
char*
pointer, not a singlechar
. What you probably mean to do is:其他人关于 strncpy() 进入 temp 而不是 temp[z] 的说法是正确的。
我建议您还想捕获自由变量的指数。我观察到您似乎忽略了一个隐含的“0x^2”术语。如果您的下一步是针对不同的 x 值评估多项式(或者更糟糕的是,对其运行求解器),您将需要知道这些幂。
The other guys are correct about strncpy()'ing into temp rather than temp[z].
I'm going to suggest that you also want to capture the exponents on your free variable. I observe an implicit "0x^2" term that you appear to be neglecting. If your next step is to evaluate your polynomial for various values of x (or, worse, run a solver on it), you will need to know those powers.
这种解决方案可以很容易地实现,但为了使其能够防止额外的空白、缺少空白以及足够广泛的范围来处理多个运算符和变量名,这种策略变得越来越复杂和困难(特别是如果您需要有有意义的错误)如果解析失败则显示消息)。
在我看来,使用 boost.regex (或者如果整个任务需要分析操作顺序,甚至是 boost.spirit )来实现防弹解决方案会更容易,它可以轻松处理具有很大程度容忍度的此类语法。
This kind of a solution can be made easily enough but to make it proof against extra white space, missing white space and broad enough to handle multiple operators and variable names this kind of strategy increasingly complex and difficult (Especially if you need to have meaningful error messages if the parse fails).
Seems to me it would be easier to implement a bullet-proof solution using boost.regex (or even boost.spirit if the overall task requires order of operations to be analysed) which can easily handle these kind of syntaxes with a large degree of tolerance.