热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

一元多项式的实现

Term.h#ifndefTERM_H#defineTERM_H#include<iostream&g
Term.h
#ifndef TERM_H
#define  TERM_H

#include 
< iostream >
using   namespace  std;

class  Polynominal;
class  Term ... {
public:
    Term(
int c,int e);
    Term(
int c,int e,Term* nxt);
    Term
* InsertAfter(int c,int e);
private:
    
int coef;
    
int exp;
    Term
* link;
    friend ostream 
& operator<<(ostream &,const Term & );
    friend 
class Polynominal;
}
;

Term::Term(
int  c, int  e):coef(c),exp(e) ... {
    link 
= NULL;
}


Term::Term(
int  c, int  e,Term *  nxt):coef(c),exp(e) ... {
    link 
= nxt;
}


Term
*  Term::InsertAfter( int  c, int  e) ... {
    link 
= new Term(c,e,link);
    
return link;
}


ostream 
&   operator << (ostream  &   out , const  Term &  val) ... {
    
if(val.coef == 0return out;
    
out<<val.coef;
    
switch(val.exp)...{
        
case 0:break;
        
case 1:out<<"X";break;
        
default:out<<"X^"<<val.exp;break;
    }

    
return out;
}

#endif   // TERM_H


Polynominal.h
#ifndef POLYNOMINAL_H
#define  POLYNOMINAL_H
#include 
< iostream >

#include 
" Term.h "
using   namespace  std;
class  Polynominal ... {
public:
    Polynominal();
    
void AddTerms(istream& in);
    
void AddLast(int c,int e);
    
void Output(ostream& outconst;
    
void PolyAdd(Polynominal& r);
    
void MakeEmpty();
    
void PolyMultiply(Polynominal& );
private:
    Term
* theList;
    friend ostream 
& operator<<(ostream &const Polynominal &);
    friend istream 
& operator>>(istream &, Polynominal &);
    friend Polynominal
& operator+(Polynominal &, Polynominal &);
    friend Polynominal
& operator*(Polynominal &, Polynominal &);
}
;

Polynominal::Polynominal()
... {
    theList 
= new Term(0,-1);
    theList
->link = theList;
}


void  Polynominal::AddTerms(istream &   in ) ... {
    
int c,e;
    Term 
*= theList;
    
while(true)...{
        cout
<<"Input a term(coef,exp): "<<endl;
        
in>>c>>e;
        
if(e < 0break;
        q 
= q->InsertAfter(c,e);
    }

}


void  Polynominal::MakeEmpty() ... {
     Term 
*= theList->link;
     
while(p->link != theList)...{
                   theList
->link = p->link;
                   delete p;
                   p 
= theList->link;
     }

     theList
->link = theList;
     delete p;
}


void  Polynominal::AddLast( int  c, int  e) ... {
     Term 
*= theList;
     
while(p->link != theList) p = p->link;
     Term 
*temp = new Term(c,e,theList);
     p
->link = temp;
}

void  Polynominal::Output(ostream &   out const ... {
    
int first = 1;
    Term 
*= theList->link;
    
while(p != theList)...{
        
if(!first &&(p->coef > 0)) out<<"+";
        first 
= 0;
        
out<<*p;
        p 
= p->link;
    }

}


void  Polynominal::PolyAdd(Polynominal &  r) ... {
    Term 
* p = r.theList->link;
    Term 
*q1 = theList;
    Term 
*= theList->link;
    
while(p != r.theList)...{
        
while((q->exp > p->exp) && (q->link != theList))...{
            q 
= q->link;
            q1 
= q1->link;
        }

        
if(q->exp < p->exp) ...{
                  q1
->InsertAfter(p->coef,p->exp);
                  q1 
= q1->link;
        }

        
else if(q->exp == p->exp)...{
            q
->coef += p->coef;
        }

        
else...{
             q
->InsertAfter(p->coef,p->exp);
             q1 
= q;
             q 
= q->link;
        }

        p 
= p->link;
    }

}


void  Polynominal::PolyMultiply(Polynominal &  b) ... {
     Term 
*= theList->link;
     Term 
*= b.theList->link;
     Polynominal result,temp; 
     
if(p == theList || q == b.theList) return;
     
while(p != theList)...{
             
while( q != b.theList)...{
                    temp.AddLast(p
->coef*q->coef,p->exp+q->exp);
                    q 
= q->link;
             }

            result.PolyAdd(temp);
            temp.MakeEmpty();
            p 
= p->link; 
            q 
= b.theList->link;
     }

     theList 
= result.theList;
}


ostream
&   operator   << (ostream  & out , const  Polynominal  & x) ... {  
    x.Output(
out);   
    
return out;
}


istream
&   operator   >> (istream &   in , Polynominal  & x) ... {  
    x.AddTerms(
in);   
    
return in;
}


Polynominal
&   operator + (Polynominal  & a, Polynominal  & b)
... {  
    a.PolyAdd(b);
    
return a;
}


Polynominal
&   operator * (Polynominal  & a,Polynominal  & b) ... {
             a.PolyMultiply(b);
             
return a;
}



#endif   // POLYNOMINAL_H



main.cpp
#include  < cstdlib >
#include 
< iostream >
#include 
" Polynominal.h "
using   namespace  std;

int  main( int  argc,  char   * argv[])
... {
    Polynominal x,y;
    cout
<<"Input x :"<<endl;
    cin
>>x;
    cout
<<"x: "<<x<<endl;
    cout
<<"Input y:"<<endl;
    cin
>>y;    
    cout
<<"y: "<<y<<endl;
    cout
<<"x*y: "<<x*y<<endl;
    system(
"PAUSE");
    
return EXIT_SUCCESS;
}


1.如果要把多项式a的值赋给多项式b:a.theList = b.theList,不能写成a.theList->link = b.theList->link
2.考虑如下两段代码段
Term *p = theList->link;
while( p != theList) {
    cout<coef<    p = p->link;
}
把theList之后的每项系数都打印出来,最后p指向theList
Term *p = theList->link;
while(p->link != theList){
    cout<coef<     p = p->link;
}
把theList之后的每项(除了最后一项)系数都打印出来,最后p指向最后一项
3.这些代码不能在vc 6.0上编译通过,提示友元类不能访问私有成员的错误,据说这是vc 6.0的bug,但在dev c++可以顺利编译通过.

推荐阅读
  • 本文介绍了在Cpp中将字符串形式的数值转换为int或float等数值类型的方法,主要使用了strtol、strtod和strtoul函数。这些函数可以将以null结尾的字符串转换为long int、double或unsigned long类型的数值,且支持任意进制的字符串转换。相比之下,atoi函数只能转换十进制数值且没有错误返回。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 第五周项目一——体验常成员函数(1)
    设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等。在设计中,由于求距离、求对称点等操作对原对象不能造成任何改变,所以,将这些函数设计为常成员函数是合适的,能够避免数据成 ... [详细]
  • ProblemDescriptionAninchwormisatthebottomofawellninchesdeep.Ithasenoughene ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • http:blog.csdn.nethardVBarchive200710101818756.aspx请从opencv_share@163.com密码:download ... [详细]
  • IwasstudyingfasterIOmethodsforprogrammingproblems,Ifoundoutthismethodofusinggetchar ... [详细]
  • 要求:海伦公式:ssqrt(p*(p-a)*(p-b*)(p-c)),其中p(a+b+c)2,a,b,c为三角形的三个边。定义两个带参数的宏,一个用来求p,另一个用来求s题目分 ... [详细]
  • Here是指向最小代码的链接,如果消失了, ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系
    本文介绍了使用shell脚本判断IP是否在同一网段、判断IP地址是否在某个范围内、计算IP地址范围、判断网段之间的包含关系的方法和原理。通过对IP和掩码进行与计算,可以判断两个IP是否在同一网段。同时,还提供了一段用于验证IP地址的正则表达式和判断特殊IP地址的方法。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
author-avatar
夏雨荷Cassiopeia
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有