0

I have a general file which constructs certain shapes and I would like to read in the lines:

0    0      1
0    x(r)   y(r)

where x and y are functions of r, which is a variable taken in as an argument to main, such as 2*r-4.

Then I need to read the columns downwards into arrays.

For columns with just numbers you can do something like:

file >> x[j];

However I am unsure how to get the expressions to be read into the program and then be evaluated and then be put into an array as a number.

I am having problems reading in columns with entries of different types. Ideally I would like each to be assigned like:

temp[0]=1;
temp[1]=2*r-4;

but I am not sure how to do this.

(I am not sure whether the function fscanf would work?)

nikolas
  • 8,707
  • 9
  • 50
  • 70
  • You could make all the rows interpreted as std::function, where constants are just lamda functions `[x](int r){return x}`. – IdeaHat Jul 19 '13 at 19:56

1 Answers1

0

You can read an entire line with file.getline(someString, maxLength). Then you can split the string so you have strings for x(r) and y(r). These strings need to be parsed as mathematical expressions. This question has good information on the subject for c++: Convert string to mathematical evaluation

Community
  • 1
  • 1
827
  • 4,864
  • 4
  • 19
  • 24