LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 903|回复: 2

再发代码

[复制链接]
发表于 2004-3-16 16:14:34 | 显示全部楼层 |阅读模式

  1. /****************************multinomial  V0.1********************************/
  2. /****这个类是看到论坛上有人问多项式的数据结构写的,只实现了多项式加法********/
  3. /****当然,这个类还可以扩展,可以重载*运算符,-运算符等等,还有构造***********/
  4. /****函数也可以再进行扩展,使多项式初始化界面能够再漂亮一些******************/
  5. /****我的C++不太好,英文单词也是用金山词霸查的,也许变量定义的不太好*******/
  6. /****请大家指正**************************************************************/

  7. #ifndef _multinomial_h
  8. #define _multinomial_h
  9. #include <deque>
  10. #include <exception>
  11. namespace com_ybokham_multinomial  //学习java的名字空间规则
  12. {
  13. using namespace std;
  14. class multinomial         //multinomial的中文意思是多项式
  15. {
  16.     private:
  17.         deque<double> data;       //使用STL中的deque容器作为多项式系数的容器
  18.         int length;           //多项式变量的最高指数,其实跟data.size()相等
  19.     public:
  20.         multinomial():data(deque<double>()),length(0){}    //默认构造函数
  21.                
  22.         multinomial(size_t size,const double val = 0):data(deque<double>(size,val)),length(size)
  23.         {}  //已知最高幂指数的构造函数
  24.                
  25.         multinomial(multinomial &in):data(in.getData()),length(in.getLength())
  26.         {}  //拷贝构造函数
  27.                
  28.         void setCoefficient(int power, doublecoefficient)//Coefficient的英文意思是系数
  29.         {                                      //这个函数用来给那些已经初始化deque大小的多项式
  30.                 data.at(power) = coefficient;   //设置各系数
  31.         }
  32.         void setCoefficient(double coefficient)  //这个函数用来给使用默认构造函数构造的多项式
  33.         {                                        //设置系数
  34.                 data.push_front(coefficient);
  35.                 length = data.size();
  36.         }
  37.         double &getCoefficient(int power)//获得指定幂指数的系数   
  38.         {
  39.                 return data.at(power);
  40.         }
  41.         multinomial &operator=(multinomial &in)   //重载=运算符
  42.         {
  43.                this->data = in.getData();
  44.                this->length = in.getLength();
  45.                return *this;
  46.         }
  47.                
  48.         int &getLength()       //得到多项式的最高次幂
  49.         {
  50.                 return length;
  51.         }
  52.         deque<double> &getData()   //得到多项式的各系数
  53.         {
  54.                 return data;
  55.         }
  56.         multinomial &operator+(multinomial &in)     //重载+运算符
  57.         {
  58.                 int shortSize = this->getLength() < in.getLength()?this->getLength():in.getLength();
  59.                 int longSize = this->getLength()> in.getLength()?this->getLength():in.getLength();
  60.                 for(int i=0;i<shortSize;++i)
  61.                 {
  62.                     this->setCoefficient(i,(this->getCoefficient(i)+in.getCoefficient(i)));
  63.                 }
  64.                 if(this->getLength() >= in.getLength())
  65.                 {
  66.                     return *this;
  67.                 }
  68.                 else
  69.                 {
  70.                     this->data.resize(in.getLength());
  71.                     for(int i=shortSize;i<longSize;++i)
  72.                     {
  73.                         this->setCoefficient(i,in.getCoefficient(i));
  74.                     }
  75.                     length = this->data.size();
  76.                     return *this;
  77.                 }
  78.         }      
  79.     };
  80. }   
  81. #endif
复制代码
 楼主| 发表于 2004-3-16 16:18:39 | 显示全部楼层

简单使用示例

此示例显示两个多项式相加,开始时读入多项式的各系数,这里采用按降幂顺序写入系数,0系数也要输入。
最后是显示出相加以后的结果。在windows下dev-cpp编译通过

  1. #include "multinomial.h"
  2. #include <iostream>
  3. #include <stdlib.h>

  4. using namespace com_ybokham_multinomial;
  5. using namespace std;
  6. int main()
  7. {
  8.     multinomial multi1,multi2,multi3;
  9.     double data;
  10.     cout << " Please input multi1's coefficient by descending order with 1000 as ending: " << endl;
  11.     try
  12.     {
  13.         for(cin >> data ; data!= 1000; )
  14.         {
  15.                multi1.setCoefficient(data);
  16.                cin >> data;
  17.         }
  18.         cout << " Please input multi2's coefficient by descending order with 1000 as ending: " << endl;
  19.         for(cin >> data; data !=1000; )
  20.         {
  21.                multi2.setCoefficient(data);
  22.                cin >>data;
  23.         }
  24.     }
  25.     catch(exception &e)
  26.     {
  27.        cout << e.what() << endl;
  28.        return 1;
  29.     }
  30.    
  31.     multi3 = multi1 + multi2;
  32.     for(int i = 0; i < multi3.getLength(); i++)
  33.     {
  34.         cout << multi3.getCoefficient(i) << endl;
  35.     }
  36.     system("pause");
  37.     return 0;

  38. }
复制代码
 楼主| 发表于 2004-3-16 16:27:05 | 显示全部楼层

使用deque的原因

因为deque提供头部和尾部的插入,而且提供高效的随机下标访问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表