Math parser conversion to c++ from c

Hello, today I found a great mathematical expression parser at http://www.yann-ollivier.org/mathlib/mathexpr.php
But there is a problem, it is written in C. I know C can be used in C++ but I do not understand the code in C. I'm wondering if there is a program that can convert c to c++ or if anyone would be willing to do it?
The total amount of code that needs to be converted is about 1400 lines so changing it by hand might be tough.
There is a c++ version along with the c version in the archive that you download from that site.

In fact it all seems to be c++, it just doesn't use the c++ standard libs. I doubt if you will find a converter for that.
Last edited on
Oh okay, well I need help understanding this program, lines 22-29 and how I could change it to do something else:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include "mathexpr.h"
#include <iostream>

using namespace std;
int main()
{

 // The number that the variable "x" will point to
double x;
 // Creates a variable named "x" and which value will be x
 RVar xvar ( "x" , &x );

 // Asks for a fomula depending on the variable x, e.g. "sin 2x"
 char s[500]="";
cout << "Enter a formula depending on the variable x:\n";
gets(s);

 // Creates an operation with this formula. The operation depends on one
 // variable, which is xvar; the third argument is an array of pointers
 // to variables; the previous argument is its size
 RVar* vararray[1]; vararray[0]=&xvar;
 ROperation op ( s, 1, vararray );

 // Affects (indirectly) a value to xvar
 x=3;
 // Printfs the value of the formula for x=3;
 printf("%s = %G for x=3\n\n", op.Expr(), op.Val() );
 cin.get();

 return 0;
}
Last edited on
Here is what I *think* is going on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
double x; // this is your variable for your equation. 

RVar xvar ( "x" , &x ); // RVar seems to be keeping a reference to your variable for the engine

// vararray seems to be an array of variables to be operated on 
RVar* vararray[1]; 

// In this example kust one variable is being put in the array
vararray[0]=&xvar;

// The list of variables is now being sent to the expression processing engine.
// The s is a string expression in x such as "x^2-x*4+2"
ROperation op ( s, 1, vararray );

// No the op object can be queried for the input expression(op.Expr())
and the output value (op.Val()) that will depend on the input value of x

x=3;
printf("%s = %G for x=3\n\n", op.Expr(), op.Val() );

// the printf() statement is the equivalent of this:

std::cout << op.Expr() << " = " << op.Val() << " for x=" << x << "\n\n";


Topic archived. No new replies allowed.