0

I am doing a university project where I have to create a numerical integration calculator. I need the user to input the integrand function to replace the double f(double x) function below so that my program can work for any 1-D integral with non-infinity limits.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double f(double x)
{
    return x;
}

double traprule(double a, double b, double N)
{
    double h, sum, fn, fa, fb, I;

    sum = 0;
    h = (b - a) / N;

    for (int n = 1; n < N; n++)
    {
        fn = f(a + n * h);
        sum = sum + fn;
    }

    fa = f(a);
    fb = f(b);
    I = 0.5 * h * ((fa + fb) + 2 * sum);
    return I;
}

int main()
{
    scanf("%s", &f)
    double result;
    result = traprule(0, 10, 10);
    printf("%lg", result);
    return 0;
}
Toby
  • 9,696
  • 16
  • 68
  • 132
Henry Wang
  • 115
  • 2
  • 4
  • 4
    So you need an expression evaluator ? – Jabberwocky Dec 07 '16 at 15:37
  • http://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work – Anton Malyshev Dec 07 '16 at 15:37
  • 1
    you'll have to specify your input in more detail. your scanf function will not work. – john elemans Dec 07 '16 at 15:38
  • 1
    If you are saying that the user can enter ~arbitrary mathematical expressions for the function, that seems like the hard part of the project, and you have shown no work towards achieving it on your own. And in any case you need to define the allowed input precisely, e.g., what operators are accepted and can well-known functions (like `sin`) be referenced, etc. – Arkku Dec 07 '16 at 15:42
  • What is an expression evaluator? The input is any mathematical function, for example, sin(2)x^2. – Henry Wang Dec 07 '16 at 15:43
  • Possible duplicate of [Evaluating mathematical expressions](http://stackoverflow.com/questions/1151127/evaluating-mathematical-expressions) – phuclv Dec 07 '16 at 15:44
  • You still need to define the syntax for the input precisely, e.g., what other functions than `sin` are supported, what is the precedence of operators, in which circumstances can the multiplication operator be omitted, etc. (For example, is `sqrt(2)(2 + -3x)^π/2^2` acceptable and what is the order of evaluation.) – Arkku Dec 07 '16 at 15:49
  • Is there any other way of doing it without using an expression evaluator? It looks like it is beyond the scope of my project right now. – Henry Wang Dec 07 '16 at 15:51
  • 1
    Evaluating (mathematical) expressions is what you are trying to do, so, no, it cannot be done without an "expression evaluator". Whether it can be done without coding one yourself, yes: find an existing one and use it (assuming it is allowed in your project). For example: https://github.com/codeplea/tinyexpr (as for complexity of the question, this one is called "tiny" and it is over 600 lines). – Arkku Dec 07 '16 at 16:05
  • Can you show me an example of using tinyexpr to convert a scanned string into a mathematical function, then use it to calculate something? Let's say scanning x^2 as a string where x is 2. – Henry Wang Dec 07 '16 at 16:20
  • The github page has examples. – Arkku Dec 07 '16 at 16:26

1 Answers1

4

The C standard library includes nothing that would allow you to parse arbitrary mathematical expressions directly. The implementation of such an expression parser/evaluator is far outside the scope of a Stack Overflow answer, but if you are allowed to use an external library (i.e., this problem is not what your project is about), there are open source options, such as TinyExpr. You should be able to just read the user's input into a string (such as with fgets) and pass it to the library.

(If you are not allowed to use a library, read up on parsers and study the diagrams and source code on the linked github page, then ask a more specific question if you run into problems.)

Arkku
  • 41,011
  • 10
  • 62
  • 84