I've written a program that allows you to calculate the definite integral of a function but I feel that it is limited and I want to rewrite it so that the user inputs an equation which is then stored as a string and then that string becomes the equation for the double float variable that I want to use. I have two ideas on how it could be implemented but I don't know how to code it. One method would be for the program to go through the string and take each character in it and append it to the double float variable. Two the program could convert each character, create an array where each target is one character from the string and then these are appended to the end to the end of the double float variable. I'm a beginner so if possible could you also explain what each function means cause it's all Greek to me at this point.

This is my initial code:

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

double f1(double a, double b, double e,double d) //My function for the y values
{
    double y, h;
    h=(powf(a, b));
    y=(e)*(h)+d; //Equation for the function
    return (y);
}

int main()
{
        double xmin, xmax, inc, g, coex, x, c, Sum, expox, y1; //My variables
        Sum=0;
    do {
        cout << "What is the coefficient of x? ";
        cin >> coex;
        cout << "What is the exponent of x? ";
        cin >> expox;
        cout << "Is there a constant? ";
        cin >> c;
        cout << "What is the lower boundary? ";
        cin >> xmin;
        cout << "What is the upper boundary? ";
        cin >> xmax;
        cout << "In what increments do you wish to integrate? ";
        cin >> inc;
        xmax=xmax-inc;
        x=xmin;
        Sum=0;
            do {
                y1=fabs(f1(x, expox, coex, c)); //First y value
                Sum=Sum+(y1)*inc; //The Sum of the area under the graph
                x=x+inc;
                } while (x<=xmax);
        cout << "\n Integral=" << setprecision(12) << Sum;
        cout << "\n Press 1 then enter to rerun or press 0 and enter to quit. \n";
        cin >> g;
    } while (g==1);
    if (g==0)
        {
            cout << "Goodbye! I hope I've been able to help! Designed and coded by Ekin Ozturk. Please do not take credit for something you haven't done. :)";
            cin.get();
        }
    }

I checked that and I've been reading but now I'm more confused than I was when I started. What do these functions do? Like I read it but if someone could explain in English it would be great.

Stringstreams are a method of HOW to do something. I'm confused about what precisely you are trying to do. You're creating a program to compute definite integrals. You have a program that apparently works. I'm with you so far. You feel it's "limited" and want to improve it. Understood to a point, but you haven't elaborated what the "limit" of your old program is and what the envisioned new program should do that the old one cannot. Presumably integrate a larger variety of integrals?

What is the user supposed to type into this string? I can't offer an opinion of the HOW until I understand the WHAT. What is this program supposed to handle as far as input? Just as importantly, what does it NOT need to be able to handle?

Also, what is a "double float"? There are floats, doubles, and long doubles. I've never heard of a "double float". I assume that's just a double?

Yeah a double float is a double, I just find it easier to remember it as a double float since it stands for a double precision floating point number.

My purpose here is to go beyond what I initially programmed which only allows for equations such as: x^2+5, x^3+2, etc. with only one x variable inside it. I want to make the program capable of taking user input for the equation such as: x^3+5x^2-x+1 and calculate the integral based on that.

The program needs to be able to handle user input as though it were the equation for a variable. So it should treat the string as though it were like the equation

Sum=Sum+(y1)*inc

I hope that makes sense. The user should be able to input any sort of equation for the variable y and the program should be able to interpret it and calculate a value based on the "x" variable.

>> The user should be able to input any sort of equation for the variable y

One step at a time. If I may, to make your life easier as a beginner, here's a rephrasing that may both help and may also point out a few issues.


"The user should be able to input any sort of WELL-FORMED POLYNOMIAL equation for the variable y."


What's the difference? Well, for one, the word "polynomial". "Any sort of equation" would include logarithms, sines, tangents, factorials, etc. I imagine you meant polynomials only, but it's always good to say so explicitly.

"Well formed". What's that mean? One, it means you don't have to validate that they enter. If they enter jibberish, the program does not need to catch that and correct them. Also, you can make your life easier and require the input to follow a format that makes your parsing job easier. For example, don't let them enter "x^3+5x^2-x+1", make them enter "1x^3+5x^2-1x^1+1x^0". This makes your job parsing job easier. I would also suggest that at least in the beginning, you require the user to enter all subtraction as adding a negative, so it turns into this...

"1x^3+5x^2+-1x^1+1x^0".

"-1x^1" has turned into "+-1x^1".

It's less appealing to the eye, but one less thing to keep track of when you're programming.

Finally, no spaces allowed between terms. You can make it more complicated after you handle the easy case above. Your job is to break this...

"1x^3+5x^2+-1x^1+1x^0"

into separate terms. How do you do this? Well, note that I required all terms to be separated by a + sign. Now we have a string and replace all '+' characters into spaces.

To do that, you have at least two options...

  1. Go through the string character by character looking for '+' and when you find one, replace it with a space.
  2. Look at the string library and see if it has any useful functions to help do that for you.

Now you turn the string into that stringstream that another poster mentioned and you extract a term at a time.

Once you have a term, you need to parse that into a coefficient and an exponent. Once you have that, you can take it from there, I think.


All these steps are probably overwhelming if attempted all at once. My advice would be to break it into steps. First step... Write a program that takes the string "3x^7" and parses it into a 3 and a 7 and prints them.

