如何从用户那里获取方程的输入并在 c++ 中对其进行评估

How to get input of an equation from user and evaluate it in c++

本文关键字:c++ 评估 输入 用户 那里 获取 方程      更新时间:2023-10-16

我想从用户那里获取一个数学(单变量代数)方程,比如x^3 - 4x -9 = 0,并想针对不同的x值对其进行评估。

我尝试创建两个数组;一个用于获取x的幂输入,另一个用于获取系数。程序向用户询问方程中存在的项数,然后生成该数字 1 的数组。 但是,此算法有助于仅打印方程,而不操作或计算方程。

/* 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<<"nntt How many terms do you have in your equation? ";
cout<<"nt For Example:  x^3 - 4x - 9 = 0   , has '3' terms     and ";
cout<<"nt               x^4 + x^3 - 7x^2 - x + 5 = 0   , has '5' terms";
cout<<"nt 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<<"tt Please enter the "<<c<<" th/st/nd/rd highest degree of x:  ";
cin>>this->powerarr[i];
cout<<endl<<endl<<"tt Please enter the coefficient of "<<c<<" th/st/nd/rd highest degree of x (with sign -/+):  ";
cin>>this->coeffarr[i];
}
cout<<endl<<endl<<"nnt Values Set!";
getch();

}
void Bisection::showeqn()
{
int i, n;
n = this->noofcaparr-1;
system("cls");
cout<<endl<<endl<<"tt 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);
}

尝试检查此代码。如果它要求输入,那么让我们尝试一个例子:在第一个输入中,键入 3,在第二个3中键入 1,然后键入1,然后键入1,然后键入 -4,然后键入0,然后键入-9这将在屏幕上打印以下公式:x^3 - 4x - 9 = 0

但是,我无法操纵或评估这个等式。如果我想通过使其等于fx并取不同的x值来计算方程,然后评估fx的值,我不能这样做。

我已经尝试在互联网上搜索它,但所有解决方案要么没有帮助,要么太复杂而无法理解。

我是一个非常新的程序员,我对数据结构,野牛或任何类似的解析器一无所知。请以简单的方式解释/帮助我,尽可能简单。

请不要投反对票,如果您在问题中发现任何错误,请在评论中告诉我;我会把我的问题记下来。 提前感谢!

正如NO_NAME所认为的那样,计算函数并不难,尽管在当前的数据布局下,我们无法用他建议的迭代来计算power,因为没有每个指数的术语。但是这个变体有效:

double Bisection::evalfun(int x)
{
double f = 0;
for (int i = 0; i < this->noofcaparr; ++i)
f += coeffarr[i] * pow(x, powerarr[i]);
return f;
}

示例调用不同的 x 值

cout <<endl;
for (int x = -5; x <= 5; ++x) cout <<a.evalfun(x) <<'t';
cout <<endl;

也许你想使用double x而不是int x