多项式相加
来源:互联网 发布:华育软件培训 编辑:程序博客网 时间:2024/05/17 06:38
利用顺序表实现多项式的相加
参考李春葆老师的数据结构教程
#ifndef __POLY_H_#define __POLY_H_#include <iostream>using namespace std;const int MaxSize = 100;struct PolyElem{ double coef; //系数 int exp; //指数};//class PolyClass{public: PolyClass( ); //构造函数 ~PolyClass( ); //析构函数 void DispPoly(); //输出多项式 void CreateListR(double a[], int b[], int n);//建立多项式 PolyClass& operator = (PolyClass &s); //重载 = 运算符 friend PolyClass& operator+(PolyClass&, PolyClass& );//友元函数,重载“+”运算符,用于求两个多项式相加private: PolyElem *data; int length;};//PolyClass::PolyClass(){ data = new PolyElem[MaxSize]; length = 0;}//PolyClass::~PolyClass(){ delete []data;}//void PolyClass::DispPoly(){ int i = 0; bool first = true; //first位true表示第一项 while(i<length) { if(first) first = false; else if(data[i].coef>0) cout<<"+"; if(data[i].exp == 0) //指数为0时不输出x cout<<data[i].coef; else if(data[i].exp == 1)//指数为1时不输出指数 cout<<data[i].coef<<"x"; else cout<<data[i].coef<<"x^"<<data[i].exp; ++i; } cout<<endl;}//void PolyClass::CreateListR(double a[], int b[], int n){ int i; for(i=0; i<n; ++i) { data[i].coef = a[i]; data[i].exp = b[i]; } length = n;}//PolyClass& PolyClass::operator=(PolyClass&s){ delete []data; //释放原有的空间 data = new PolyElem[MaxSize]; for(int i=0; i<s.length; ++i) { data[i].coef = s.data[i].coef; data[i].exp = s.data[i].exp; } length = s.length; return *this;}//PolyClass&operator+(PolyClass &poly1, PolyClass &poly2){ int i = 0,j = 0,k = 0; static PolyClass poly3; while((i<poly1.length)&&(j<poly2.length)) { if(poly1.data[i].exp == poly2.data[j].exp) { poly3.data[k].exp = poly1.data[i].exp; poly3.data[k].coef = poly1.data[i].coef + poly2.data[i].coef; ++i; ++j; ++k; } else if(poly1.data[i].exp < poly2.data[j].exp) { poly3.data[k].exp = poly1.data[i].exp; poly3.data[k].coef = poly1.data[i].coef; ++k; ++i; } else { poly3.data[k].exp = poly2.data[i].exp; poly3.data[k].coef = poly2.data[i].coef; ++k; ++j; } } while(i != poly1.length) { poly3.data[k].exp = poly1.data[i].exp; poly3.data[k].coef = poly1.data[i].coef; ++k; ++i; } while(j != poly2.length) { poly3.data[k].exp = poly2.data[i].exp; poly3.data[k].coef = poly2.data[i].coef; ++k; ++j; } poly3.length = k; return poly3;}#endif // __POLY_H_#include "Poly.h"int main(){ PolyClass poly1,poly2,poly3; //建立3个多项式顺序表对象 double a[MaxSize]; int b[MaxSize], n; //创建第一个多项式顺序表 a[0] = 2.0;a[1] = 3.2;a[2] = -6.0;a[3] = 10.0; b[0] = 0;b[1] = 1;b[2] = 3;b[3] = 5; n = 4; poly1.CreateListR(a,b,n); cout<<"输出第一个多项式: "; poly1.DispPoly(); //创建第二个多项式顺序表 a[0] = 6.0;a[1] = 1.8;a[2] = -2.0;a[3] = 1.0;a[4] = -2.5;a[5] = -5.0; b[0] = 0;b[1] = 1;b[2] = 2;b[3] = 3;b[4] = 4;b[5] = 5; n = 6; poly2.CreateListR(a,b,n); cout<<"输出第二个多项式: "; poly2.DispPoly(); //两个多项式相加 poly3 = poly1 + poly2; cout<<"相加后的两个多项式: "; poly3.DispPoly(); return 0;}
0 0
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 多项式相加
- 两个多项式相加!
- Google Map开发系列(十二)——定制GoogleBar --谷歌地图的本地搜索栏
- This project contains java compilation errors
- Hibernate框架简述
- 按钮点击跳出文件保存路径目录
- 2015,我们错过了邮件营销的哪些爆点?
- 多项式相加
- 一个奇怪的页面样式问题
- Linux .o a .so .la .lo的区别
- html5 canvas 详细使用教程
- 各种iOS界面效果
- java中反射机制的演示
- 做过的DP集合
- [javase学习笔记]-8.7 静态代码块
- 【Linux】使用update-alternatives命令进行版本的切换