Ok, thank you so much. So I guess I will use the stringstream function to look at each character.
1) If the first character is a number I will tell it to place it as a coefficient.
2) Then go onto the second character where if the second character is also a number or a dot then append it to the end of the coefficient value.
3) Then when it reaches the letter x I will tell it to append it to the end of the coefficient with a "*" before it in order to tell it to multiply it.
4a) Then I will tell it to look at the characters afterwards and if there is a "^" sign I will tell it to place all the following characters until a "+" sign into the the function pow as pow(x, "characters from string"-1) and tell it to append this value to the end of the variable in parentheses with a "*" in front of it.
4b) If there is no "^" sign then it will continue with the rest, repeating the previous steps until the end of the string.

OR

1) Take all the characters from the beginning till the "x" and append it to the variable "y".
2) Take all the characters from the "x" till the "+" and append it to the end of the "y" variable as the function pow(x, "characters from string") in parentheses.
3) If there is "+" sign then repeat previous three steps, if not then end loop and continue with main function.

I think the second one seems to be much easier to implement since it sounds less confusing but then when it comes to programming sounding simple isn't enough. I really don't know how to use stringstream to do this so anything that would help me better understand would be very welcome.

Thank you so much!

Yes, you can do this, but if you require everything to be "well-formed" as I defined it, you won't have to do a lot of it. See the rules I made above. The code assumes the input follows these rules.

Here's an example to get you started. Stringstreams and string functions can be confusing. Sometimes it is easiest to learn by following an example that works. It uses stringstreams to parse a term. It also uses stringstreams to parse the equation into terms (I guess it's not really an "equation", but you know what I mean).

#include <iostream>
#include <sstream>
#include <string>
using namespace std;


void ParseTerm(string term, double& coeff, double& exp)
{
    // term is "87x^125"
    const string TOKEN = "x^"; // technical term for the delimiter to look for is "token", so we name it "TOKEN".
    const int TOKEN_LENGTH = TOKEN.length(); // TOKEN_LENGTH is 2
    const int index = term.find(TOKEN); // index is 2 because "x^" is located at position 2 of the string.
    term.replace(index, TOKEN_LENGTH, " ");  // replace "x^" with a space.  term is now "87 125"
    stringstream ss (term, stringstream::in | stringstream::out); // stream constains "87 125"
    ss >> coeff; // extract 87
    ss >> exp; // extact 125
}


void TurnPlusesToSpaces(string& equation)
{
    // you write this.  Replace line below with the real code.
    equation = "87x^125 89x^5 -673x^7843";
}


int main()
{
    double coeff, exp;

    // an example of a single term
    string term = "87x^125";
    ParseTerm(term, coeff, exp);
    cout << coeff << " " << exp << "\n"; // prints out "87 125"

    // an example of an equation
    string equation = "87x^125+89x^5+-673x^7843";
    TurnPlusesToSpaces(equation); // equation is now "87x^125 89x^5 -673x^7843"
    stringstream ss (equation, stringstream::in | stringstream::out);

    while(ss >> term)
    {
        ParseTerm(term, coeff, exp);
        cout << coeff << " " << exp << "\n";
    }

    return 0;
}

http://www.cplusplus.com/reference/iostream/stringstream/
http://www.cplusplus.com/reference/string/string/


Hope this helps.

That's great. Thank you so much. This is going to be very useful!

Cheers!

#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
#include <iomanip>

using namespace std;

double f1(double a, double b, double e) //My function for the y values
{
    double y, h;
    h=(powf(a, b));
    y=(e)*(h); //Equation for the function
    return (y);
}

void ParseTerm(string term, double& coeff, double& exp)
{
    const string TOKEN = "x^";
    const int TOKEN_LENGTH = TOKEN.length();
    const int index = term.find(TOKEN);
    term.replace(index, TOKEN_LENGTH, " ");
    stringstream ss (term, stringstream::in | stringstream::out);
    ss >> coeff;
    ss >> exp;
}

void TurnPlusesToSpaces (string& equation)
{
    const string TOKEN = "+";
    const int TOKEN_LENGTH = TOKEN.length();
    const int index = equation.find(TOKEN);
    equation.replace(index, TOKEN_LENGTH, " ");
    stringstream ss (equation, stringstream::in | stringstream::out);
    ss >> equation;
}

int main()
{
    double coeff, exp, Sum, xmin, xmax, x, inc, y1;
    xmin = 0;
    xmax = 2;
    inc = 0.1;
    Sum = 0;
    string term = "1x^1";
    x = xmin;
    xmax = xmax-inc;
    string equation = "1x^2 2x^1 1x^1";
    cin >> equation;
    stringstream ss(equation, stringstream::in | stringstream::out);

    while (ss >> term)
    {
       ParseTerm(term, coeff, exp);
        do {
            y1=fabs(f1(x, exp, coeff)); //First y value
            Sum=Sum+(y1)*inc; //The Sum of the area under the graph
            x=x+inc;
            } while (x<=xmax);
    }
    cout << setprecision(3) << Sum << "\n";
    return 0;
}

This is what I have gotten so far and I don't get the results I want and I can't seem to figure out why. Could someone help?

This question has already been answered. Start a new discussion instead.