gpt4 book ai didi

多项式程序中的 C++ 动态数组和指针导致退出错误 255

转载 作者:行者123 更新时间:2023-11-30 05:36:47 25 4
gpt4 key购买 nike

我目前正在编写一个程序,该程序读取度数和系数并创建多项式结构。该程序可以将多项式相加和相乘,然后输出和或乘积。程序运行,输出正确答案,然后得到windows错误:

poly.exe 已停止工作问题导致程序停止正常工作。如果有可用的解决方案,Windows 将关闭该程序并通知您。

然后 scite 显示退出代码:255

我认为它可能是我用于乘法多边形的双 for 循环中的某些东西,或者是初始化指向系数数组的指针。但我不知道是什么。我正在使用的方法如下:

#include <iostream>
#include <stdio.h>
using namespace std;

struct Poly {
int degree;
int *coeff; //array of coefficients from lowest degree to highest degree
};

//Reads the coefficients of a polynomial from standard input
//Creates a poly struct and returns a pointer to the poly
Poly* readPoly();

//Outputs a polynomial to standard output with the variable x
void outputPoly(const Poly* p, char x);

//Computes the sum of two polynomials and returns
//a pointer to the new poly struct which is their sum
Poly* addPoly(const Poly* a, const Poly* b);

//Computes the product of two polynomials and returns
//a pointer to the new poly struct which is their product
Poly* multPoly(const Poly* a, const Poly* b);

//Returns to the heap the memory allocated for the polynomial
//and sets p to the nullptr
void deletePoly(Poly* &p);

Poly* readPoly() {
int deg;
//Read the highest degree
cout << "Input the degree: ";
cin >> deg;

//Handles when the degree is == 0
if(deg == 0) {
int *C = new int[deg+1];
Poly *p;
p = new Poly;
p->degree = deg;
p->coeff = C;
return p;
}

int *C = new int[deg+1];
//Read the coefficients
cout << "Input the coefficients: ";
for(int i = 0; i <= deg; i++) {
cin >> C[i];
}

//Create a new poly structure, assign its fields
//and retun a pointer to the new structure
Poly *p;
p = new Poly;
p->degree = deg;
p->coeff = C;

return p;
}

void outputPoly(const Poly* p, char x) {
//Set the degree and cooefficients to be used in the loop
int d = p->degree;
int *C = p->coeff;

//Output the polynomial, depending on the degree/coeff
for(int i = 0; i <= d; i++) {

//if the coeff is zero, and the degree is > 1
if(C[i] == 0 && i > 0)
continue; //Skip the +

//if the degree is 0, and the coeff is 0
else if(i == 0 && C[i] == 0) {
cout << C[i];
continue; //Skip the +
}

//if the degree is 0, and the coeff is not 0
else if(i == 0 && C[i] != 0)
cout << C[i];

//if the degree is 1, and the coeff is 0
else if(C[i] == 0 && i == 1)
cout << x;

//if the degree is 1, and the coeff is 1
else if(C[i] == 1 && i == 1)
cout << x;

//if the degree is 0, and the coeff is > 0
else if(C[i] > 0 && i == 0)
cout << C[i] << "*" << x;

//if the coefficient is 1
else if(C[i] == 1)
cout << x << "^" << i;

//if the degree is 1
else if(i == 1)
cout << C[i] << "*" << x;

//any other circumstance
else
cout << C[i] << "*" << x << "^" << i;

//Print a +, as long as it's not the last term
if(i != d)
cout << " + ";
}
}

void deletePoly(Poly* &p) {
delete[] p->coeff; //Delete the array first
delete p;
p = nullptr;
}

const Poly* getLargest(const Poly* a, const Poly* b) {
//Helper function to get the larger polynomial, given two
if(a->degree > b->degree)
return a;
else
return b;
}

const Poly* getSmallest(const Poly* a, const Poly* b) {
//Helper function to get the smaller polynomial, given two
if(a->degree < b->degree)
return a;
else
return b;
}

Poly* addPoly(const Poly* a, const Poly* b){
int i, j;

int *polyOneC = a->coeff;
int *polyTwoC = b->coeff;

//The new polynomials degree is the size of the polynomial that is the largest
int polyThreeD = getLargest(a, b)->degree;
int *polyThreeC = new int[polyThreeD];

for(i = 0, j = 0; j <= polyThreeD; i++, j++) {
//If the polynomials are of different size,
//then any coefficent term over the size
//of the smaller polynomial degree stays the same
if(i > getSmallest(a, b)->degree)
polyThreeC[i] = getLargest(a, b)->coeff[i];
else
//Otherwise, just add them
polyThreeC[i] = polyOneC[j] + polyTwoC[j];
//"Shifts" if it's equal to zero
if(polyThreeC[i] == 0)
i--;
}

//Ensures the remaining terms have a coefficient value(0)
while(i <= polyThreeD) {
polyThreeC[i] = 0;
i++;
}

Poly *sumPoly;
sumPoly = new Poly;
sumPoly->degree = polyThreeD;
sumPoly->coeff = polyThreeC;

return sumPoly;
}

Poly* multPoly(const Poly* a, const Poly* b) {

//Get the degrees and arrays of coefficients
int polyOneD = a->degree;
int polyTwoD = b->degree;

int *polyOneC = a-> coeff;
int *polyTwoC = b-> coeff;

int polyThreeD = polyOneD + polyTwoD;
int *polyThreeC = new int[polyThreeD + 1];

//Initialize the array of coefficients
for(int i = 0; i <= polyThreeD; i++)
polyThreeC[i] = 0;

//Multiple the coeffients and add to the position of the degree
for(int i = 0; i <= polyOneD; i++) {
for(int j = 0; j <= polyTwoD; j++) {
polyThreeC[i + j] += (polyOneC[i] * polyTwoC[j]);
}
}

//Create a new polynomial pointer and return it
Poly *productPoly;
productPoly = new Poly;
productPoly->degree = polyThreeD;
productPoly->coeff = polyThreeC;
return productPoly;
}

int main() {
Poly *x = readPoly();
Poly *y = readPoly();

//Test the add poly function
cout << "(";
outputPoly(x, 'x');
cout << ")";


cout << " + ";

cout << "(";
outputPoly(y, 'x');
cout << ")";
cout << endl;

//Call addPoly
Poly *z = addPoly(x, y);

cout << "= ";
cout << "(";
outputPoly(z, 'x');
cout << ")";

cout << endl;
cout << endl;

//Test the multiply poly function
cout << "(";
outputPoly(x, 'x');
cout << ")";


cout << " * ";

cout << "(";
outputPoly(y, 'x');
cout << ")";
cout << endl;

//Call multPoly
z = multPoly(x, y);

cout << "= ";
cout << "(";
outputPoly(z, 'x');
cout << ")";

cout << endl;


//Delete the polynomials now that we are done with them
deletePoly(x);
deletePoly(y);
deletePoly(z);
}

另一件事是错误不会在每次运行时发生,到目前为止,当我输入一个次数为 4 的多项式时,我已经看到了它。与次数为 2 的多项式相反,它有效很好。

谁能帮帮我!

最佳答案

您在 addPoly (polyThreeC) 中分配的系数数组不够大。你在分配数组的时候忘了给度数加一。

关于多项式程序中的 C++ 动态数组和指针导致退出错误 255,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33458352/

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