How to search in a String and replace X with a number and use the same to calculate?

2

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

  

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 should take that string to replace the number in X and use as a calculation. Is there a function ready for this? I'm standing 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 =
}
    
asked by anonymous 06.11.2016 / 04:19

3 answers

1

If the intention is to replace x in the string with 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 (eg f(x) = x^3 - 3*x -1 ), otherwise it would be more complicated (you would have to increase the size of the string, move it and so on).

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

    
06.11.2016 / 13:07
0

So I understand you want to find 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: link

    
06.11.2016 / 05:06
0

You want two different things at once: replacing string text (you just have to look at boost.org, which has great functions for that) and transform 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, generating a set of computer instructions. You have to look for something that compiles this text for a sequence of instructions.

If you do not want to bother doing or finding a miniature equation compiler you could use some of the GPU, CUDA or OpenCL or Compute Shaders processing. They accept strings as source code to run on the GPU, though not the way you are doing it.

    
13.10.2017 / 22:02