单链表实现多项式加法的头文件(待删改)
来源:互联网 发布:年轻人牙膏知乎 编辑:程序博客网 时间:2024/05/17 02:47
编译可通过,但在进行加法运算时会突然停止
#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;
}
};
- 单链表实现多项式加法的头文件(待删改)
- 多项式加法的实现
- 单链表实现多项式的存储和加法
- 多项式加法的实现(链表版)
- 多项式加法的Java实现
- 多项式的加法乘法实现
- 单链表的应用——多项式加法的C语言实现(链式存储结构)
- 多项式的加法与乘法(C++实现)
- 用链表实现一元多项式的加法运算
- 链表实现两个多项式的加法
- 一元多项式加法——单链表实现
- C++实现多项式加法
- 多项式加法-->动态实现
- NOJ_1005_多项式加法(采用尾插法建立单链表的运用)
- //单链表1-求两个多项式的加法
- 多项式的加法运算(等待修改)
- 多项式的加法
- 两个多项式的加法
- 软件产品经理的职责
- 数据库设计的三大范式、BCNF、4NF
- oracle获取sql执行计划方法
- IP,TCP 和 HTTP
- wifidog 源码初分析(1)
- 单链表实现多项式加法的头文件(待删改)
- Qt Creator 重要的快捷操作
- Flex和Servlet结合上传文件报错(一)
- 百度API 应用实例之音乐搜索
- wzplayer for ios 针对(mms)优化版本V1.0
- 常见的计算机网络面试题目
- 说明符和限定符
- C++ 程序员自信心曲线图
- android项目中spinner设置默认值