当前位置: > c++ 多项式 Polynomial...
题目
c++ 多项式 Polynomial
#include
using namespace std;
struct Polynomial {
int degree;
int *coeffs; // array of coefficients from
// lowest degree to highest degree
};
// reads the coefficients of a polynomial from standard input and
// creates a polynomial which it returns
Polynomial* readPoly();
// outputs a polynomial to standard output using the variable x
void outputPoly( Polynomial* p,char x );
// computes the sum of two polynomials
Polynomial* addPoly( Polynomial* a,Polynomial* b );
// computes the product of two polynomials
Polynomial* multPoly( Polynomial* a,Polynomial* b );
int main()
{
char x;
Polynomial *p;
readPoly();
outputPoly(p,x);
return 0;
}
Polynomial* readPoly()
{
int deg,*coefficient;
coutdeg;
coefficient = new int[deg+1]; // space for deg+1 coefficient
cout degree = deg;
p -> coeffs = coefficient;
}
//if the last degree is associated with a zero ,the output will show an extra "+" sign in the end
void outputPoly(Polynomial* p,char x)
{
x = 'x';
//char sign = '+';
for(int i= 0;idegree;i++){
string sign = "+";
if(p->coeffs[i] == 0)
{
continue;
}
if(i == 0)
{
if((p->coeffs[i+1]degree))
sign = "";
coutcoeffs[i]degree)) //if it is the last coefficent with number
sign = "";
cout

提问时间:2021-01-24

答案
有个大问题:主函数里面的指针p,与Polynomial* readPoly()中的p,属于不同的作用域.两个是不同的两个指针变量.所以你在readPoly()当中创建了的多项式结构并没有赋给主函数里面的p,这时p所指向的内存是未知的,可能引起...
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.