如何从用户那里获取方程的输入并在 c++ 中对其进行评估
How to get input of an equation from user and evaluate it in c++
我想从用户那里获取一个数学(单变量代数)方程,比如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
。
相关文章:
- std::condition_variable::wait()如何评估给定的谓词
- c++11评估顺序(未定义的行为)
- 如何使用"equal to"以外的评估编写开关语句
- 嵌套 if 中没有返回评估
- 懒惰的参数评估try_emplace?
- 在实践中,在运行时为零的乘法中是否有任何"lazy"评估
- 如何改进一堆在已知值范围内评估变量的 else-if 条件?
- C++17:使用 std::optional 来评估枚举是否包含值
- CNTK:->转发或 ->评估某些电脑上的崩溃,而不是其他电脑上的崩溃
- ConstexPR :GCC比Clang更努力地评估ConstexPR
- C++:Constexpr斐波那契数列评估
- C++能保证论点评估的原子性吗?
- 在编译时评估函数开销的通用方法
- 如何使用 std::forward 精确地评估参数包的扩展?
- MSVC 在不知道类型的情况下评估上下文(和错误)
- C++评估顺序优化是否意味着对不同的操作数使用不同的内核?
- 条件分支评估
- C++ constexpr语言 - 可以在编译时评估值吗?
- 井字游戏:评估节点的启发式值
- 布尔值始终评估为真