| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:请问下这个程序有什么问题,溢出了吗?
只看楼主 加入收藏
成功了吗
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-4-10
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:1 
请问下这个程序有什么问题,溢出了吗?
#include "stdafx.h"
#include<iostream>

using namespace std;

//项结点类
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=0;
}
Term::Term(int c,int e, Term* nxt)
{
    link=nxt;
}
Term* Term::InsertAfter(int c, int e)
{
    link=new Term(c, e, link);
    return link;
}
ostream &operator<<(ostream & out, const Term& val)
{//重载“<<”,用coef X^exp表示
    if(val.coef==0) return out;
    out<<val.coef;
    switch(val.exp){
    case 0: break;
    case 1: out<<"X"; break;
    default: out<<"X^"<<val.exp; break;
    }
    return out;
}

//多项式类
class Polynominal
{
public:
    Polynominal();
    ~ Polynominal();
    void AddTerms(istream& in);
    void Output(ostream& out) const;
    void PolyAdd(Polynominal& r1);
    Polynominal PolyMul(Polynominal& r2);
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;  //构成循环链表
}
Polynominal::~Polynominal()
{
    Term* p = theList->link;
    while(p!=theList){
        theList->link = p->link;  //删除p结点
        delete p;  //释放p的存储空间
        p = theList->link;  //p指向下一待删结点
    }
    delete theList;  //释放表头结点的存储单元
}

// 多项式类的输入输出
void Polynominal:: AddTerms(istream & in)  //按降幂输入各项,构造单循环链表
{
    Term* q = theList;
    int c, e;
    for(;;){
        cout<<"Input a term(coef,exp):\n(end with (0, -1))\n"<<endl;
        cin>>c>>e;
        if (e<0) break;
        q = q->InsertAfter(c,e);  //将c,e插入表尾结点q之后
    }
}
void Polynominal:: Output(ostream& out) const
{
    int first=1; Term *p = theList->link;
    cout<<"The polynominal is:\n"<<endl;
    for(; p!=theList; p=p->link) {
        if(!first && (p->coef>0)) out<<"+";  //在非第一项的正系数前输出+号
        first=0;
        out<<*p;  //调用Term类上重载的“<<”操作
    }
    cout<<"\n"<<endl;
}

//多项式相加
void Polynominal::PolyAdd(
Polynominal & r1)
{// 将多项式r加到多项式this上
    Term* q, *q1 = theList, *p;  //q1指向表头结点
    p = r1.theList->link;  //p指向第一个要处理的结点
    q = q1->link;  //q1是q的前驱,p和q指向两个当前进行比较的项
    while(p->exp>0){  //对r1的单循环链表遍历,直到全部结点都处理完
        while(p->exp<q->exp) {   //跳过q->exp大的项p
            q1=q; q=q->link;
        }
        if(p->exp==q->exp) {
            q->coef = q->coef + p->coef;
            if(q->coef==0) {
                q1->link = q->link; delete(q);  //若相加后系数为0,则删除q
                q=q1->link;   //重置q指针
            }
        }
        else
            q1 = q1->InsertAfter(p->coef, p->exp);
        p=p->link;
    }
}

Polynominal Polynominal::PolyMul(Polynominal &r1)
{
    Term* q, *q1=theList, *p, *t2;
    Polynominal temp1, temp2;
    p = r1.theList->link;
    q = q1->link;
    t2 = temp2.theList->link;
    while(p->exp>0){
        while(q->exp>0){
            t2 = t2->InsertAfter(p->coef * q->coef, p->exp + q->exp);
            temp1=temp1+temp2;
            q=q->link;
        }
        p=p->link;
    }
    return temp1;
}



//几个友元类
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;
}

void main()
{
    Polynominal p,q;
    cin>>p; cout<<p;
    cin>>q; cout<<q;
    q=q.PolyMul(p);
    cout<<q;
    //cout<<q.PolyMul(p);
}

下面是编译提示:
#include "stdafx.h"
#include<iostream>

using namespace std;

