How to search a string for X and replace it with a number and use it to calculate?

Asked

Viewed 95 times

2

I’m creating a C program to do the Newtow-Raphson method, it should automatically calculate for the person. But in one part of the method should be done the automatic substitution of values in the function as for example below:

f(x) = x 3 - 3x -1

f(1) = 1 3 - 3.1 -1

The function the user will type as a String, and the program must take this string to replace the number in the X and use as a calculation. Is there any function ready for this? I’m stuck in this part, so far I have :

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

void painelPerguntas();
double calculoQtIteracao(double raizA, double raizB, double testeParada);
double calculoModuloAB(double raizA, double raizB);

int main(){
    painelPerguntas();

}

void painelPerguntas(){
    double raizA = 0, raizB = 0, testeParada = 0;
    printf("RAIZ [a]: ");
    scanf("%lf", &raizA);
    fflush(stdin);
    printf("RAIZ [B]: ");
    scanf("%lf", &raizB);
    fflush(stdin);
    printf("PARADA: ");
    scanf("%lf", &testeParada);

    //CALCULO QUANTIDADE DE ITERAÇÕES
    calculoQtIteracao(raizA, raizB, testeParada);
    double qtIteracoes = calculoQtIteracao(raizA, raizB, testeParada);

    //CALCULA |A-B|
    calculoModuloAB(raizA, raizB);

    //CALCULA F(A).F(C)


}

double calculoQtIteracao(double raizA, double raizB, double testeParada){
    double calculo = (log10(raizB - raizA) - log10(testeParada)) / log10(2);
    return round(calculo);
}

double calculoModuloAB(double raizA, double raizB){
    double calculo = raizA - raizB;
    if(calculo < 0){
        calculo = calculo * -1;
        return calculo;
    }else {
        return calculo;
    }
}

double calculoSinal(double a, double c){
    double calculoA =
}
  • The function calculoSinal() who is responsible for calculating the mathematical function f(x) = x^3 - 3x -1 ?

3 answers

1


If the intention is to replace x in the string by a value, this is easily solved with a loop:

for (int i = 0; i < strlen(s); i++) {
    if (s[i] == 'x') s[i] = numero;
}

The only problem is that the multiplication signals should be explicit by the user (e.g.: f(x) = x^3 - 3*x -1), otherwise it would be more complicated (would have to increase the string size, move it and etc).

Now, if you also want to calculate the expression, then there is no simple way by itself without resorting to enough code. A solution is to use this library, who apparently does the heavy lifting you need.

  • Yes the user would type in the OUTPUT the function as you typed so for example: x 3 - 3*x -1, I understood what you did above, but now how to use this expression already with the replaced number to calculate and return the result?

  • This part is not something trivial. If you can use external libraries, one solution is Tinyexpr that I gave you.

  • 1

    Ata, so there are only external libraries, and no "default" to use right @Emoon? Thanks.

0

From what I understand you want to locate a number in a correct string?! You can use the find function of the string.

int AcharValor(std::string srt)
{
    if (srt.find('0') != std::string::npos)
        return 0;
    else if (srt.find('1') != std::string::npos)
        return 1;
    else if (srt.find('2') != std::string::npos)
        return 2;
    else if (srt.find('3') != std::string::npos)
        return 3;
    else if (srt.find('4') != std::string::npos)
        return 4;
    else if (srt.find('5') != std::string::npos)
        return 5;
    else if (srt.find('6') != std::string::npos)
        return 6;
    else if (srt.find('7') != std::string::npos)
        return 7;
    else if (srt.find('8') != std::string::npos)
        return 8;
    else if (srt.find('9') != std::string::npos)
        return 9;
}

Reference: https://stackoverflow.com/questions/9642292/function-to-check-if-string-contains-a-number

  • I want to locate the X in a String and then use this String as an expression to calculate, and I’m doing it in C and not in C++.

0

You want two different things at the same time: string text replacement (which just looks at boost.org, which has excellent functions for this) and turn the "source code" of an equation into commands. This second part is more complicated because the real problem is a compiler problem - given a source code, generate a set of computer instructions. You have to search for something that commits this text to a sequence of instructions.

If you don’t want to go to the trouble of making (or finding ready) a mini compiler of equations you could use some of the processing in GPU, like CUDA or Opencl or Compute Shaders. They accept strings as source code to run on the GPU, though not the way you are doing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.