English 中文(简体)
C tokenize polynomial coefficients
原标题:

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?

最佳回答

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] = .
  • The reason you re not getting any output values is that coefficient[a] = is writing to some random memory because a is uninitialized.
问题回答

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

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.

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.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签