如何从用户获取方程式的输入并在C ++中对其求值

时间:2019-07-10 07:20:58

标签: c++ math equation equation-solving

我想从用户那里得到一个数学(单变量代数)方程,例如 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 的值,我不能这样做。

我已经尝试过在Internet上搜索它,但是所有解决方案都没有帮助或太复杂以至于无法理解。

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

请不要投票,如果您在问题中发现任何错误,请在评论中告诉我;我会问我的问题。 预先感谢!

1 个答案:

答案 0 :(得分:0)

就像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