如何修复我的程序在C ++崩溃?

时间:2010-03-29 04:39:16

标签: c++

我是编程的新手,我正在尝试编写一个添加和减去多项式的程序。我的程序有时可以工作,但大多数时候,它随机崩溃,我不知道为什么。这是非常错误的,并且还有其他我正在尝试解决的问题,但我无法真正得到任何进一步的编码,因为它崩溃了。我在这里完全是新人,但任何帮助都将不胜感激。

以下是代码:

#include <iostream>
#include <cstdlib>

using namespace std;

int getChoice();
class Polynomial10
{
private:
    double* coef;
    int degreePoly;

public:
    Polynomial10(int max); //Constructor for a new Polynomial10
    int getDegree(){return degreePoly;};
    void print(); //Print the polynomial in standard form
    void read(); //Read a polynomial from the user
    void add(const Polynomial10& pol); //Add a polynomial
    void multc(double factor); //Multiply the poly by scalar
    void subtract(const Polynomial10& pol); //Subtract polynom
};

void Polynomial10::read()
{
    cout << "Enter degree of a polynom between 1 and 10 : ";
    cin >> degreePoly;

    cout << "Enter space separated coefficients starting from highest degree" << endl;
    for (int i = 0; i <= degreePoly; i++) cin >> coef[i];
}

void Polynomial10::print()
{
    for (int i = 0;i <= degreePoly; i++) {
       if (coef[i] == 0) cout << "";
       else if (i >= 0) {
           if (coef[i] > 0 && i != 0) cout<<"+";
           if ((coef[i] != 1 && coef[i] != -1) || i == degreePoly) cout << coef[i];
           if ((coef[i] != 1 && coef[i] != -1) && i != degreePoly ) cout << "*";
           if (i != degreePoly && coef[i] == -1) cout << "-";
           if (i != degreePoly) cout << "x";
           if ((degreePoly - i) != 1 && i != degreePoly) {
               cout << "^";
               cout << degreePoly-i;
           }
       }
   }
}

void Polynomial10::add(const Polynomial10& pol)
{
    for(int i = 0; i<degreePoly; i++) {
        int degree = degreePoly;
        coef[degreePoly-i] += pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::subtract(const Polynomial10& pol)
{
    for(int i = 0; i<degreePoly; i++) {
        coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];
    }
}

void Polynomial10::multc(double factor)
{
    //int degreePoly=0;
    //double coef[degreePoly];
    cout << "Enter the scalar multiplier : ";
    cin >> factor;
    for(int i = 0; i<degreePoly; i++) coef[i] *= factor;
}

Polynomial10::Polynomial10(int max)
{
    degreePoly = max;
    coef = new double[degreePoly];
    for(int i; i < degreePoly; i++) coef[i] = 0;
}

int main()
{
    int choice;
    Polynomial10 p1(1),p2(1);
    cout << endl << "CGS 2421: The Polynomial10 Class" << endl << endl << endl;
    cout
        << "0. Quit\n"
        << "1. Enter polynomial\n"
        << "2. Print polynomial\n"
        << "3. Add another polynomial\n"
        << "4. Subtract another polynomial\n"
        << "5. Multiply by scalar\n\n";

    int choiceFirst = getChoice();
    if (choiceFirst != 1) {
        cout << "Enter a Polynomial first!";
    }
    if (choiceFirst == 1) {choiceFirst = choice;}

    while(choice != 0) {
        switch(choice) {
            case 0:
                return 0;
            case 1:
                p1.read();
                break;
            case 2:
                p1.print();
                break;
            case 3:
                p2.read();
                p1.add(p2);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
            case 4:
                p2.read();
                p1.subtract(p2);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
            case 5:
                p1.multc(10);
                cout << "Updated Polynomial: ";
                p1.print();
                break;
        }
        choice = getChoice();
    }
    return 0;
}

int getChoice()
{
    int c;
    cout << "\nEnter your choice : ";
    cin >> c;
    return c;
}

3 个答案:

答案 0 :(得分:3)

使用p1(1)p2(1)创建对象时,每个对象中的coef数组被分配为包含一个元素。然后在read()中,您只需将degreePoly设置为(可能更高)值,但不要更改coef的分配。它仍然只包含一个元素,但所有系数都写入它,可能是在数组的边界上写入。相反,应释放旧的coef并分配适当大小的新数组。

同样在addsubtract中,您指定的系数超出范围(对于i = 0):

coef[degreePoly-i] -= pol.coef[degreePoly-(i+1)];

在数学上从索引degreePoly-(i+1)中减去索引degreePoly-i处的系数似乎也是错误的。此外,目前尚未处理两个多边形可能具有不同度数的情况。

答案 1 :(得分:2)

一个问题是coef分配。您正在分配degreePoly = max个元素的数组(两个实际对象为1)。但我认为它应该是max + 1,因为n次多项式具有n + 1个系数。这个逐个错误出现在其他地方(例如add和mult)。同样在构造函数中,您没有初始化i,因此它有一个未定义的值,这意味着您可以(可能会)写入您无法控制的内存。

稍后,在您的阅读代码中,您会覆盖degreePoly,但不会重新分配coef,因此它可能是错误的大小,至少如果您超过最大值。另请注意,您接受degreePoly + 1系数(我认为这是正确的)。这意味着构造函数中的max只对应于读取中的degreePoly 0!在read中输入1将导致读取两个值,这将使数组溢出。

写入无效指针位置并使用未初始化的值会导致未定义的行为,这就是为什么它有时会崩溃

答案 2 :(得分:0)

if (choiceFirst == 1) {choiceFirst = choice;}

没有意义,因为choice在此阶段未初始化。你可能想要:

if (choiceFirst == 1) {choice = choiceFirst;}