//项结点类
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=0;
}
Term::Term(int c,int e, Term* nxt)
{
    link=nxt;
}
Term* Term::InsertAfter(int c, int e)
{
    link=new Term(c, e, link);
    return link;
}
ostream &operator<<(ostream & out, const Term& val)
{//重载“<<”,用coef X^exp表示
    if(val.coef==0) return out;
    out<<val.coef;
    switch(val.exp){
    case 0: break;
    case 1: out<<"X"; break;
    default: out<<"X^"<<val.exp; break;
    }
    return out;
}

//多项式类
class Polynominal
{
public:
    Polynominal();
    ~ Polynominal();
    void AddTerms(istream& in);
    void Output(ostream& out) const;
    void PolyAdd(Polynominal& r1);
    Polynominal PolyMul(Polynominal& r2);
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;  //构成循环链表
}
Polynominal::~Polynominal()
{
    Term* p = theList->link;
    while(p!=theList){
        theList->link = p->link;  //删除p结点
        delete p;  //释放p的存储空间
        p = theList->link;  //p指向下一待删结点
    }
    delete theList;  //释放表头结点的存储单元
}

// 多项式类的输入输出
void Polynominal:: AddTerms(istream & in)  //按降幂输入各项,构造单循环链表
{
    Term* q = theList;
    int c, e;
    for(;;){
        cout<<"Input a term(coef,exp):\n(end with (0, -1))\n"<<endl;
        cin>>c>>e;
        if (e<0) break;
        q = q->InsertAfter(c,e);  //将c,e插入表尾结点q之后
    }
}
void Polynominal:: Output(ostream& out) const
{
    int first=1; Term *p = theList->link;
    cout<<"The polynominal is:\n"<<endl;
    for(; p!=theList; p=p->link) {
        if(!first && (p->coef>0)) out<<"+";  //在非第一项的正系数前输出+号
        first=0;
        out<<*p;  //调用Term类上重载的“<<”操作
    }
    cout<<"\n"<<endl;
}

//多项式相加
void Polynominal::PolyAdd(
Polynominal & r1)
{// 将多项式r加到多项式this上
    Term* q, *q1 = theList, *p;  //q1指向表头结点
    p = r1.theList->link;  //p指向第一个要处理的结点
    q = q1->link;  //q1是q的前驱,p和q指向两个当前进行比较的项
    while(p->exp>0){  //对r1的单循环链表遍历,直到全部结点都处理完
        while(p->exp<q->exp) {   //跳过q->exp大的项p
            q1=q; q=q->link;
        }
        if(p->exp==q->exp) {
            q->coef = q->coef + p->coef;
            if(q->coef==0) {
                q1->link = q->link; delete(q);  //若相加后系数为0,则删除q
                q=q1->link;   //重置q指针
            }
        }
        else
            q1 = q1->InsertAfter(p->coef, p->exp);
        p=p->link;
    }
}

Polynominal Polynominal::PolyMul(Polynominal &r1)
{
    Term* q, *q1=theList, *p, *t2;
    Polynominal temp1, temp2;
    p = r1.theList->link;
    q = q1->link;
    t2 = temp2.theList->link;
    while(p->exp>0){
        while(q->exp>0){
            t2 = t2->InsertAfter(p->coef * q->coef, p->exp + q->exp);
            temp1=temp1+temp2;
            q=q->link;
        }
        p=p->link;
    }
    return temp1;
}



//几个友元类
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;
}

void main()
{
    Polynominal p,q;
    cin>>p; cout<<p;
    cin>>q; cout<<q;
    q=q.PolyMul(p);
    cout<<q;
    //cout<<q.PolyMul(p);
}
搜索更多相关主题的帖子: class private include public friend 
2010-04-18 22:09
asdjc
Rank: 6Rank: 6
来 自:武汉
等 级:侠之大者
威 望:7
帖 子:98
专家分:487
注 册:2010-1-22
收藏
得分:20 
太长了,把错误提示和行号写出来。
2010-04-19 16:05
快速回复:请问下这个程序有什么问题,溢出了吗?
数据加载中...
 
   



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

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