C 标记多项式系数

发布于 2024-08-12 16:37:53 字数 726 浏览 5 评论 0原文

我试图将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

御守 2024-08-19 16:37:53

这:

strncpy(temp[z], p, z);

需要是:

strncpy(temp, p, z);

但请记住,strncpy 并不总是以空字符结尾字符串。

另外,z 将是系数的长度,但缓冲区中需要一个额外的字节作为空终止符。

更新:

检查您的链接,我仍然看到几个严重的问题:

  • 您不能在 strtok 中使用“-”,因为它会选择“-4x”中的以及您想要的。我认为你应该只分割空格并将 +/- 运算符作为标记处理。
  • strncpy 函数使字符串未终止,这可能会导致 atoi 崩溃或随机给出错误的值。一种惯用的形式是手动编写终止符,例如,temp[z] = '\0'
  • 您没有获得任何输出值的原因是 coefficient[a] = 正在写入某些随机内存,因为 a 尚未初始化。

This:

strncpy(temp[z], p, z);

Needs to be:

strncpy(temp, p, z);

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:

  • You can't use "-" in 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.
  • The strncpy function leaves the string un-terminated, which may cause atoi to crash or give the wrong value randomly. One idiomatic form is to write the terminator manually, e.g., temp[z] = '\0'.
  • The reason you're not getting any output values is that coefficient[a] = is writing to some random memory because a is uninitialized.
谷夏 2024-08-19 16:37:53

您将 char 传递给 strncpy

  strncpy(temp[z], p, z);

第一个参数应该是 char* 指针,而不是单个 char >。您可能想做的是:

  strncpy(temp, p, z);

You're passing a char to strncpy:

  strncpy(temp[z], p, z);

The first argument should be a char* pointer, not a single char. What you probably mean to do is:

  strncpy(temp, p, z);
风吹短裙飘 2024-08-19 16:37:53

其他人关于 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.

给我一枪 2024-08-19 16:37:53

这种解决方案可以很容易地实现,但为了使其能够防止额外的空白、缺少空白以及足够广泛的范围来处理多个运算符和变量名,这种策略变得越来越复杂和困难(特别是如果您需要有有意义的错误)如果解析失败则显示消息)。

在我看来,使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文