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
人气:170 ℃ 时间:2020-08-25 09:36:48
解答
有个大问题:主函数里面的指针p,与Polynomial* readPoly()中的p,属于不同的作用域.两个是不同的两个指针变量.所以你在readPoly()当中创建了的多项式结构并没有赋给主函数里面的p,这时p所指向的内存是未知的,可能引起...
推荐
- C++问题:定义一个多项式类Polynomial,其实例为多项式:a0+a1x+a2x2+...+anxn
- Factoring a Polynomial有关能否构成多项式的定理
- C程序设计 急啊多项式计算输入并建立多项式;输出多项式;两个多项式相加,建立并输出和多项式两个多项式
- M是5次多项式,N是4次多项式,则M+N是() A.9次多项式 B.4次多项式 C.5次多项式 D.1次多项式
- 若B是一个四次多项式,C是一个二次多项式,则“B -C ”( ) A.可能是七次多项式 B 一定是大于七项的多项式
- 自欺欺人的意思
- 学雷锋,知党恩,讲道德,见行动,800字作文
- 用隐秘,幻想,信念,痴想,其中的三个造一个句
猜你喜欢