A while back i wanted to develop an application that calculated the integral of any polynomial that was input but I felt it was lacking the capability to to find more complex polynomials such as:
(x^2+x)/(x^2+x^3)
Unfortunately my attempts at implementing a numerator and denominator as part of the program have failed. So far I have written the following code:
#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)); //x term
y=(e)*(h); //numerator for the function
return (y);
}
void ParseTerm(string term, double& coeff, double& exp)
{
const string TOKEN = "x^"; //The term to replace
const int TOKEN_LENGTH = TOKEN.length(); //The length of the search term
const int index = term.find(TOKEN); //Finds the term in the string and writes the position as an integer
term.replace(index, TOKEN_LENGTH, " "); //Replaces the term at position index with a space
stringstream ss (term, stringstream::in | stringstream::out); //The non-spaced characters in the string are designated ss
ss >> coeff; //Outputs the preceding number in the stream as the coefficient
ss >> exp; //Outputs the following number in the stream as the exponent
}
void TurnPlusesToSpaces (string& equation)
{
int foundp, foundm;
foundp=equation.find("+");
foundm=equation.find("-");
while (foundp*foundm != 1) {
const string TOKENA = "+"; //The term to replace
const int TOKENA_LENGTH = TOKENA.length(); //The length of the search term
int indexa = equation.find(TOKENA); //Outputs the position of the term as an integer
if (indexa!=-1) {
equation.replace(indexa, TOKENA_LENGTH, " "); //Replaces the term with a space in the string equation
}
//cout << equation << "\n";//Debug purposes, outputs equation in current loop
const string TOKENB = "-"; //The term to replace
const int TOKENB_LENGTH = TOKENB.length(); //The length of the search term
int indexb = equation.find(TOKENB); //Outputs the position of the term as an integer
if (indexb!=-1) {
equation.replace(indexb, TOKENB_LENGTH, " ");
}
foundp=equation.find("+");
//cout << foundp;
foundm=equation.find("-");
//cout << foundm;
//cout << equation << "\n";
}
}
int main()
{
double coeffn, expn, coeffd, expd, Suma, Sumb, xmin, xmax, x, inc, y1, y2, y, Area; //Declaration of essential variables
int k;
k = 1;
string terma, termb;
string numerator = "1x^2+2x^1+1x^0"; //Declaration of the string for the numerator
string denominator = "1x^2+2x^1+1x^0";
do {
Suma = 0;
Sumb = 0;
cout << "\n" << "What is the numerator? \n";
cin >> numerator; //Input for the numerator
cout << "\n" << "What is the denominator? \n";
cin >> denominator; //Input for the denominator
/*cout << "How many plus signs exist in the numerator? \n";
cin >> re; //Input for number of plus signs*/
cout << "What is the lower boundary? \n";
cin >> xmin; //The lower boundary
cout << "What is the upper boundary? \n";
cin >> xmax; //The upper boundary
cout << "In what increments do you wish to integrate? \n";
cin >> inc; //Steps between each calculation, increases calculation time
string term = "000x^000"; //Test term
x = xmin; //Sets x to the first value
xmax -= inc; //Prevents inaccuracy due to the upper boundary
TurnPlusesToSpaces (numerator); //Calls earlier function
//cout << numerator << "\n"; //Outputs the numerator
stringstream ssa(numerator, stringstream::in | stringstream::out);
//cout << terma << "\n";
TurnPlusesToSpaces (denominator); //Calls earlier function
//cout << denominator << "\n"; //Outputs the numerator
stringstream ssb(denominator, stringstream::in | stringstream::out);
//cout << termb << "\n";
do {
while (ssa >> terma) {
ParseTerm(terma, coeffn, expn);
cout << coeffn << " " << expn << "\n";
y1=fabs(f1(x, expn, coeffn)); //First y value
//cout << y1;
Sumb+=y2;
};
while (ssb >> termb) {
ParseTerm(termb, coeffd, expd);
cout << coeffd << " " << expd << "\n";
y2=fabs(f1(x, expd, coeffd)); //First y value
//cout << y2;
Sumb+=y2;
};
y=Suma/Sumb;
x+=inc;
Area+=(y)*inc;
} while (x<=xmax);
cout << "\n Integral=" << setprecision(12) << Area;
cout << "\n Press 1 then enter to rerun or press 0 and enter to quit. \n";
cin >> k;
} while (k==1);
return 0;
}
I understand almost all of it but I lack a good understanding of the operation of stringstream
function. I was wondering if someone could assist me in finding where I've gone wrong and helping me to correct it.
The input format is: 1x^2+1x^1+1x^0
and it goes on like that.
I have several commented lines of code which I was using for debugging purposes. If anyone could lend a helping hand I'd be grateful because I can't figure out where I went wrong. If you could explain where I went wrong rather than give a corrected code it would be great since it would help me to hone my c++ skills and knowledge.
Cheers!
Microno