I want to take a mathematical (single variable algebraic) equation from a user, say x^3 - 4x -9 = 0 and want to evaluate it for different values of x.
I've tried creating two arrays; one for getting the input of powers of x and another for coefficients. The program asks from the user about the number of terms present in the equation and then it makes the arrays of that number-1. But, this algorithm is helpful in only printing the equation and not manipulating or evaluating it.
/* this code can take some relevant input from the user and form an equation on the screen to show it to the user. */
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
class Bisection
{
int noofcaparr, *coeffarr, *powerarr, eqn;
char *eqnprnt;
public:
void geteqn();
void showeqn();
void setupeqn();
};
void Bisection::geteqn()
{
int c, i, n;
system("cls");
cout<<"\n\n\t\t How many terms do you have in your equation? ";
cout<<"\n\t For Example: x^3 - 4x - 9 = 0 , has '3' terms and ";
cout<<"\n\t x^4 + x^3 - 7x^2 - x + 5 = 0 , has '5' terms";
cout<<"\n\t Enter the number of terms present in your equation: ";
cin>>this->noofcaparr;
n = this->noofcaparr-1;
this->coeffarr = new int[n];
this->powerarr = new int[n];
for(i=0, c=1; i<=n; i++, c++ )
{
cout<<endl<<endl<<"\t\t Please enter the "<<c<<" th/st/nd/rd highest degree of x: ";
cin>>this->powerarr[i];
cout<<endl<<endl<<"\t\t Please enter the coefficient of "<<c<<" th/st/nd/rd highest degree of x (with sign -/+): ";
cin>>this->coeffarr[i];
}
cout<<endl<<endl<<"\n\n\t Values Set!";
getch();
}
void Bisection::showeqn()
{
int i, n;
n = this->noofcaparr-1;
system("cls");
cout<<endl<<endl<<"\t\t Your equation is: ";
for(i=0; i<=n; i++ )
{
if(this->powerarr[i]==0)
{
if(i==0)
{
if(this->coeffarr[i]>= 0)
{
if(this->coeffarr[i]==1)
{
cout<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" ";
}
}
else
{
if(this->coeffarr[i]== -1)
{
cout<<" -"<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" ";
}
}
}
else
{
if(this->coeffarr[i]>= 0)
{
cout<<" +"<<(this->coeffarr[i])<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" ";
}
}
}
else
{
if(this->powerarr[i]==1)
{
if(i==0)
{
if(this->coeffarr[i]>= 0)
{
if(this->coeffarr[i]==1)
{
cout<<"x";
}
else
{
cout<<(this->coeffarr[i])<<"x";
}
}
else
{
if(this->coeffarr[i]== -1)
{
cout<<" -"<<"x";
}
else
{
cout<<(this->coeffarr[i])<<"x";
}
}
}
else
{
if(this->coeffarr[i]>= 0)
{
cout<<"+"<<(this->coeffarr[i])<<"x";
}
else
{
cout<<(this->coeffarr[i])<<"x";
}
}
}
else
{
if(i==0)
{
if(this->coeffarr[i]>= 0)
{
if(this->coeffarr[i]==1)
{
cout<<"x^"<<this->powerarr[i]<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<" ";
}
}
else
{
if(this->coeffarr[i]== -1)
{
cout<<" -"<<"x^"<<this->powerarr[i]<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<" ";
}
}
}
else
{
if(this->coeffarr[i]>= 0)
{
cout<<" +"<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<" ";
}
else
{
cout<<" "<<(this->coeffarr[i])<<" "<<"x^"<<this->powerarr[i]<<" ";
}
}
}
}
}
cout<<" = 0";
getch();
}
int main()
{
Bisection a;
a.geteqn();
a.showeqn();
getch();
return(0);
}
Try checking this code. If it asks for input then let's try an example: In the first input, type 3, in second 3, then 1, then 1, then -4, then 0 and then -9. This will print the following equation on the screen: x^3 - 4x - 9 = 0
But, I cannot manipulate or evaluate this equation. If I want to calculate the equation by making it equal to fx and taking different values of x and then evaluate the value of fx, I cannot do that.
I have already tried searching for it on the internet but all of the solutions were either unhelpful or too complex to understand.
I am a very new programmer and I know nothing about data structure, bison or any similar parser. Please explain/help me in a simple way, as simple as possible.
Please do not downvote, if you find anything wrong in the question then, let me know in the comments; I'll take my question down. Thanks in advance!