多项式相加

M0605050215 2009-03-12 01:37:13
我想用链表来实现多项式相加,程序没有完成,中途测试有点小错误:请大家帮帮忙....

//这是没有表头表尾结点的链表,构造函数采用头插法....这个文件我测试过没有任何问题;


//LinkList.h文件........................................
#include<iostream>
using namespace std;

template <class T>
struct Node
{
T data;
Node<T>* next;
};

template <class T>
class LinkList
{
private:
Node<T> *first;
int length;
public:
LinkList() {first=NULL;length=0;}
LinkList(T *a,int n);
int Locate(T x);
int Del(T x);
void Insert(int i,T x);
T Get(int i);
int GetLength() {return length;}
ostream& Print(ostream&);
~LinkList();
};

template <class T>
LinkList<T>::LinkList(T *a,int n)
{
first = new Node<T>;
first ->next = NULL;
for (int i=0; i<n; i++)
{
Node<T> *N = new Node<T>;
N ->data = a[i];
N ->next = first->next;
first ->next =N;
}
Node<T> *S=first;
first=first->next;
delete S;
length=n;
}

template <class T>
int LinkList<T>::Locate(T x)
{
Node<T> *N=first;
int i=0;
for(first,i;N!=NULL;i++)
{
if(x==N->data)
return i;
N = N->next;
}
cerr<<"NO This Number!!!";
return -1;
}

template <class T>
T LinkList<T> ::Get(int i)
{
if(i>=length)
{
cerr<<"越界!!"<<endl;
return -1;
}

Node<T> *N=first;
for (int j=0;j<i;j++)
N=N->next;
cout<<endl;
return N->data;
}

template <class T>
int LinkList<T>::Del(T x)
{
int locate = Locate(x);
if (-1==locate)
return -1;
Node<T> *N=first;
if (0 == locate)
{
first = first->next;
delete N;
return locate;
}
{
for(int i=0;i<locate-1;i++)
{
N=N->next;
}
Node<T> *S=N->next;
N->next=S->next;
delete S;
return locate;
}
}

template <class T>
void LinkList<T>::Insert(int i,T x) //如果用的是默认构造函数生成list,遇不能插入
{
Node<T> *S=new Node<T>; //注意:Node<T> S 与 Node<T> *S=new Node<T>区别
S->data = x;
if (i<0 || i>=length)
{
cerr<<"越界!!!!!!!!!"<<endl;
return;
}

if (0==i)
{
S->next=first;
first=S;
++length;
return;
}

Node<T> *N=first;
for (int j=0;j<i-1;j++)
N=N->next;
S->next=N->next->next;
N->next=S;
++length;
}

template <class T>
ostream& LinkList<T>::Print(ostream& o)
{
Node<T> *T=first;
o<<endl;
while(T!=NULL)
{
o<<T->data<<" ";
T=T->next;
}
o<<endl;
return o;
}


template<class T>
LinkList<T>::~LinkList()
{
Node<T>* P=first;
Node<T>* S;
while(P!=NULL)
{
S=P;
P=P->next;
delete S; //由于结点都是由new 申请的,所以用 delete 释放....
}
}

//下面是主函数所在文件,在这里函数重载有几个问题,请高人指点,,谢谢!!
//main.cpp文件
#include "LinkList.h"

typedef struct
{
double coe;
int exp;
friend ostream& operator <<(ostream& , const DATA& );
friend istream& operator >>(istream& , DATA& );
} DATA;


ostream& operator <<(ostream &os, const DATA &data)
{
os<<"("<<data.coe<<","<<data.exp<<")"<<"\t";
return os;
}

istream& operator >>(istream &is, DATA &data)
{
cout<<"分别输入系数和指数"<<endl;
is>>data.coe>>data.exp;
return is;
}

int main()
{
DATA *data = new DATA[];
int N;

//从键盘输入多项式A;
cout<<"输入多项式A的项数N";
cin>>N;
for (int i=0;i<N;i++)
cin>>data[i];
LinkList <DATA> A(data,N);

//从键盘输入多项式B;
cout<<"输入多项式B项数N";
cin>>N;
for(i=0;i<N;i++)
cin>>data[i];
LinkList <DATA> B(data,N);
return 0;
}


Compiling...
main.cpp
C:\Documents and Settings\Administrator\桌面\test\LinkList\main.cpp(7) : error C2143: syntax error : missing ',' before '&'
C:\Documents and Settings\Administrator\桌面\test\LinkList\main.cpp(7) : error C2059: syntax error : '&'
C:\Documents and Settings\Administrator\桌面\test\LinkList\main.cpp(8) : error C2061: syntax error : identifier 'DATA'
C:\Documents and Settings\Administrator\桌面\test\LinkList\main.cpp(8) : error C2805: binary 'operator >>' has too few parameters
C:\Documents and Settings\Administrator\桌面\test\LinkList\main.cpp(14) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.

...全文
214 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
angthingdone 2009-03-14
  • 打赏
  • 举报
回复
如果用typedef struct 定义的话 就不要写" friend ostream& operator <<(ostream& , const DATA& );
friend istream& operator >>(istream& , DATA& );”
也可以写成class DATA{
double coe;
int exp;
friend ostream& operator <<(ostream& , const DATA& );
friend istream& operator >>(istream& , DATA& );
};
这样也可以 ,,我也是刚学 多多指教!
M0605050215 2009-03-12
  • 打赏
  • 举报
回复
没有啊....上面的错误...
到底是么回事呢??
难道是编译器不行???????????????????????????????我用的是VC6.0
swordshi 2009-03-12
  • 打赏
  • 举报
回复
估计是输入'的问题
夹心饼干 2009-03-12
  • 打赏
  • 举报
回复
LZ是不是在拼音ABC下输入的',',如果是的话会有这个问题哦
根据错误看应该是就是个小问题吧
pengzhixi 2009-03-12
  • 打赏
  • 举报
回复
DATA *data = new DATA[];//括号里面的数字呢
M0605050215 2009-03-12
  • 打赏
  • 举报
回复
LinkList.h文件我单独测试过,基本上没有问题...也就是main.cpp有问题,重载<<与>>出现了上面问题.....
上面所有报错都在
friend ostream& operator <<(ostream& , const DATA& );
friend istream& operator >>(istream& , DATA& );
这两行,请大家帮我看看,,谢谢!!!!!
(DATA是封装一个多项式的系数和指数)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