| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 375 人关注过本帖
标题:一个关于重载的函数看不懂
只看楼主 加入收藏
C_Y_ME
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-5-22
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:4 
一个关于重载的函数看不懂
Polynomial & Polynomial::operator + (Polynomial & add)
{
    int number = add.num, mrk = 0;
    Polynomial & mi = *this;
    int cosize = add.size; int * q = new int[cosize];
    for (int d=0;d<cosize;++d) q[d]=add.a[d];
    for (int i = 0, s = 0; i < number; ++i, s+=2)
    {
        mrk = 0;
        for (int j = 0, k = 0; j < mi.num; ++j,k+=2)
        {
            if (q[s]==mi.a[k])
            {
                mrk = 1;
                mi.a[k+1]+=q[s+1];
                q[s+1]=0;
            }
        }
        if (!mrk)
        {
            int *p = new int[mi.size+2];
            for (int d=0;d<mi.size;d++)
                p[d]=mi.a[d];
            p[mi.size] = q[s];
            p[mi.size+1] = q[s+1];
            q[s+1]=0;
            mi.a = p;
            mi.num++;
            mi.size+=2;
        }
    }
    return *this;
}
搜索更多相关主题的帖子: number 
2013-05-22 20:28
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:10 
有什么不懂的,这不是写得挺好的?

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-05-22 21:27
C_Y_ME
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-5-22
收藏
得分:0 
回复 2楼 peach5460
新手啊,多项式重载加法,这个要是主函数调用是不是很麻烦啊?
2013-05-22 22:35
C_Y_ME
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-5-22
收藏
得分:0 
回复 2楼 peach5460
程序代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <cstring>
   
using namespace std;
   
class Polynomial
{

 public:
      Polynomial(int  = 0);
      Polynomial & operator + (Polynomial & add);
      Polynomial & operator - (Polynomial & add);
      Polynomial & operator * (Polynomial &);
      Polynomial & operator += (Polynomial & add);
      Polynomial & operator -= (Polynomial & add);
      Polynomial & operator *= (Polynomial &);
      Polynomial & test(Polynomial & right);

 private:
     int *a;
     int size, num;
};

Polynomial & Polynomial::operator + (Polynomial & add)
{
    int number = add.num, mrk = 0;
    Polynomial & mi = *this;
    int cosize = add.size; int * q = new int[cosize];
    for (int d=0;d<cosize;++d) q[d]=add.a[d];
    for (int i = 0, s = 0; i < number; ++i, s+=2)
    {
        mrk = 0;
        for (int j = 0, k = 0; j < mi.num; ++j,k+=2)
        {
            if (q[s]==mi.a[k])
            {
                mrk = 1;
                mi.a[k+1]+=q[s+1];
                q[s+1]=0;
            }
        }
        if (!mrk)
        {
            int *p = new int[mi.size+2];
            for (int d=0;d<mi.size;d++)
                p[d]=mi.a[d];
            p[mi.size] = q[s];
            p[mi.size+1] = q[s+1];
            q[s+1]=0;
            mi.a = p;
            mi.num++;
            mi.size+=2;
        }
    }
    return *this;
}

Polynomial & Polynomial::operator - (Polynomial & add)
{
  int number = add.num, mrk = 0;
  int cosize = add.size; int * q = new int[cosize];
  for (int d=0;d<cosize;++d) q[d]=add.a[d];
  for (int i = 0, s = 0; i < number; ++i, s+=2)
  {
      mrk = 0;
      for (int j = 0, k = 0; j < num; ++j,k+=2)
      {
          if (q[s]==a[k])
          {
              mrk = 1;
              a[k+1]-=q[s+1];
              q[s+1]=0;
          }
      }
      if (!mrk)
       {
          int *p = new int[size+2];
          for (int d=0;d<size;d++)
              p[d]=a[d];
          p[size] = q[s];
          p[size+1] = -q[s+1];
          q[s+1]=0;
          a = p;
          num++;
          size+=2;
        }
   }
   return *this;
}

Polynomial & Polynomial::operator * (Polynomial & mu)
{
    Polynomial b, c;
    c = *this; b = *this;
    for (int j=0,r=0; j<b.num;++j,r+=2)
    {
       (*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
    }
   for (int i=1,k=2;i<mu.num;++i,k+=2)
   {
         for (int j=0,r=0; j<b.num;++j,r+=2)
         {
            b.a[r]+=mu.a[k];
            b.a[r+1]*=mu.a[k+1];
         }
        *this += b;
         b = c;
    }
   return *this;

 }


 
Polynomial & Polynomial::operator += (Polynomial & add)
{
    int number = add.num, mrk = 0;
    int cosize = add.size; int * q = new int[cosize];
    for (int d=0;d<cosize;++d) q[d]=add.a[d];
    for (int i = 0, s = 0; i < number; ++i, s+=2)
    {
        mrk = 0;
        for (int j = 0, k = 0; j < num; ++j,k+=2)
        {
           if (q[s]==a[k])
            {
                mrk = 1;
                a[k+1]+=q[s+1];
                q[s+1]=0;
            }
        }
        if (!mrk)
        {
            int *p = new int[size+2];
            for (int d=0;d<size;d++)
                p[d]=a[d];
                p[size] = q[s];
                p[size+1] = q[s+1];
                q[s+1]=0;
                a = p;
             num++;
             size+=2;
        }
    }
    return *this;
}

 
Polynomial & Polynomial::operator -= (Polynomial & add)
{
    int number = add.num, mrk = 0;
    int cosize = add.size; int * q = new int[cosize];
    for (int d=0;d<cosize;++d) q[d]=add.a[d];
    for (int i = 0, s = 0; i < number; ++i, s+=2)
    {
        mrk = 0;
        for (int j = 0, k = 0; j < num; ++j,k+=2)
        {
            if (q[s]==a[k])
            {
                mrk = 1;
                a[k+1]-=q[s+1];
                q[s+1]=0;
            }
        }
      if (!mrk)
      {
          int *p = new int[size+2];
          for (int d=0;d<size;d++)
             p[d]=a[d];
             p[size] = q[s];
             p[size+1] = -q[s+1];
             q[s+1]=0;
             a = p;
             num++;
            size+=2;
      }
    }
    return *this;
}

Polynomial & Polynomial::operator *= (Polynomial & mu)
{
    Polynomial b, c;
    b = *this;
    c = *this;
    for (int j=0,r=0; j<b.num;++j,r+=2)
    {
        (*this).a[r]+=mu.a[0]; (*this).a[r+1]*=mu.a[1];
    }
    for (int i=1,k=2;i<mu.num;++i,k+=2)
    {
        for (int j=0,r=0; j<b.num;++j,r+=2)
        {
             b.a[r]+=mu.a[k];
             b.a[r+1]*=mu.a[k+1];
        }
        *this += b;
        b = c;
     }
     return *this;
}

int main(void)
{
    return 0;
}
2013-05-22 22:36
apull
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:三体星系
等 级:版主
威 望:216
帖 子:1509
专家分:9241
注 册:2010-3-16
收藏
得分:10 
运算符重载,你可以看看
http://www.
2013-05-23 01:57
快速回复:一个关于重载的函数看不懂
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030068 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved