0

所以我的函数以降序积分多项式。

例如,3X^3-2x+1 = [3 0 2 1]

当然有限制,但这只是为了练习。

除非我输入一个 0 数组,否则我的积分会一直以 -1 或不正确的形式返回。

这给我带来了另一个问题,当输入前几个数字时,如果我输入一个非数字,它就会卡在我的 goto 循环中。我知道我不应该使用这些,请告诉我如何不使用它。

这是我的代码:

#include <iostream>
#include "eval.h"
int main ()
{
        using namespace std;              // yes i know using std:: is better... but I got lazy


        cout<<"This program takes the coefficients of a polinomial in decreasing \n "
                "degree. For example, x^2-1 = [1 0 -1] \n";
                                          // explains to the user what the function does, and         how to use it

        int columns;
            cout<<"Enter The highest degree plus one \n ";  // in other words, how many elements     the array will have
            cin>> columns;
            cout<<"Enter Your Coefficients\n ";

        float myMatrix[columns];                            //This was a code for practice, so I     decided to use for loops to get user input

                for (int i = 1; i<columns+1; ++i)
                {
                    cin>>myMatrix[i];
                }
                                                            //Im well aware i could have used other things
        float skeletonMatrix[columns+1];

                for (int ii = 1; ii< columns+8;++ii)        //for loop that injects user array into a skeleton matrix which is one element bigger beacuse of +C
                {
                    skeletonMatrix[ii] = (myMatrix[ii]/(columns+1-ii));
                }

            cout<<"The Integral is"<<endl;
                for (int iii = 1; iii<columns+1; ++iii)
                {
                        if (skeletonMatrix[iii] == 0 )
                        {
                            continue;
                        }
                    cout<<skeletonMatrix[iii]<<"x^"<<columns+1-iii<<" + ";
                }
            cout<<"C"<<endl;                                // Obviously I have not added the +C element because I will do that at another point, for a different purpose

            cout<<"Do you wish to Evaluate the Integral??? \n ";
            cout<<"Y of yes, N for no";
        char chYorN;
tryagain:
            cin>>chYorN;
        double dScalara;
        double dScalarb;
        double integral;
                switch(chYorN)
                {

                    case 'y':
                        {
                            cout<<"Enter where you want the integral \n to be evaluated"<<endl;
                            cout<<"e.g. from a to b would be a, then b"<<endl;
                            cin>>dScalara;
                            cin>>dScalarb;
                            integral = (eval(columns , dScalarb , myMatrix) - eval(columns , dScalara , myMatrix));
                            cout<<"The integral is"<< integral <<endl;
                        break;
                        }


                    case 'n':
                        {
                            cout<<"Thank you for using my program"<<endl;
                        break;
                        }

                    default:                                // At the matrix input, input a non number, and the program will go to this for some reason
                        {
                            cout<<"Try again Brah";
                        goto tryagain;                      // <-----im afraid it has to do with this, please show me how to replace this
                        }
                }

    return 0;
}

我的 Eval.h 如下:

#ifndef EVAL_H
#define EVAL_H
#include <math.h>

double eval(int columns, double dScalar, float myMatrix[])
{
    double dSum;
            for (int i=0; i<columns ; ++i)
            {
                myMatrix[columns-i]= myMatrix[columns-i]*(pow(dScalar,(double)i)); //evaluates a     function at dScalar
            }

            for (int ii=1; ii<columns+1;++ii)                                      // sums up the different parts
            {
                dSum+=myMatrix[ii];
            }

    return dSum;
}
#endif

如果有另一种显示我的数组的方式,请告诉我。我希望我可以连续显示它,而不是一列。

也是一个二维数组。我希望它显示如下:

1 0 2 2 0

0 1 2 3 0

0 0 0 0 1

4

2 回答 2

2

错误的一个可能原因可能是您的 goto 语句将控件放在变量声明之前。而不是这个:

tryagain:
    cin>>chYorN;
    double dScalara;
    double dScalarb;
    double integral;
            switch(chYorN)
            {

试试这个:

double dScalara;
double dScalarb;
double integral;
tryagain:
    cin>>chYorN;
    switch(chYorN)
     {

另外,请注意数组索引从零开始。如果有int a[3],它将被存储为a[0], a[1], a[2]而不是a[1], a[2], a[3]。(在我打字的时候,@Ed Heal 给出了一个关于这个的答案。所以,我不会详细说明)。

希望这有帮助。

于 2013-03-28T06:00:35.500 回答
1

你访问的各种数组都不正确。

例如这段代码:

    float myMatrix[columns];
    for (int i = 1; i<columns+1; ++i)
            {
                cin>>myMatrix[i];
            }

应该

    float myMatrix[columns];

            for (int i = 0; i<columns; ++i)
            {
                cin>>myMatrix[i];
            }

由于数组的索引从 0 开始,因此最终索引为columns-1.

您在其他各种阵列中也有类似的问题。

此错误将导致未定义的错误。

于 2013-03-28T05:51:19.517 回答