gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 00:48:34 24 4
gpt4 key购买 nike

我是编程新手,我正在尝试编写一个对多项式进行加减运算的程序。我的程序有时可以运行,但大多数时候,它随机崩溃,我不知道为什么。它有很多错误,还有其他我正在尝试修复的问题,但由于它崩溃,我无法真正完成任何进一步的编码。我在这里是全新的,但我们将不胜感激任何帮助。

代码如下:

#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;
}

最佳答案

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

此外,在 addsubtract 中,您分配给超出范围的系数(对于 i=0):

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

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

关于c++ - 在 C++ 中如何修复我的程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2535807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com