单链表实现多项式加法的头文件(待删改)

来源:互联网 发布:年轻人牙膏知乎 编辑:程序博客网 时间:2024/05/17 02:47
问题找到了
1,赋值构造函数时head没有申请内存,成了野指针
2,加法实现时的if判断应该是写成if...else...,否则可能会if多次
3,还有其他一些小问题

下面的代码还没有改



编译可通过,但在进行加法运算时会突然停止


#include <iostream>
#include <cmath>
using namespace std;
//Define the point struct and line class
//Using struct and class type
struct point                                             
{
 double pi;
 double qi;                                           
 point* next;
};

class line                                                
{
 point* head;                                        
 int size;                                           
public:
 int gets()const { return size;}
 point* getp()const { return head;}

 line(int t = 0)
 {
  size = t;
  head = new point;
 }                                 
 line(const line & l1)                                
 {
  point* p = head;
  point* q = l1.getp();
  size = l1.gets();
  for (int i = 0; i < size ; i ++)
  {
   if (q->next)
   {
    p->next = new point;
    q = q->next;
    p = p->next;
    p->pi = q->qi;
    p->qi = q->qi;
   }
   else
   {
    p->next = NULL;
    break;
   }
  }
 }
 //friend void add(line & l1,line & l2);
 line operator+(line & l)
 {
  point* p = this->head->next;
  point* q = l.getp()->next;
  while (p && q)
  {
   if (p->qi > q->qi) p = p->next;
   if (p->qi == q ->qi) {p->pi += q->pi; p = p->next; q = q->next; }
   if (p->qi < q->qi)
   {
    point* temp;
    temp = q->next;
    q->next = p->next;
    p->next = q;
    p = q->next;
    q = temp;
   }
  }
  if (!p) p = q;
  return *this;
 }
 /*friend line operator-(const line & l1,const line & l2)
 {
  line l(0);
  point* t = l.head;
  point* t1 = l1.head->next;
  point* t2 = l2.head->next;
  while (t1 && t2)
  {
   t->next = new point;
   t = t->next;
   if ( t1->qi > t2->qi ) { t->qi = t1->qi; t->pi = t1->pi; t1 = t1->next; l.size ++; }
   if ( t1->qi == t2->qi ) { t->qi = t1->qi; t->pi = t1->pi - t2->pi; t1 = t1->next; t2 = t2->next; l.size ++; }
   if ( t1->qi < t2->qi ) { t->qi = t2->qi; t->pi = -t2->pi; t2 = t2->next; l.size ++; }
  }
  while (t1)
  {
   t->next = new point;
   t = t->next;
   t->qi = t1->qi;
   t->pi = t1->pi;
   t1 = t1->next;
   l.size ++;
  }
  while (t2)
  {
   t->next = new point;
   t = t->next;
   t->qi = t2->qi;
   t->pi = -t2->pi;
   t2 = t2->next;
   l.size ++;
  }
  delete t;
  t = NULL;
  return l;
 }
 /*friend line operator*(const line & l1, const line & l2)
 {
  
 }*/

 friend istream& operator>>(istream & input,line & l)
 {
  point* pt=l.head;
  cout << "Please input elements with descending order" << endl;
  for ( int i = 0 ; i < l.size ; i ++ )
  {
   pt->next = new point;
   pt = pt->next;
   cout << "Please input the " << i+1 <<"th element's pi" << endl;
   input >> pt->pi;
   cout << "Please input the " << i+1 <<"th element's qi" << endl;
   input >> pt->qi;
  }
  pt->next = NULL;
  return input;
 }
 friend ostream& operator<<(ostream & output,line & l)
 {
  point* pt = l.head->next;
  for ( int i = 0; i < l.size-1; i ++)
  {
   if (pt->pi)
   {
    cout << pt->pi << "X^" << pt->qi << " + ";
    pt = pt->next;
   }
   else
   {
    pt = pt->next;
    continue;
   }
  }
  if (pt->pi) cout << pt->pi << "X^" << pt->qi << endl;
  else cout << 0 << endl;
  return output;
 }
};

0 0
原创粉丝点击