【发布时间】:2011-04-07 00:12:14
【问题描述】:
#include <iostream>
using namespace std;
struct Term;
struct Node;
typedef Term* termPtr;
typedef Node* list;
list cons(int degree,int coeff, list p);
struct Term
{
int degree;
int coeff;
};
struct Node
{
termPtr term;
list link;
};
class polynomial
{
private:
list poly;
static const int VARIABLE_X='X';
char variable;
public:
polynomial():poly(NULL),variable(VARIABLE_X){};
polynomial(int coef,int deg);
polynomial insert (termPtr t,list p);
int degree() const;
int coeff(int n) const;
void setPrintVariable (char x){variable=x;}
char getPrintVariable()const { return variable;}
friend const polynomial readPoly();
friend void printPoly(polynomial a);
void deletePoly();
friend const polynomial operator +(const polynomial &a,const polynomial &b);
friend const polynomial operator *(const polynomial &a,const polynomial &b);
};
polynomial::polynomial(int c,int d)
{
if(poly == NULL)//compiler doesnt understand this part
poly = cons(c,d,poly);
else // i put my cons here just to make the poly
poly=cons(c,d,poly);
}
list cons(int c,int d, list p)
{
termPtr aterm = new Term;
aterm->coeff=c;
aterm->degree=d;
list q = new Node;
q->term = aterm;
q->link = p;
return q;
}
void printPoly (polynomial a)
{
cout<<"[";
if(a.poly == NULL)
cout<<"]";
else
while(a.poly != NULL)
{
cout<<"("<<a.poly->term->coeff<<" X "<<a.poly->term->degree;
a.poly=a.poly->link ;
}
cout<<endl;
}
此代码使用链表来存储多项式。 一种结构用于多项式次数和系数;另一个结构是为了创建一个节点来创建一个链表。
我的代码有两个问题:
一个为 NULL 的空多项式,但在构造函数中我的条件语句没有找到它。
为什么我的打印方法不起作用。
我的打印方法有这个问题
polynomial.exe 中 0x00c1166b 处的未处理异常:0xC0000005:访问冲突读取位置 0xcccccccc。
【问题讨论】:
-
1. “编译器不理解 poly 是 NULL”是什么意思?编译器在做什么,为什么你认为它是错误的? 2.您的打印方法在什么情况下不起作用?它有什么作用?你希望它会做什么?
-
当我调试它时,构造中的条件(if (poly==NULL))被忽略了
-
1. “被忽略”是什么意思?你怎么知道它被忽略了? 2. 说到忽略,你忽略了我的第二个问题。
-
在两个参数的
polynomial构造函数中,你认为poly的值应该是什么?为什么? -
对不起。我没看到。首先-我的意思是我的 poly 是空的,但它似乎不为空。我应该使用什么条件语句来确定它是否为 NULL。第二 - 我想打印 poly: Unhandled exception at 0x00c1166b in polynomial.exe: 0xC0000005: Access violation reading location 0xcccccccc.
标签: c++