如何从用户那里获取方程式的输入并在 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<<"\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);
}

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

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

我已经尝试在互联网上搜索它,但所有的解决方案要么没有帮助,要么过于复杂,难以理解。

我是一个非常新的程序员,我对数据结构、bison 或任何类似的解析器一无所知。请explain/help我用简单的方式,越简单越好。

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

正如 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