#include <stdio.h>
#include "mathexpr.h"
Go to the source code of this file.
Functions | |
int | main () |
int main | ( | ) |
Definition at line 30 of file example.cpp.
References ROperation::Diff(), ROperation::Expr(), ROperation::Val(), x, and z.
00031 { 00032 00033 // The number that the variable "x" will point to 00034 float x; 00035 // Creates a variable named "x" and which value will be x 00036 RVar xvar ( "x" , &x ); 00037 00038 // Asks for a fomula depending on the variable x, e.g. "sin 2x" 00039 char s[500]=""; 00040 printf("Enter a formula depending on the variable x:\n"); 00041 gets(s); 00042 00043 // Creates an operation with this formula. The operation depends on one 00044 // variable, which is xvar; the third argument is an array of pointers 00045 // to variables; the previous argument is its size 00046 RVar* vararray[1]; vararray[0]=&xvar; 00047 ROperation op ( s, 1, vararray ); 00048 00049 // Affects (indirectly) a value to xvar 00050 x=3; 00051 // Printfs the value of the formula for x=3; 00052 printf("%s = %G for x=3\n\n", op.Expr(), op.Val() ); 00053 00054 // Creates a function name which can be used in later functions to refer to the operation op(x) 00055 RFunction f (op, &xvar); f.SetName("f"); 00056 00057 // Creates a second variable named y, and a formula depending on both x and y 00058 float y; 00059 RVar yvar ( "y" , &y ); 00060 RVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar 00061 vararray2[0]=&xvar; vararray2[1]=&yvar; 00062 00063 // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y) 00064 printf("Enter a formula using x, y and the function f: x |-> %s that you just entered :\n", op.Expr() ); 00065 gets(s); 00066 RFunction* funcarray[1]; funcarray[0]=&f; 00067 ROperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray ); 00068 // vararray2 is a RVar* array with two elements 00069 // funcarray is a RFunction* array with one element 00070 y=5; 00071 printf("Value for x=3, y=5 : %G\n", op2.Val() ); 00072 00073 // Turns the last expression into a function of x and y 00074 RFunction g(op2, 2, vararray2); g.SetName("g"); 00075 00076 // Here is another way to do it 00077 float z,t; 00078 RVar zvar("z", &z), tvar("t", &t); 00079 ROperation op3,zop,top; 00080 zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value 00081 00082 op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( ) 00083 // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2 00084 00085 z=5;t=7; 00086 printf("\nLet g be the function g : x,y |-> %s\n", op2.Expr() ); 00087 printf("Value of %s for z=5,t=7:\n %G\n", op3.Expr(), op3.Val() ); 00088 00089 ROperation dopdt = op3.Diff(tvar); // Computes the derivative of op3 w.r.t t 00090 printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %G\n", dopdt.Expr(), dopdt.Val() ); 00091 00092 return 0; 00093 }