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

私の配列を表示する別の方法がある場合は、お知らせください。列ではなく、行で表示できたらいいのにと思います。

また、2次元配列も同様です。私はそれが次のように表示されることを望みます:

1 0 2 2 0

0 1 2 3 0

0 0 0 0 1

4

2 に答える 2

2

エラーの考えられる理由の 1 つは、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]保存されます。(これについては、@Ed Heal による回答があります。そのため、詳しくは説明しません)。a[0], a[1], a[2]a[1], a[2], a[3]

これが役に立ったことを願っています。

于 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 に答える